Android AlertDialog with setMultiChoiceItems Example
MainActivity.java
package com.bipin.rupadiya.alertdialogdemo;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
final CharSequence myList[] = { "Tea", "Coffee", "Milk" };
ArrayList<Integer> selList=new ArrayList();
boolean bl[] = new boolean[myList.length];
RelativeLayout rl;
String msg ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl = (RelativeLayout) findViewById(R.id.myRL);
final Button btn = (Button) findViewById(R.id.btnDialog);
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setTitle("What do you Like ?");
ad.setMultiChoiceItems(myList,bl, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
if(arg2)
{
// If user select a item then add it in selected items
selList.add(arg1);
}
else if (selList.contains(arg1))
{
// if the item is already selected then remove it
selList.remove(Integer.valueOf(arg1));
}
}
});
ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
msg="";
for (int i = 0; i < selList.size(); i++) {
msg=msg+"\n"+(i+1)+" : "+myList[selList.get(i)];
}
Toast.makeText(getApplicationContext(),
"Total "+ selList.size() +" Items Selected.\n"+ msg , Toast.LENGTH_LONG)
.show();
}
});
ad.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"You Have Cancel the Dialog box", Toast.LENGTH_LONG)
.show();
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
msg="";
ad.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
final CharSequence myList[] = { "Tea", "Coffee", "Milk" };
ArrayList<Integer> selList=new ArrayList();
boolean bl[] = new boolean[myList.length];
RelativeLayout rl;
String msg ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl = (RelativeLayout) findViewById(R.id.myRL);
final Button btn = (Button) findViewById(R.id.btnDialog);
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setTitle("What do you Like ?");
ad.setMultiChoiceItems(myList,bl, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
if(arg2)
{
// If user select a item then add it in selected items
selList.add(arg1);
}
else if (selList.contains(arg1))
{
// if the item is already selected then remove it
selList.remove(Integer.valueOf(arg1));
}
}
});
ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
msg="";
for (int i = 0; i < selList.size(); i++) {
msg=msg+"\n"+(i+1)+" : "+myList[selList.get(i)];
}
Toast.makeText(getApplicationContext(),
"Total "+ selList.size() +" Items Selected.\n"+ msg , Toast.LENGTH_LONG)
.show();
}
});
ad.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
"You Have Cancel the Dialog box", Toast.LENGTH_LONG)
.show();
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
msg="";
ad.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:text="@+id/myRL" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/btnDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="172dp"
android:text="Show Dialog" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:text="@+id/myRL" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/btnDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="172dp"
android:text="Show Dialog" />
</RelativeLayout>
Labels:
Android,
Mobile Computing
Subscribe to:
Post Comments (Atom)
Subjects
- WordPress
- Mobile Computing-4649303 Practical Solution
- Android Programming New Syllabus Theory
- PHP LAMP Question Bank
- PHP LAMP Theory
- Step by Step Android Example
- Android Practical
- Android Theory
- Android Question Bank
- Networking FON Practical
- Networking FON Theory
- OS Practical
- OS Theory
- HTML
- JavaScript
- J2EE WTAD Theory
- J2EE WTAD Question Bank
- J2EE WTAD Quick Guide
- J2EE WTAD GTU Papers
- J2EE WTAD Practical
- Python
- JAVA Theory
- JAVA Practical
- MIS
Categories
- Android (55)
- c (11)
- Configure Tomcat7 (2)
- CSS (3)
- Decryption (16)
- Difference (1)
- Encryption (16)
- Error Detection and Correction Techniques (3)
- FON (27)
- Framing Technic (2)
- install Tomcat (2)
- J2EE (29)
- JAVA (13)
- JavaScript (19)
- linux (8)
- OS (17)
- PHP (11)
- Protocol (3)
- SERVER SOCKET PROGRAMING (7)
- Servlet (13)
- shell script (33)
- unix (22)
- WTAD (34)
Blog Archive
-
▼
2013
(64)
-
▼
August
(17)
- Android AlertDialog with setMultiChoiceItems Example
- Android ProgressDialog Example
- Android AlertDialog setSingleChoiceItems Example
- Android AlertDialog List Example
- Android AlertDialog Example
- Android TimePickerDialog Example
- Android DatePickerDialog Example
- Android Button onClick Example that implements on...
- Android Button Example with onClickListener
- Android TimePicker Example with setOnTimeChangedLi...
- Android DatePicker example with onDateChanged list...
- An Android RadioGroup Example Programmatically
- Android RadioGroup Example using setOnCheckedChang...
- Android Checkbox Example
- android Checkbox setOnClickListener Example
- Android Checkbox setOnCheckedChangeListener Example
- command line arguments in linux shell script
-
▼
August
(17)
Total Pageviews
© BipinRupadiya.com. Powered by Blogger.
1 comments:
i have long list item and dialog is not scrollable, please suggest!!
Post a Comment