6

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" />

Edric
  • 24,639
  • 13
  • 81
  • 91
Dennis Lu
  • 762
  • 2
  • 9
  • 21
  • Isn't it answer your question - http://stackoverflow.com/a/31514677/632516 ? – Divers Oct 02 '16 at 05:32
  • @Divers No, i can't do like this, `colorAccent` will change both of negative and positive color – Dennis Lu Oct 02 '16 at 05:36
  • If you need change only one button, you should do it via java, because it's a hacky solution and not following design principals of android. How to do it via java - check my link above. – Divers Oct 02 '16 at 05:38
  • I do know how to change color via java. And yes, it is a tacky solution. But i still curious why i can't change it successfully even i will not use this solution finally. – Dennis Lu Oct 02 '16 at 05:42
  • I've answered why you can't do it via styles - it's not following android design and shouldn't be used. – Divers Oct 02 '16 at 05:43
  • Yeah you'r right bro. It it not the correct way. – Dennis Lu Oct 02 '16 at 05:46
  • You can not change it without using java – ziLk Oct 02 '16 at 06:01
  • @zilk Actually it seems can be changed via styles. [Destructive Dialogs](http://code.hootsuite.com/tips-and-tricks-for-android-material-support-library-2-electric-boogaloo/) – Dennis Lu Oct 02 '16 at 06:18
  • @L.Meng How can you get the positiveButton using XML to be able to change it using styles. – ziLk Oct 02 '16 at 06:31
  • @zilk You can see my Edit-1, source code of material dialog, you can find it in appcompatv7, positive button use an attr called buttonBarPositiveButtonStyle. – Dennis Lu Oct 02 '16 at 06:36
  • How do you change pretty **any** View's background image? By using a style or... (which is also used indirectly by styles) by using a `StateList Selector`. EASY. – Phantômaxx Oct 02 '16 at 09:24

3 Answers3

10

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.

Dennis Lu
  • 762
  • 2
  • 9
  • 21
5
builder.setOnShowListener( new OnShowListener() {
  @Override
  public void onShow(DialogInterface arg0) {
       builder.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(COLOR_YOU_WANT);
  }
}

Hope it helps! Cheers!

O_o
  • 1,103
  • 11
  • 36
  • "I don't want change the button's colour in java code." – Divers Oct 02 '16 at 05:58
  • @Divers, if you know, why don't you post the answer? :/ – O_o Oct 02 '16 at 06:07
  • Thank you anyway @ibtehaz , i know how to change single button's text color via java. And the method you posted is truly correct but sorry, it's not what i want. – Dennis Lu Oct 02 '16 at 06:15
  • @naXa, I guess you've already fixed this issue. I was on a vacation. Bt in case you're still stuck, please open a new question. Cause cannot resolve method setOnShowListener" may mean a lot a thing, starting from your gradle might need to resync, you need to restart your project.... – O_o Aug 26 '18 at 04:12
  • This should not say builder - it is confusing. OnShowListener() is a method of the AlertDialog not the builder. So you need to create the dialog with builder.create() then you will find that method. @O_o you are not creating helpful comments – d4c0d312 Aug 14 '19 at 14:24
  • @d4c0d312, I haven't done Android Development professionally in three years. When I wrote that answer, I am pretty sure I know what I was doing. However, Right now, I have no idea what you are saying. Maybe, we shouldn't take hints from an answer that is 3 years old, unless it's how to close VIM. @_@ – O_o Aug 25 '19 at 11:42
0

There is no way to do it via styles, because in general it's wrong from UI point of view. If you want to do that anyway, just do via java.

Divers
  • 9,531
  • 7
  • 45
  • 88
  • @RyanNewsom You didn't get me. I meant it is wrong because you break the standard behavior of the system. Although I understand that Google often just forgets to provide API. – Divers Jun 14 '18 at 08:07