Wednesday, September 19, 2012

Menu Dialog Demo in Android

Hello Friends!
    This is a simple demo for create menu dialog box in android for this I am using Alert Dialog Builder.

1-Print Screen:



2-manifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.android.meenu"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 </manifest>

3-MenuDemoActivity.java

package com.manish.android.meenu;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;

public class MenuDemoActivity extends Activity {
       private Button button;

       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              final String[] option = new String[] { "Add", "View", "Change",
                           "Delete" };
              ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                           android.R.layout.select_dialog_item, option);
              AlertDialog.Builder builder = new AlertDialog.Builder(this);

              builder.setTitle("Select Option");
              builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

                     public void onClick(DialogInterface dialog, int which) {
                           // TODO Auto-generated method stub

                     }
              });
              final AlertDialog dialog = builder.create();

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

              button.setOnClickListener(new View.OnClickListener() {
                     public void onClick(View v) {
                           dialog.show();
                     }
              });
       }
}

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

   <Button
       android:id="@+id/buttonMeenu"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
       android:layout_marginTop="50sp"
       android:layout_gravity="center"
       android:text="Pess here!"/>


</LinearLayout>

Thanks,

 You might also like:
1)http://www.androidhub4you.com/2012/07/how-to-create-popup-window-in-android.html

2)http://www.androidhub4you.com/2012/09/alert-dialog-box-or-confirmation-box-in.html

19 comments:

  1. how to open a new class when clicked on ADD button...nyways gr8 tutorial..keep posting!!

    ReplyDelete
    Replies
    1. builder.setAdapter(adapter, new DialogInterface.OnClickListener()
      { public void onClick(DialogInterface dialog, int which)
      {
      // TODO Auto-generated method stub
      if(which==1){
      Intent intent=new Intent(A.this,ADD.class)
      startActivity(intent);
      }
      if(which==2){
      Intent intent=new Intent(A.this,Search.class)
      startActivity(intent);
      }
      });

      Please check above code it will help you..
      Note: I have not check so please it at your end and if which is start from "0" then change "1" to "0"..

      Thanks!

      Delete
  2. thank you manish...i got the ans....works perfectly :-)

    ReplyDelete
  3. i want to add image button with the list..........thank you in advance

    ReplyDelete
    Replies
    1. 1)Create a layout with Name and Image according to your requirement
      2)Change below array to List -
      final String[] option = new String[] { "Add", "View", "Change", "Delete" };
      3)And instead of simple String use Object of your Item-
      ArrayAdapter adapter = new ArrayAdapter(this,
      android.R.layout.select_dialog_item, option);

      4)Pass your custom row.xml instead of android.R.layout.select_dialog_item.

      Delete
  4. Thanks for Amaging Tutorial.
    I want to pass data to Dialog Menu Item.
    Please Help me.

    ReplyDelete
    Replies
    1. What do mean? You want dynamic item instead of { "Add", "View", "Change","Delete" }; ?

      Delete
    2. Suppose Change "Add" to "Call" ; "View" to "SMS"
      I want to add more button and pass Mobile Number for call

      Delete
    3. builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub

      if(which==0){
      makeCall();
      }
      else if(which==1){
      sendSMS();
      }
      else if(which==2){
      //dow what you want
      }
      }
      });

      public void makeCall(){
      //write here calling code
      }

      public void sendSMS(){
      //write here send sms code
      }

      Delete
    4. How to receive mobile number using putExtra

      Delete
    5. just write-
      String phone=getItntent().getStringExtra("phone");

      Delete
    6. Suppose, I have 5 TextView in XML Layout
      When I click "TextView1" pass Mobile Number 111111111111111 to Dialog Menu
      When I click "TextView2" pass Mobile Number 2222222222222 to Dialog Menu
      When I click "TextView3" pass Mobile Number 3333333333333 to Dialog Menu
      When I click "TextView4" pass Mobile Number 4444444444444 to Dialog Menu
      When I click "TextView5" pass Mobile Number 5555555555555 to Dialog Menu

      Delete
    7. Create a global variable on top of the activity and store clicked item value into that and send when you click next button.

      Delete
    8. Sorry we don't have sample code for your custom requirement. I want to suggest you " do some demo project before go with development". Like how to create global variable,how to pass value from one screen to another screen, how to create method, constructor, send sms, make call, activity life cycle, basic of android development etc...

      Delete
    9. Please tell me, where use below code-
      String phone=getItntent().getStringExtra("phone");

      Delete
  5. excellent explanation
    doubts
    Adding two button and two dialog, one for Button1 and other for Button2 in same layout.xml ?

    ReplyDelete