• 12
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Missing buttons on AlertDialog | Android 7.0 (Nexus 5x)

I am trying to create an AlertDialog but the buttons are not showing. Only seeing this issue in Android 7.0:

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This app needs location access");
builder.setMessage("Please grant location access so this app can detect beacons.");
builder.setPositiveButton(android.R.string.ok, null);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    @TargetApi(Build.VERSION_CODES.M)
    public void onDismiss(final DialogInterface dialog) {
        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
    }
});
builder.show();

AlertDialog

Indeed it seems that AlertDialog theme needs to be defined. An alternative approach to above would be to define AlertDialog theme in Application theme:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- ... other AppTheme items ... -->
    <item name="android:alertDialogTheme">@style/AlertDialogTheme</item>
</style>

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

Then it is enough create AlertDialog.Builder only with Context parameter.

Note: The above seems to work only for android.app.AlertDialog.Builder and is not working for AppCompat builder (android.support.v7.app.AlertDialog.Builder, at least as of version 25.0.1). In case of AppCompat builder, I had to pass theme ID as second parameter to Builder constructor to have buttons visible.

  • 34
Reply Report
      • 1
    • You just need to use @style/AlertDialogTheme</item> (without android:) to work for android.support.v7.app.AlertDialog.Builder using just the context parameter

So it turns out on Android 7.0 you have to provide a theme. At least, that's what I had to do.

    <style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="borderlessButtonStyle">@style/Widget.AppCompat.Button.Borderless.Colored</item>
    </style>


    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme);
  • 18
Reply Report

What worked for me was in styles.xml:

<style name="LightDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textColor">@android:color/primary_text_light</item>
    <item name="colorAccent">#007fff</item>
    <item name="buttonBarButtonStyle">@style/DialogButtonStyle</item>   
</style>

and

<style name="DialogButtonStyle" parent="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">#007fff</item>                   
</style>

and in your program:

final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity(), R.style.LightDialogTheme);
  • 11
Reply Report

You can create a custom theme for Alert Dialog, and set alertDialogTheme in your app theme.

<!--Alert Dialog Theme -->
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">

    <item name="android:textColor">@color/colorPrimary</item>
    <item name="buttonBarButtonStyle">@style/DialogButtonStyle</item>
    <item name="colorAccent">@color/colorAccent</item>

    <!--If minimum API level is greater than or equal to 23, you can define the color of Title text separately  -->
    <item name="android:titleTextColor">@SomeColor</item> 

</style>

<!--This is to style the buttons of alert dialog-->
<style name="DialogButtonStyle" parent="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <item name="android:textColor">@color/colorAccent</item>
</style>

and finally, set the custom created theme to alertDialogTheme in Application Theme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--To make the change global to application-->
    <item name="alertDialogTheme">@style/AlertDialogTheme</item>
</style>

Tested for android.support.v7.app.AlertDialog

  • 5
Reply Report

I had a similar issue and the thing was that I wasn't using the support library for my AppCompatActivity, therefore I changed:

import android.app.AlertDialog;

to

import android.support.v7.app.AlertDialog;

and it worked.

  • 4
Reply Report

Sorry for late answer, Its about import problem. you need to select v7.alertDialog, not use app.alertDialog.

Please change your code to v7.alertDialog and you will show button color in nexus as well.

enter image description here

  • 0
Reply Report

Maybe its too late, but I hope someone would use this solution. you can do it something like this: You should set onShowListenter to your alertDialog, inside this function you should getButton() and than setTextColor to it. An example:

alertDialog = alertDialogBuilder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){
    @Override
    public void onShow(DialogInterface dialogInterface){
        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(R.color.black);
    }
});
  • -2
Reply Report