Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog do not show positive and negative button

Tags:

android

dialog

I used AlertDialog to alert user confirm delete. I check on my device (Android 5.1) and it show well

enter image description here

But on some another device (also run Android 5.1), the dialog missed positive and negative button.

enter image description here

I checked and found that devices happen this issue have a medium resolution (960x540, 854x480).

Is resolution relate with this issue ? If not, can you tell me the reason and how to fix this issue ?

My code for display dialog:

    public static final Dialog yesNoDialog(Context context,                                                String message,                                                DialogInterface.OnClickListener yesAction, DialogInterface.OnClickListener noAction) {               AlertDialog.Builder  builder = new AlertDialog.Builder(context,R.style.todoDialogLight);              builder.setTitle(context.getString(R.string.app_name))                     .setMessage(message)                     .setCancelable(false)                     .setPositiveButton("YES", yesAction)                     .setNegativeButton("NO", noAction);             return builder.create();  } 

And styles.xml

  <style name="todoDialogLight" parent="Theme.AppCompat.Light.Dialog">              <!-- Used for the buttons -->             <item name="colorAccent">@color/colorPrimaryDark</item>             <item name="android:textStyle">bold</item>             <!-- Used for the title and text -->             <item name="android:textColorPrimary">@color/colorText</item>             <!-- Used for the background -->             <!-- <item name="android:background">#4CAF50</item>-->             <item name="android:fontFamily">sans-serif</item>             <item      name="android:windowAnimationStyle">@style/RemindDialogAnimation</item>             <item name="android:layout_width">@dimen/width_remind_dialog</item>             <item name="android:layout_height">wrap_content</item>  </style> 
like image 915
CauCuKien Avatar asked Sep 14 '16 02:09

CauCuKien


People also ask

How can I customize AlertDialog in Android?

Step 1: Create a XML file: custom_layout. Add the below code in custom_layout. xml. This code defines the alertdialog box dimensions and add a edittext in it.

Is AlertDialog deprecated?

A simple dialog containing an DatePicker . This class was deprecated in API level 26.

How many buttons can be set up on an AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

How do I get view from AlertDialog?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

So the buttons are there for me. Unfortunately, they were white text on white background. It has nothing to do with the resolution but more to do with the theme you are choosing. To solve this you need to set the right text color in your dialog theme.

For example, in styles.xml add

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert"> <item name="colorPrimary">@color/colorPrimaryDarkBlue</item> </style> 

and in your activity add

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MyActivity.this, R.style.MyDialogTheme); 

Hope this helps.

like image 199
Ali Avatar answered Sep 23 '22 17:09

Ali


@Ali answer is correct but doesn't work with the new Design Library

To use it with the design library, use this style.

<style name="AlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">     <item name="colorPrimary">@color/colorAccent</item> </style> 

Then you use it like this.

 AlertDialog.Builder(it, R.style.AlertDialogTheme)                     .setMessage("The message")                     .setPositiveButton("Yes") { _, _ -> /* Do something*/}                     .setNegativeButton("No") { _, _ -> }                     .show() 
like image 44
Aryeetey Solomon Aryeetey Avatar answered Sep 23 '22 17:09

Aryeetey Solomon Aryeetey