Monday, May 21, 2012

MENU BAR in android


12.MENU BAR

12.1 MENU BASICS

Menus are an important part of an activity's user interface, which provide users a familiar way to perform actions. Android offers a simple framework for you to add standard menus to your application.
There are three types of application menus:
Options Menu
The primary collection of menu items for an activity, which appears when the user touches the MENU button. When your application is running on Android 3.0 or later, you can provide quick access to select menu items by placing them directly in the Action Bar, as "action items."
Context Menu
A floating list of menu items that appears when the user touches and holds a view that's registered to provide a context menu.
Submenu
A floating list of menu items that appears when the user touches a menu item that contains a nested menu.
This document shows you how to create each type of menu, using XML to define the content of the menu and callback methods in your activity to respond when the user selects an item.

12.2 CREATING A MENU RESOURCE

            Instead of instantiating a Menu in your application code, you should define a menu and all its items in an XML menu resource, then inflate the menu resource (load it as a programmable object) in your application code. Using a menu resource to define your menu is a good practice because it separates the content for the menu from your application code. It's also easier to visualize the structure and content of a menu in XML.
To create a menu resource, create an XML file inside your project's res/menu/ directory and build the menu with the following elements:
<menu>
Defines a Menu, which is a container for menu items. A <menu> element must be the root node for the file and can hold one or more <item> and <group> elements.
<item>
Creates a MenuItem, which represents a single item in a menu. This element may contain a nested <menu> element in order to create a submenu.
<group>
An optional, invisible container for <item> elements. It allows you to categorize menu items so they share properties such as active state and visibility. See the section about Menu groups.
Here's an example menu named game_menu.xml:
<?xml version="1.0" encoding="UTF-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/item1"
            android:title="Signout"
            />
    </menu>
Now it’s time for program. In our program we sign in and reach second activity where we are provided with some buttons for specific actions from there by clicking the menu button we could sign out. Lets see our first activity
package a.b.c;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class FirstActivity extends Activity {
      TextView t1,t2,t3;
      EditText e1,e2;
      String us1="suja", us2= "shil";
      String ps1 = "123", ps2="456";
      String s1,s2;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        t1 = (TextView)findViewById(R.id.tv1);
        e1 = (EditText)findViewById(R.id.ed1);
       
        t2 = (TextView)findViewById(R.id.tv2);
        e2 = (EditText)findViewById(R.id.ed2);
       
    }
    public void Submit(View v)
    {
      s1=  e1.getText().toString();
      s2 = e2.getText().toString();
      if(s1.equals(us1))        
      {
        if(s2.equals(ps1))
        {
        Intent i = new Intent(FirstActivity.this, second.class);
          startActivity(i);
        }
        else
          {
              t3 = (TextView)findViewById(R.id.tv3);
              t3.setText("Sorry wrong username/password");
          }
      }
      if(s1.equals(us2))        
      {
        if(s2.equals(ps2))
        {
           Intent i = new Intent(FirstActivity.this, second.class);
             startActivity(i);
        }
        else
          {
              t3 = (TextView)findViewById(R.id.tv3);
              t3.setText("Sorry wrong username/password");
          }
      }
     
    }
    public void Cancel(View v)
    {
      e1.setText(" ");
      e2.setText(" ");
      t3 = (TextView)findViewById(R.id.tv3);
          t3.setText(" ");
    }
}
Its main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/androidvsapple"
    android:layout_marginTop="20dip">

     <TextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Welcome"    
     android:textSize="25dip"
     android:gravity="center_horizontal"
     android:textColor="#000000"
     />
    
     <!-- User name -->
     <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:layout_marginTop="40dip"
     >
     <TextView
      android:id="@+id/tv1"
      android:text="  Username"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textSize="15dip"
      android:textColor="#000000"
      />  
      <EditText
      android:id="@+id/ed1"
      android:layout_width="138dip"
      android:layout_height="wrap_content"
      android:layout_marginLeft="19dip"
      />        
     </LinearLayout>
    
     <!--Password  -->
     <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:layout_marginTop="20dip"
     >
     <TextView
      android:id="@+id/tv2"
      android:text="  Password "
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textSize="15dip"
      android:textColor="#000000"
      />  
      <EditText
      android:id="@+id/ed2"
      android:layout_width="140dip"
      android:layout_height="wrap_content"
      android:layout_marginLeft="19dip"
      />        
     </LinearLayout >
     
     <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:layout_marginTop="20dip">
     <Button
      android:id="@+id/bttn1"
      android:text="Submit"     
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="40dip"
      android:onClick="Submit"
      />
     <Button
         android:id="@+id/bttn2"
         android:text="Cancel"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginLeft="50dip"
         android:onClick="Cancel" />        
      </LinearLayout>
      <TextView
          android:id="@+id/tv3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="#000000"
          />
</LinearLayout>
Now second.java
package a.b.c;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class second extends Activity {
      Button b2,b3;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
       
             
        b2 = (Button)findViewById(R.id.bttn2);
        b2.setOnClickListener(new OnClickListener() {
                 
                  public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms:5556"));
                        startActivity(i);
                       
                  }
            });
       
        b3 = (Button)findViewById(R.id.bttn3);
        b3.setOnClickListener(new OnClickListener() {
                 
                  public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
                        startActivity(i);
                       
                  }
            });
       
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      // TODO Auto-generated method stub
      MenuInflater menuinflater = getMenuInflater();
      menuinflater.inflate(R.menu.menu, menu);
      return true;
    }
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // TODO Auto-generated method stub
        
         switch(item.getItemId())
         {
         case R.id.item1:
               Intent i = new Intent(second.this,FirstActivity.class);
               Toast.makeText(getApplicationContext(), "You have successfully signed out", Toast.LENGTH_SHORT).show();
               startActivity(i);
               return true;
         case R.id.call:
               Intent intent = new Intent(Intent.ACTION_DIAL);
                  startActivity(intent);
                  return true;
        
         default :
               return super.onOptionsItemSelected(item);
         }
    }
  
  
   
}
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/androidvsapple" >
  
    <TextView
        android:text="Welcome to second activity"
        android:textColor="#000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dip"
        android:layout_marginTop="30dip"      
        />
    <TextView
        android:text="Click menu button to signout"
        android:textColor="#000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/bttn2"
        android:text="   sms  "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/bttn3"
        android:text="   internet  "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
manifestfile will look like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="a.b.c"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".FirstActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".second"></activity>
    </application>
    <uses-permission android:name="android.permission.CALL_PHONE"/>

</manifest>

            From our application code, we are inflating a menu resource (convert the XML resource into a programmable object) using MenuInflater.inflate(). For example, the following code(second.java) inflates the menu.xml file defined above, during the onCreateOptionsMenu() callback method, to use the menu as the activity's Options Menu. The getMenuInflater() method returns a MenuInflater for the activity. With this object, you can call inflate(), which inflates a menu resource into a Menu object.When we click on any of items in our menu then the onOptionsItemSelected is called. It checks each item by it it by getting id using item.getItemId() method. Correspoding o each item in menu an action is written, which is carried out while pressing on it.

No comments:

Post a Comment