Sunday 3 March 2013

Options Menu Demo



Demo:






1)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:background="#ffffff"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="100dp"
        android:text="Options Menu"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000000" />
</LinearLayout>

2)options.xml:(created in menu folder)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:id="@+id/item1" android:title="Option1"></item>
      <item android:id="@+id/item2" android:title="Option2"></item>
      <item android:id="@+id/item3" android:title="Option3"></item>
      <item android:id="@+id/item4" android:title="Option4"></item>
      <item android:id="@+id/item5" android:title="Option5"></item>
      <item android:id="@+id/item6" android:title="Option6"></item>
      <item android:id="@+id/item7" android:title="Option7"></item>
      <item android:id="@+id/item8" android:title="Option8"></item>
      <item android:id="@+id/item9" android:title="Option9"></item>
      <item android:id="@+id/item10" android:title="Option10"></item>
</menu>

3)Activity:
 
package com.venky.optionmenu;

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

public class OptionMenuActivity 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) {
            // TODO Auto-generated method stub
            getMenuInflater().inflate(R.menu.option, menu);
            return super.onCreateOptionsMenu(menu);
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
            // TODO Auto-generated method stub

            switch (item.getItemId()) {
            case R.id.item1:

                  Toast.makeText(getApplicationContext(), "You Clicked => Option 1",
                              Toast.LENGTH_SHORT).show();
                  break;

            case R.id.item2:

                  Toast.makeText(getApplicationContext(), "You Clicked => Option 2",
                              Toast.LENGTH_SHORT).show();
                  break;
            case R.id.item3:

                  Toast.makeText(getApplicationContext(), "You Clicked => Option 3",
                              Toast.LENGTH_SHORT).show();
                  break;
            case R.id.item4:

                  Toast.makeText(getApplicationContext(), "You Clicked => Option 4",
                              Toast.LENGTH_SHORT).show();
                  break;
            case R.id.item5:

                  Toast.makeText(getApplicationContext(), "You Clicked => Option 5",
                              Toast.LENGTH_SHORT).show();
                  break;
            case R.id.item6:

                  Toast.makeText(getApplicationContext(), "You Clicked => Option 6",
                              Toast.LENGTH_SHORT).show();
                  break;
            case R.id.item7:

                  Toast.makeText(getApplicationContext(), "You Clicked => Option 7",
                              Toast.LENGTH_SHORT).show();
                  break;
            case R.id.item8:

                  Toast.makeText(getApplicationContext(), "You Clicked => Option 8",
                              Toast.LENGTH_SHORT).show();
                  break;
            case R.id.item9:

                  Toast.makeText(getApplicationContext(), "You Clicked => Option 9",
                              Toast.LENGTH_SHORT).show();
                  break;
            case R.id.item10:

                  Toast.makeText(getApplicationContext(), "You Clicked => Option 10",
                              Toast.LENGTH_SHORT).show();
                  break;
            }
            return super.onOptionsItemSelected(item);
      }
}


4)Download this Project Click Here
  

No comments:

Post a Comment