Monday, 16 July 2012

OPTIONS MENU IN ANDROID EXAMPLE


   OPTIONS MENU IN ANDROID EXAMPLE


In Android there are mainly 3 types of menus are acaiable


  •    options menu
  •    context menu
  •    popup menu
          following is the example for the options menu.in this first we want to create a menu folder in the res and inside the menu create a xml file



menuj.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="add" 
     android:icon="@drawable/bb"  >
     </item>
    <item 
    android:id="@+id/item2" 
    android:title="delete" 
    android:icon="@drawable/cc">
    </item>
    <item 
    android:id="@+id/item3" 
    android:title="exit" 
    android:icon="@drawable/aa">
    </item>
    
</menu>


OptionalmenuActivity.java



package a.b;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class OptionalmenuActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menuj, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.item1:
                add();
                return true;
            case R.id.item2:
                delete();
                return true;
            case R.id.item3:
                exit();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
private void exit() {
// TODO Auto-generated method stub
Toast.makeText(this, "jithu exit demo", Toast.LENGTH_SHORT).show();
}
private void add() {
Intent i=new Intent(this,abc.class);
startActivity(i);
// TODO Auto-generated method stub\
Toast.makeText(this, "function 2 called", Toast.LENGTH_SHORT).show();
}
private void delete() {
// TODO Auto-generated method stub
Toast.makeText(this, "jithu", Toast.LENGTH_SHORT).show();
}
}

then the menu is created.then write the xml file


main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>


main1.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <Button 
    android:text="Button" 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </Button>
    <Button 
    android:text="Button" 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></Button>
    <Button 
    android:text="Button" 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </Button>
    
</LinearLayout>












  

No comments:

Post a Comment