I am learning android and I need to create a global custom android confirm dialog to be used across the entire app but does not work. First the UI of the application looks like this image
The buttons looks small and dont know what am doing wrong.
Here is my layout xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="25dip"
android:layout_marginLeft="25dip"
android:background="@drawable/alert_bg">
<TextView
android:textColor="@color/colorStandardBlack"
android:gravity="center"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:textAllCaps="true"
android:textStyle="bold"
android:id="@+id/text_view_confirm"
android:text="@string/confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:id="@+id/liner_confirm"
android:background="@color/chrome_bg"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<TextView
android:textColor="@color/colorStandardBlack"
android:gravity="center"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:id="@+id/text_view_confirm_message"
android:text="Lorem ipskljsdf kfjsda fkdlsja fs fdsfdsfsdfsdfsdf fsfsdfsdffdf sfsdf fksjffska fdjslkfjsdlfjslfjsdkl fksdfjklsdjfklsd"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="100dp">
<Button
style="@style/Button_With_Radius"
android:background="#fdcb6e"
android:textColor="@color/white"
android:layout_width="120dp"
android:text="@string/cancel"
android:id="@+id/button_cancel"
android:textAllCaps="true"
android:layout_height="wrap_content" />
<Button
style="@style/Button_With_Radius"
android:background="#0984e3"
android:layout_marginLeft="35dp"
android:textColor="@color/white"
android:text="@string/confirm"
android:textAllCaps="true"
android:id="@+id/button_confirm"
android:layout_width="120dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
and here is the style for the buttons
<style name="Button_With_Radius">
<item name="android:textColor">@color/colorPrimary</item>
<item name="android:textStyle">bold</item>
<item name="android:textAllCaps">false</item>
<item name="android:background">@drawable/primary_round_with_radius</item>
</style>
here is primary_round_with_radius.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white"/>
<corners android:radius="5dp" />
<stroke android:width="1px" android:color="@color/colorPrimary" />
</shape>
Now on the other end of java the function returns false all times even if I click confirm. Here is my java code.
static boolean yes_no;
public static boolean confirmAction(final Context context,String message)
{
yes_no=false;//if declared here it requesting to be final but final cannot be intialized in a inner class
final Dialog dialogView=getCustomDialog(context,R.layout.custom_confirm_dialog);
dialogView.show();
Button confirm=dialogView.findViewById(R.id.button_confirm);
Button cancel=dialogView.findViewById(R.id.button_cancel);
TextView textView=dialogView.findViewById(R.id.text_view_confirm_message);
textView.setText(message);
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
yes_no=true;
dialogView.dismiss();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
yes_no=false;
dialogView.dismiss();
}
});
return yes_no;
}
public static Dialog getCustomDialog(Context context, int layout_id)
{
Dialog dialog= new Dialog(context, android.R.style.Theme_Dialog);
if ( dialog!=null && dialog.isShowing() ){
dialog.cancel();
}
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(layout_id);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
return dialog;
}
and here is how am trying to use my custom dialog
boolean yes=AppUtil.confirmAction(this,"Are you sure you wish to do this?"); Toast.makeText(this,""+yes,Toast.LENGTH_SHORT).show();//this always return false and here is alert bg xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#fff" />
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
</shape>