Hello Friends!
3-MenuDemoActivity.java
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>
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>
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
how to open a new class when clicked on ADD button...nyways gr8 tutorial..keep posting!!
ReplyDeletebuilder.setAdapter(adapter, new DialogInterface.OnClickListener()
Delete{ 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!
thnks..got it!!
Deletethank you manish...i got the ans....works perfectly :-)
ReplyDeletei want to add image button with the list..........thank you in advance
ReplyDelete1)Create a layout with Name and Image according to your requirement
Delete2)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.
Thanks for Amaging Tutorial.
ReplyDeleteI want to pass data to Dialog Menu Item.
Please Help me.
What do mean? You want dynamic item instead of { "Add", "View", "Change","Delete" }; ?
DeleteSuppose Change "Add" to "Call" ; "View" to "SMS"
DeleteI want to add more button and pass Mobile Number for call
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
Deletepublic 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
}
How to receive mobile number using putExtra
Deletejust write-
DeleteString phone=getItntent().getStringExtra("phone");
Please Send Sample Code
DeleteSuppose, I have 5 TextView in XML Layout
DeleteWhen 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
Create a global variable on top of the activity and store clicked item value into that and send when you click next button.
DeleteCan I get Sample Source code
DeleteSorry 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...
DeletePlease tell me, where use below code-
DeleteString phone=getItntent().getStringExtra("phone");
excellent explanation
ReplyDeletedoubts
Adding two button and two dialog, one for Button1 and other for Button2 in same layout.xml ?