Tuesday, January 29, 2013

Opening a webpage In Browser


We can open a Web Page with Given URL in Android.  To do this start an Activity passing the URL of the WebPage as data.

Make sure you declare following permission to access Internet

<uses-permission android:name="android.permission.INTERNET" /> 

 Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse("http://www.indianrail.gov.in/pnr_Enq.html"));
                    startActivity(i);





Sending Email In Android

In Android we can send Email  using following Code

                         Intent i = new Intent(Intent.ACTION_SEND);
                                 i.setType("message/rfc822");
                                 i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recieveremail@gmail.com"});
                                 i.putExtra(Intent.EXTRA_SUBJECT, "Subject of Mail");
                                 i.putExtra(Intent.EXTRA_TEXT   , "The Message Body");
                                 try {
                                           startActivity(Intent.createChooser(i, "Sending mail..."));
                                         
                                   }
                                 finally {
                                     

                                          // Code to execute when unable to send Email
                                      }



Activity In Android

An activity presents a visual user interface for one focused endeavor the  user can undertake.
An application might consist of just one activity or many.
Each one is implemented as a subclass of the Activity base class

An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. 

An application usually consists of multiple activities that are loosely bound to each other. Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application for the first time. Each activity can then start another activity in order to perform different actions. Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack"). When a new activity starts, it is pushed onto the back stack and takes user focus.. 


Declaring Activity In Manifest



<manifest ... >
  <application ... >
      <activity android:name=".ExampleActivity" />
      ...
  </application ...> 
</manifest >
 
 
 

Using Intent Filters  In Activity Tag

<activity android:name=".ExampleActivity" android:icon="@drawable/app_icon">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity> 
 
The <action> element specifies that this is the "main" entry point to the application.
 The <category> element specifies that this activity should be listed in the
system's application launcher (to allow users to launch this activity). 


Starting An Activity:



You can start another activity by calling startActivity(), passing it an Intent that describes the activity you want to start. The intent specifies either the exact activity you want to start or describes the type of action you want to perform

 
     Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent); 

Implementing the lifecycle callbacks:

When an activity transitions into and out of the different states described above, it is notified through various callback methods. All of the callback methods are hooks that you can override to do appropriate work when the state of your activity changes. The following skeleton activity includes each of the fundamental lifecycle methods:

public class ExampleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // The activity is being created.
    }
    @Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible.
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
    }
    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    }
}

 

More Android Topics

Android : Introduction

       Eclipse Setup for Android Development

                     Configuring Eclipse for Android Development

          Begging With Android

                     Creating Your First Android Project

         Working With Layouts

                      Understanding Layouts in Android
                          Working with Linear Layout (With Example)
                                Nested Linear Layout (With Example)
                          Table Layout
                          Frame Layout(With Example)
                         Absolute Layout
                         Grid Layout

       Activity

                     Activity In Android
                     Activity Life Cycle
                     Starting Activity For Result
                     Returning Result from Activity

     Working With Views

                     Using Buttons and EditText in Android 
                     Using CheckBoxes in Android 
                     Using AutoCompleteTextView in Android
                     Grid View

     Dialogs In Android

                     Working With Alert Dialog
                     Adding Radio Buttons In Dialog
                     Adding Check Boxes In Dialog
                     Creating Customized Dialogs in Android
                    Adding EditText in Dialog
                   Creating Dialog To Collect User Input

                 DatePicker and TimePickerDialog

                              Using TimePickerDialog and DatePickerDialog In android

    Working With SMS

                  How to Send SMS in Android
                  How To Receive SMS
                  Accessing Inbox In Android

    ListView:

               Populating ListView With DataBase

      Menus In Android

                    Creating Option Menu
                    Creating Context Menu In Android

      TelephonyManager

                    Using Telephony Manager In Android

     Working With Incoming Calls

                    How To Handle Incoming Calls in Android
                    How to Forward an Incoming Call In Android
                   CALL States In Android

    Miscellaneous

                   Notifications In Android
                   How To Vibrate The Android Phone
                   Sending Email In Android
                  Opening a webpage In Browser
                   How to Access PhoneBook In Android
                   Prompt User Input with an AlertDialog

   Storage:  Storing Data In Android

               Shared Prefferences  In Android

                             SharedPreferences In Android

               Files: File Handling In Android

                              Reading and Writing files to Internal Stoarage
                              Reading and Writing files to SD Card 
                           

                DataBase : Working With Database

                             Working With Database in Android
                             Creating Table In Android
                             Inserting, Deleting and Updating Records In Table in Android
                             How to Create DataBase in Android
                             Accessing Inbox In Android

     Animation In Android:

                  Animating A Button In Android
 

Android : Introduction

      Linux-based operating system

     Designed primarily for touchscreen mobile devices such as smartphones and tablet computers.

     Currently developed by Google.

•     Initially developed by Android Inc, whom Google financially backed and later purchased in 2005.

      The Android Software Development Kit (Android SDK) provides all necessary tools to develop Android applications. This includes a compiler, debugger and a device emulator, as well as its own virtual machine to run Android programs

Android is open source and Google releases the code under the Apache License

Android applications run in a sandbox, an isolated area of the system that does not have access to the rest of the system's resources, unless access permissions are explicitly granted by the user when the application is installed. Before installing an application, the Play Store displays all required permissions

The source code for Android is available under free and open source software licenses. Google publishes most of the code (including network and telephony stacks)[104] under the Apache License version 2.0  

The version history of the Android mobile operating system began with the release of the Android beta in November 2007. The first commercial version, Android 1.0, was released in September 2008


Below is List of Android Version and API Level

 
Version Code name Release date           API level
4.2     Jelly Bean November 13, 2012               17
4.1.x Jelly Bean July 9, 2012                      16
4.0.x Ice Cream Sandwich December 16, 2011       15
3.2 Honeycomb July 15, 2011       13
3.1 Honeycomb May 10, 2011       12
2.3.3–2.3.7 Gingerbread February 9, 2011      10
2.3–2.3.2 Gingerbread December 6, 2010       9
2.2 Froyo May 20, 2010        8
2.0–2.1 Eclair October 26, 2009       7
1.6 Donut September 15, 2009                4

Friday, January 25, 2013

Working With Menu: Creating Context Menu In Android

The Android Development Tutorials blog contains Basic as well as Advanced android tutorials.Go to Android Development Tutorials to get list of all Android Tutorials.


Context Menu 

A context menu provides actions that affect a specific item or context frame in the UI.
Context Menu appears when user long press on a View like ListView, GridView etc.

Learn  Creating Option Menu In Android
Android Custom Alert Dialog Example 

For Ex:  Below Is Context menu  registerd with List View
When A User long Press on a contacts he/she gets two option Call and SMS through Context Menu.





Some More Good  Android Topics
Customizing Toast In Android 
 Showing Toast for Longer Time
Customizing Checkboxes In Android  
Customizing Progress Bar  
To learn Basic of Android Animation  go to  Android Animation Basics



Creating a  context menu

To create a context menu:
  1. Register the View to which the context menu should be associated by calling registerForContextMenu() and pass it the View. Here we have used Context Menu with ListView

  2. Implement the onCreateContextMenu() method in your Activity When the registered view receives a long-click event, the system calls your onCreateContextMenu()method. This is where you define the menu items, usually by inflating a menu resource.

1:  Registering The View(here List View)  for Context menu.


We have to register the ListView for ContextMenu in onCreate  method


@Override
            public void onCreate(Bundle savedInstanceState)
            {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.show_contacts);

              
                    listViewSmartContacts=(ListView)findViewById(R.id.listSmartContacts);                    
                    contactListAdapter=new ContactListAdapter(this);
                    listViewSmartContacts.setAdapter(contactListAdapter);

       
                    // Register the ListView  for Context menu
                    registerForContextMenu(listViewSmartContacts);
       }

2: Implement the onCreateContextMenu()


When the registered view receives a long-click event, the system calls your onCreateContextMenu() method. 

             @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
            {
                    super.onCreateContextMenu(menu, v, menuInfo);
                    menu.setHeaderTitle("Select The Action"); 
                    menu.add(0, v.getId(), 0, "Call"); 
                    menu.add(0, v.getId(), 0, "Send SMS");
    
            } 


MenuInflater allows you to inflate the context menu from a menu resource 

3: Implement onContextItemSelected method



When the user selects a menu item, the system calls this method so you can perform the appropriate action.


             @Override 
            public boolean onContextItemSelected(MenuItem item)
            { 


                        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
                       
                // 
info.position will give the index of selected item
                           intIndexSelected=info.position              
                                if(item.getTitle()=="Call")
                                {
                
                                   // Code to execute when clicked on This Item

                                } 
                                else if(item.getTitle()=="Send SMS")
                                {
                                   

                                  // Code to execute when clicked on This Item                                                        } 
                                else
                                {

                                    return false;
                                } 
                                return true; 
                       
                                   

              } 
             



The Completye Code:


package com.mtracker;

import com.mtracker.R;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;


public class ShowSmartContactsActivity extends Activity
{
  
      
            ListView listViewSmartContacts;
            ContactListAdapter  contactListAdapter;
            //ArrayList<String> numberList;
           
            String number;
           
            public ShowSmartContactsActivity()
            {
                /// numberList=new ArrayList<String>();
           
            }
            @Override
            public void onCreate(Bundle savedInstanceState)
            {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.show_contacts);
       
                   
                    LinearLayout layoutContacts=(LinearLayout)findViewById(R.id.layoutSmartContacts);
                    int APILevel=android.os.Build.VERSION.SDK_INT ;
                    if(APILevel>=14)
                    {
                        layoutContacts.setBackgroundResource(R.color.Default);
                    }
                   
                    listViewSmartContacts=(ListView)findViewById(R.id.listSmartContacts);
                    //getList();
                    contactListAdapter=new ContactListAdapter(this);
                    listViewSmartContacts.setAdapter(contactListAdapter);
      
                                      
                   
                    registerForContextMenu(listViewSmartContacts);
            }
           
           
           
           
           
   
           
      
      
           
               
   
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
            {
                    super.onCreateContextMenu(menu, v, menuInfo);
                    menu.setHeaderTitle("Select The Action"); 
                    menu.add(0, v.getId(), 0, "Call"); 
                    menu.add(0, v.getId(), 0, "Send SMS");
    
            }

            @Override 
            public boolean onContextItemSelected(MenuItem item)
            { 


                        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
                        String number;
              
                        try
                        {
                                number=new ContactListAdapter(this).numberList.get(info.position);
                
               
                
                                if(item.getTitle()=="Call")
                                {
                
                
                                        Intent callIntent = new Intent(Intent.ACTION_CALL);
                                        callIntent.setData(Uri.parse("tel:"+number));
                                        startActivity(callIntent);
                
                
                
        

                                } 
                                else if(item.getTitle()=="Send SMS")
                                {
                                        Intent smsIntent = new Intent(Intent.ACTION_VIEW);
                                        smsIntent.setType("vnd.android-dir/mms-sms");
                                        smsIntent.putExtra("address", number);
                                        startActivity(smsIntent);
                    

                                } 
                                else
                                {return false;} 
                                return true; 
                        }
                        catch(Exception e)
                        {
                                return true;
                        }
            } 
           
           
    
}





 

New Advance Topics:
Android ImageSwitcher                    Android TextSwitcher                                Android ViewFlipper
Android Gesture Detector               Handling/Detecting Swap Events                Gradient Drawable
Detecting Missed Calls                    Hide Title Bar                                           GridView Animation

 Beginning With Android
      Android : Introduction                                                              Configuring Eclipse for Android Development
     Creating Your First Android Project                                           Understanding Android Manifest File of your android app

 Advance Android Topics                                                              Customizing Android Views


Working With Layouts                                                                Working With Views

Understanding Layouts in Android                                                   Using Buttons and EditText in Android
Working with Linear Layout (With Example)                                     Using CheckBoxes in Android
Nested Linear Layout (With Example)                                              Using AutoCompleteTextView in Android                                                                                          Grid View
Relative Layout In Android                                                               ListView
Table Layout                                                                                   Android ProgressBar
Frame Layout(With Example)                                                          Customizing ProgressBar
Absolute Layout                                                                             Customizing Radio Buttons
Grid Layout                                                                                    Customizing Checkboxes In Android

Android Components                                                                 Dialogs In Android

Activity In Android                                                                    Working With Alert Dialog
Activity Life Cycle                                                                    Adding Radio Buttons In Dialog
Starting Activity For Result                                                       Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android                    Creating Customized Dialogs in Android
Returning Result from Activity                                                   Creating Dialog To Collect User Input
Android : Service                                                                     DatePicker and TimePickerDialog
BroadcastReceiver                                                                   Using TimePickerDialog and DatePickerDialog In android

Menus In Android                                                                ListView:
Creating Option Menu                                                               Populating ListView With DataBase
Creating Context Menu In Android                                              Populating ListView with ArrayList
                                                                                               ListView with Custom Adapter

Toast                                                                                      Working With SMS
Customizing Toast In Android                                                       How to Send SMS in Android
Customizing the Display Time of Toast                                        How To Receive SMS
Customizing Toast At Runtime                                                  Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time


TelephonyManager                                                            Storage: Storing Data In Android
Using Telephony Manager In Android                                          SharedPreferences In Android
                                                                                              Reading and Writing files to Internal Stoarage
Working With Incoming Calls                                             DataBase
How To Handle Incoming Calls in Android                                Working With Database in Android
How to Forward an Incoming Call In Android                            Creating Table In Android
CALL States In Android                                                          Inserting, Deleting and Updating Records In Table in Android


Miscellaneous
Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog