Here with Below Code for Use in Eclipse with Android SDK ,you can create Simple Demo for Custom Alert Dialog With ListView in it .
First Things in for Use of java Code files is Below:
In Activity 's OnCreate Method Add Following Code .
mButton = (Button) findViewById(R.id.btnalert);
mButton_Sample=(Button)findViewById(R.id.simpleid);
mListview = new ListView(this);
values = new String[] { "ListItem1", "ListItem2", "ListItem3",
"ListItem4", "ListItem5", "ListItem6", "ListItem7",
"ListItem8", "ListItem9", "ListItem10" };
//Adapter Use in ListView
adpter = new ArrayAdapter<String>(this,R.layout.simplelist_item_text, values);
mListview.setOnItemClickListener(this);
mListview.setAdapter(adpter);
prepareAlertDialog();
mButton_Sample.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SimpleAlertDialog();
}
});
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mAlertDialog.show();
}
});
And Now After OnCreate Method brace Complete Add Following Method For Create Alert Dialog
This Method Create AlertDialog With ListView in it.
public void prepareAlertDialog() {
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
AlertDialog.Builder mBuider = new AlertDialog.Builder(
CustomAlertDialogActivity.this);
mBuider.setTitle("ListItems");
mBuider.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do what you like on Ok Button
}
});
mBuider.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
mBuider.setView(mListview);
mAlertDialog = mBuider.create();
lp.copyFrom(mAlertDialog.getWindow().getAttributes());
}
//This Method For Create Simple AlertDialog In android
public void SimpleAlertDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CustomAlertDialogActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
And Finally Don't Forget To Activity to implements OnItemClickListener for Override this method used for Listview item.
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long log) {
// Do what you want on List Item Click
TextView mTextView;
mTextView = (TextView) view;
String txtstr;
txtstr = mTextView.getText().toString();
Toast.makeText(CustomAlertDialogActivity.this, "Your Select Item is "+ txtstr, Toast.LENGTH_SHORT).show();
mAlertDialog.cancel();
}
in Your Class File Add Following Class member on Top
ListView mListview;
Button mButton,mButton_Sample;
AlertDialog mAlertDialog;
String[] values;
ArrayAdapter<String> adpter;
Here are Some Used Layout Files in Demo.
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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:text="OpenAlert"
android:id="@+id/btnalert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/simpleid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SimpleDialog"
/>
</LinearLayout>
<--------------------------------------- ----------------------------------------------->
simplelist_item_text.xml
You can use this xml for text context in ListView to change Color,textsize,font etc
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textColor="#0000FF"
/>
First Things in for Use of java Code files is Below:
In Activity 's OnCreate Method Add Following Code .
mButton = (Button) findViewById(R.id.btnalert);
mButton_Sample=(Button)findViewById(R.id.simpleid);
mListview = new ListView(this);
values = new String[] { "ListItem1", "ListItem2", "ListItem3",
"ListItem4", "ListItem5", "ListItem6", "ListItem7",
"ListItem8", "ListItem9", "ListItem10" };
//Adapter Use in ListView
adpter = new ArrayAdapter<String>(this,R.layout.simplelist_item_text, values);
mListview.setOnItemClickListener(this);
mListview.setAdapter(adpter);
prepareAlertDialog();
mButton_Sample.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SimpleAlertDialog();
}
});
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mAlertDialog.show();
}
});
And Now After OnCreate Method brace Complete Add Following Method For Create Alert Dialog
This Method Create AlertDialog With ListView in it.
public void prepareAlertDialog() {
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
AlertDialog.Builder mBuider = new AlertDialog.Builder(
CustomAlertDialogActivity.this);
mBuider.setTitle("ListItems");
mBuider.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do what you like on Ok Button
}
});
mBuider.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
mBuider.setView(mListview);
mAlertDialog = mBuider.create();
lp.copyFrom(mAlertDialog.getWindow().getAttributes());
}
//This Method For Create Simple AlertDialog In android
public void SimpleAlertDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CustomAlertDialogActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
And Finally Don't Forget To Activity to implements OnItemClickListener for Override this method used for Listview item.
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long log) {
// Do what you want on List Item Click
TextView mTextView;
mTextView = (TextView) view;
String txtstr;
txtstr = mTextView.getText().toString();
Toast.makeText(CustomAlertDialogActivity.this, "Your Select Item is "+ txtstr, Toast.LENGTH_SHORT).show();
mAlertDialog.cancel();
}
in Your Class File Add Following Class member on Top
ListView mListview;
Button mButton,mButton_Sample;
AlertDialog mAlertDialog;
String[] values;
ArrayAdapter<String> adpter;
Here are Some Used Layout Files in Demo.
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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:text="OpenAlert"
android:id="@+id/btnalert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/simpleid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SimpleDialog"
/>
</LinearLayout>
<--------------------------------------- ----------------------------------------------->
simplelist_item_text.xml
You can use this xml for text context in ListView to change Color,textsize,font etc
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textColor="#0000FF"
/>
What amaze me the most is that almost all tutorial every where made no references to libraries. Where are those functions, classes, objects just magically appear out of thin air.
ReplyDeleteHey you did not mention about the CustomAlertDialogActivity . What should that Activity contain
ReplyDeleteThanks Himanshu. Your code actually worked perfectly!
ReplyDeleteHow can you do so with images and text together?
ReplyDeleteI need to show a EditText + a Listview in ay dialog. Is this possible. Now i m able to see just the EditText. I did: Listview = findViewById...
ReplyDelete& listview.setAdapater
& builder.setview(my layout)
AM i missing anything ?
nice blog but please attached screenshot that make great.
ReplyDeleteMost confusing piece of code ever. Try to get your organization together before posting nonsense mate.
ReplyDelete