How can I create an alertDialog
which consists of a list of items defined in the code. I want the alert similar to this:
I have seen this but still I'm not able to create the alert.
How can I create an alertDialog
which consists of a list of items defined in the code. I want the alert similar to this:
I have seen this but still I'm not able to create the alert.
Try this code. i think this will definitely helps you
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Pick Colour");
dialog.setItems(R.array.colors,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"selected Item:"+position, Toast.LENGTH_SHORT).show();
}
});
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
AlertDialog alert = dialog.create();
alert.show();
}
}
let me know if you have any doubts.