Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog change positive button color

enter image description here

I want change colour for positive button in Alertdialog, and here is what i'm doing below:

// style.xml

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:buttonBarPositiveButtonStyle">@style/positive</item>
</style>

<style name="positive">
    <item name="android:textColor">@color/accent</item>
</style>

I use the style by:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogCustom);

But what i'm doing is not working, i just want to change positive button colour. I don't want change the button's colour in java code.

Thanks in advance :-)

Edit-1 Alertdialog layout xml

<android.support.v7.internal.widget.ButtonBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/buttonPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="locale"
android:orientation="horizontal"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:gravity="bottom"
app:allowStacking="@bool/abc_allow_stacked_button_bar"
style="?attr/buttonBarStyle">

<Button
    android:id="@android:id/button3"
    style="?attr/buttonBarNeutralButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<android.support.v4.widget.Space
    android:id="@+id/spacer"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:visibility="invisible" />

<Button
    android:id="@android:id/button2"
    style="?attr/buttonBarNegativeButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<Button
    android:id="@android:id/button1"
    style="?attr/buttonBarPositiveButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

like image 607
Dennis Lu Avatar asked Oct 02 '16 05:10

Dennis Lu


2 Answers

Attention:The solution is not recommended, because this is actually a hacky solution. It's not following android design and shouldn't be used. And I change positive button color via java. Thanks to @Divers for pointing this.

styles.xml

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="buttonBarPositiveButtonStyle">@style/positive</item>
</style>

<style name="positive">
    <item name="android:textColor">@color/accent</item>
</style>

Usage

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogCustom);

Maybe here is one point you should be attention to, buttonBarPositiveButtonStyle not android: buttonBarPositiveButtonStyle.

like image 67
Dennis Lu Avatar answered Nov 03 '22 01:11

Dennis Lu


builder.setOnShowListener( new OnShowListener() {
  @Override
  public void onShow(DialogInterface arg0) {
       builder.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(COLOR_YOU_WANT);
  }
}

Hope it helps! Cheers!

like image 26
O_o Avatar answered Nov 02 '22 23:11

O_o