0

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 enter image description here 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>
Nemuga Dev
  • 334
  • 4
  • 10

2 Answers2

1

I tried your CODE, I am not getting that BUTTON SIZE issue :

PLEASE TRY TO get response by using interfaces: Hopefully this link will help you,

If getting any issue revert back to me, I'll explain.

How to create a Custom Dialog box in android?

enter image description here

Use below Color Codes:

and Change Background Colors to these:

<color name="purple">#FF6200EE</color>
<color name="teal">#FF018786</color>
<color name="black">#FF000000</color>

To Make Reusable Dialog:

Create a separate Interface Class called: DialogClickListener.java And add the following code

public interface DialogClickListener {
     public void onPositiveButtonClick();
     public void onNegativeButtonClick();
}

Create another Class Called ConfirmDialog.java that should implement the click listener:

public class ConfirmDialog extends Dialog implements
    android.view.View.OnClickListener {

}

Now In this Class Add the following Code:

 public class ConfirmDialog extends Dialog implements
    android.view.View.OnClickListener {

    public Activity activity;
    private DialogClickListener listener;
    public Button yes, no;

    public ConfirmDialog(Activity activity, DialogClickListener listener) {
        super(activity);
        this.activity = activity;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_confirm);
        yes = (Button) findViewById(R.id.btn_yes);
        no = (Button) findViewById(R.id.btn_no);

        yes.setOnClickListener(this);
        no.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_yes:
                listener.onPositiveButtonClick();
                break;
            case R.id.btn_no:
                listener.onNegativeButtonClick();
                break;
            default:
                break;
        }
        dismiss();
    }
}

Dialog Layout:

<?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:padding="40dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txt_dia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Please explain your action here"
        android:textColor="@color/theme_blue"
        android:textSize="18dp"
        android:textStyle="bold"/>


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:background="@color/white"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_yes"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:clickable="true"
            android:layout_marginEnd="10dp"
            android:text="@string/continue_text"
            android:textColor="@color/white"
            android:textStyle="normal" />

        <Button
            android:id="@+id/btn_no"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:background="@android:color/white"
            android:clickable="true"
            android:text="@string/cancel"
            android:textColor="@color/white"
            android:textStyle="normal" />
    </LinearLayout>

</LinearLayout>

Usage

Any Class where you want to use this Dialog, just implement this Listener that we created and implement the methods and simply declare dialog and show.

Sample Code

public class TryActivity extends AppCompatActivity implements DialogClickListener {

    LanguageConfirmDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_try);

        dialog = new LanguageConfirmDialog(TryActivity.this, this);
        dialog.show();
    }

    @Override
    public void onPositiveButtonClick() {
        Toast.makeText(this, "Yes Tapped", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNegativeButtonClick() {
        Toast.makeText(this, "Cancel Tapped", Toast.LENGTH_SHORT).show();
    }
}

Demo Images:

enter image description here

enter image description here

Done

Hafiza
  • 800
  • 8
  • 15
1
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    showDialog { isConfirmed: Boolean ->  
        if(isConfirmed){
            // write your logic 
        }else{
            // write your logic  
        }
    }
}

private fun showDialog(onConfirmedDialogClicked: (isConfirmed: Boolean) -> Unit) {
    val dialog = Dialog(this)
    dialog.setContentView(R.layout.dialog_layout)
    val buttonConfirm = dialog.findViewById<Button>(R.id.btnConfirm)
    val buttonCancel = dialog.findViewById<Button>(R.id.button_cancel)
    buttonCancel.setOnClickListener { 
        dialog.dismiss()
        onConfirmedDialogClicked.invoke(false)
    }
    buttonConfirm.setOnClickListener {
        dialog.dismiss()
        onConfirmedDialogClicked.invoke(true)
    }
    dialog.show()
}

}

Here is my simple code in Kotlin, your issue is the confirmAction function return false immediately before confirm button was clicked. You have to use interface instead.

Here is the java code version:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showDialog(new OnConfirmDialogClicked() {
        @Override
        public void onDialogButtonClicked(Boolean isConfirmed) {
            if(isConfirmed){
                // write your logic
            }else{
                // write your logic
            }
        }
    });
}

private void showDialog(OnConfirmDialogClicked onConfirmDialogClicked){
    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog_layout);
    Button btnConfirm = dialog.findViewById(R.id.btnConfirm);
    Button btnCancel = dialog.findViewById(R.id.btnCancel);
    btnConfirm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
            onConfirmDialogClicked.onDialogButtonClicked(true);
        }
    });
    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
            onConfirmDialogClicked.onDialogButtonClicked(false);
        }
    });
    dialog.show();
    
}
interface OnConfirmDialogClicked{
    void onDialogButtonClicked(Boolean isConfirmed);
}
    

}

Dean
  • 186
  • 5