Example 1:
You can see in this tutorial:
Android Lesson 12 part 2: ListFragment
AlertDialog.Builder adb= new AlertDialog.Builder(getActivity()); adb.setTitle("Delete?"); adb.setMessage("Are you sure you want to delete " + cities.get(position)); adb.setNegativeButton("Cancel", null); adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { cities.remove(position); adapter.notifyDataSetChanged(); }}); adb.show();
Example 2: create android dialog with a custom layout
File res/layout/dialog_content.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/sim1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="Sim 1" /> <Button android:id="@+id/sim2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@id/sim1" android:text="Sim 2" /> </RelativeLayout>
Java:
AlertDialog.Builder adb= new AlertDialog.Builder(getActivity()); adb.setTitle("Choose SIM"); View view1 = getActivity().getLayoutInflater().inflate(R.layout.dialog_content, null); view1.findViewById(R.id.sim1).setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Toast.makeText(getActivity(),"Sim 1", Toast.LENGTH_SHORT).show(); } }); view1.findViewById(R.id.sim2).setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Toast.makeText(getActivity(),"Sim 2", Toast.LENGTH_SHORT).show(); } }); adb.setView(view1); adb.setNeutralButton("Close", new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); }}); adb.show();