1

I have this button, style and color states file:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button1"
    style="@style/btnGreen"/>
<style name="btnGreen" parent="Widget.MaterialComponents.Button">
    <item name="android:textStyle">bold</item>
    <item name="backgroundTint">@color/color_states_btn_green</item>
</style>

Color states: color_states_btn_green.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#00FF00" android:state_enabled="true" />
    <item android:alpha="0.10" android:color="#00FF00"/>
</selector>

Goal is:
Button enabled => enabled style of Widget.MaterialComponents.Button
Button disabled => disabled style of Widget.MaterialComponents.Button.OutlinedButton

I need something like this (pseudocode):

<selector>
    <item style="Widget.MaterialComponents.Button.OutlinedButton" android:state_enabled="false"/>
</selector>

How can I achieve this? I do not change button attributes programmatically, because there is problem to switch between Day & Night without recreating. Thank you.

t0m
  • 3,004
  • 31
  • 53
  • Why dont you use `onClick()` on button to change shape of button? – chand mohd Aug 09 '20 at 11:51
  • `you can't change styles programmatically; you can set the look of a screen, or part of a layout, or individual button in your XML` from [Christopher Orr](https://stackoverflow.com/users/234938/christopher-orr)... – Nikhil Sharma Aug 09 '20 at 13:19
  • You can't change style in this way, but you can change all the attributes you need. – Gabriele Mariotti Aug 09 '20 at 15:32
  • I do not change button attributes programmatically, because there is problem to switch between Day & Night without recreating. – t0m Aug 11 '20 at 08:26

1 Answers1

1

XML:

<Button
            android:id="@+id/BSAMPLE"
            android:layout_width="60dp"
            android:layout_height="22dp"
            android:layout_gravity="center"
            android:layout_margin="3dp"
            android:background="#4B5E6E"
            android:gravity="center"
            android:padding="1dp"
            android:text="ENABLED"
            android:textColor="#ffffff"
            android:textSize="10sp"
            android:textStyle="bold"
            android:onClick="switchClicked"
            tools:layout_editor_absoluteX="300dp"
            tools:layout_editor_absoluteY="7dp" />

res>values>styles.xml:

<style name="btnGreen" parent="Widget.MaterialComponents.Button">
        <item name="android:textStyle">bold</item>
        <item name="backgroundTint">@color/colorPrimary</item>
    </style>

    <style name="Buttonenabled" parent="Widget.MaterialComponents.Button">
        <item name="android:background">@style/btnGreen</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:enabled">true</item>
        <item name="android:alpha">0.10</item>
    </style>

    <style name="ButtonDisabled" parent="Widget.MaterialComponents.Button.OutlinedButton">
        <item name="android:background">@style/btnGreen</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:enabled">true</item>
        <item name="android:alpha">0.10</item>
    </style>

Then by program,

  public void switchClicked(View view) {
        boolean checked = ((Switch) view).isChecked();
        String str = "";

        switch (view.getId()) {
            case R.id.sw:
                if (checked){
                    view.setClickable(false);
                    view.setBackgroundColor(R.style.btnGreen);

                }
                else{
                    view.setClickable(false);
                    view.setBackgroundColor(R.style.btnGreen);
                   }
                break;
        }
    }


MdBasha
  • 423
  • 4
  • 16
  • I do not change only backgroundColor. I want to change style from `Widget.MaterialComponents.Button` to `Widget.MaterialComponents.Button.OutlinedButton` when button is disabled. – t0m Aug 09 '20 at 12:54
  • What will this style attribute do to that button? – MdBasha Aug 09 '20 at 13:02
  • Change style [https://material.io/develop/android/components/buttons#outlined-button](https://material.io/develop/android/components/buttons#outlined-button) – t0m Aug 09 '20 at 13:09
  • set an ONCLICK attribute in – MdBasha Aug 09 '20 at 13:36
  • Check this [link](https://stackoverflow.com/a/61929320/13031115) and this [link](https://stackoverflow.com/a/2016344/13031115) Second link deals exactly your issue. – MdBasha Aug 09 '20 at 13:58
  • Updated answer. – MdBasha Aug 09 '20 at 14:15
  • I do not change button attributes programmatically, because there is problem to switch between Day & Night without recreating. I want to **change style, when button is disabled by XML**. – t0m Aug 11 '20 at 08:31
  • Then use validation in program if(BUTTON.isClickable()) in onCreate() and then call switchclicked() in if statement – MdBasha Aug 11 '20 at 08:35