Sunday 3 March 2013

Context 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" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="100dp"
        android:text="Show Context Menu" />

</LinearLayout>



2)context.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.contextmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ContextMenuActivity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Button btn = (Button) findViewById(R.id.button);

            registerForContextMenu(btn);
      }

      @Override
      public void onCreateContextMenu(ContextMenu menu, View v,
                  ContextMenuInfo menuInfo) {
            // TODO Auto-generated method stub
            getMenuInflater().inflate(R.menu.context, menu);
            super.onCreateContextMenu(menu, v, menuInfo);
      }

      @Override
      public boolean onContextItemSelected(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.onContextItemSelected(item);
      }
}


No comments:

Post a Comment