Often in apps, we need to popup a dialog that will show a message and take a Yes or No in response from the user. Rather than make different dialogs for various situations, it is better to create a generic dialog which can be used in multiple situations.
Below we create a layout xml for a dialog and then we call a Dialog object in code, within any Activity, where we need to get a Yes/No response from the user.
Dialog Layout
<?xml version="1.0" encoding="utf-8"?> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:paddingTop="20dp" android:paddingBottom="20dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:id="@+id/linearLayout"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="10dp"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.0" android:textAlignment="center" android:id="@+id/textDialogYesNoMessage" android:text="Dialog message will come here" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> <Button style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Yes" android:id="@+id/btnDialogYes" android:textColor="@color/white" android:layout_weight="0.20" /> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0.45" /> <Button style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="No" android:id="@+id/btnDialogNo" android:textColor="@color/white" android:layout_weight="0.20" /> </LinearLayout> </LinearLayout> </RelativeLayout>
In this dialog display, the TextView can be changed to show whatever text we want to show. We can add listeners to the Yes and No buttons and then take appropriate action. Below is an example case where a Payment entry has to be deleted. Likewise, for other situations, you can customize what you want to show in the Dialog and how you want to handle the Dialog buttons.
This is a basic Dialog with just a TextView and two Buttons. You can add more components to the layout as required and handle them in code as required.
Calling Dialog in Code
public void deletePayment(int id) { // custom dialog final Dialog dialog = new Dialog(this); dialog.setContentView(R.layout.dialog_yesno); dialog.setTitle("Delete Payment"); // set the custom dialog components - TextView text = (TextView) dialog.findViewById(R.id.textDialogYesNoMessage); text.setText("This will delete payment for Invoice #212"); Button btnYes = (Button) dialog.findViewById(R.id.btnDialogYes); // if button is clicked, close the custom dialog btnYes.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); // do some process here to handle deletion } }); Button btnNo = (Button) dialog.findViewById(R.id.btnDialogNo); btnNo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); }
Screenshots
This is how the dialog layout looks in Android Studio.
This is how the Delete Payment dialog looks.
Leave a Reply