Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Button's enabled color without changing disabled color

I'm using the element Button for my xml layout. Button class from android.widget that extends TextView.

This view has a possibility to tag as enable or disable by java code. .setEnabled(true|false).

Button xml code

<Button
    android:id="@+id/maps_list_save_button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/str_save"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

What I wanna:

enter image description here When my button is enable I wanna give him a purple color.

enter image description here When my button is disable get a grey color.

What I don't wanna do:

Create a new element and include the layout, I'm avoiding this because I wanna keep the selected animation, raise, padding, elevation etc. Create everything again is not smart.

What I already try:

Change the Background = it loose the inside padding, what make the button bigger and I wanna keep the material design "rules" enter image description hereenter image description here

Change Theme = I tried to change the theme, by editor and by code but two things happen: or I change more stuffs that are not the button or I change the Enable and Disable for the same color.

Even looking for the docs I did not found how to use properly this element.

like image 314
Canato Avatar asked May 04 '18 17:05

Canato


2 Answers

What you need is to change android:colorAccent value for specifically that Button. That can be achieved with applying a theme to the Button.

Inside styles.xml introduce following changes:

<style name="PurpleTheme" parent="AppTheme">
    <item name="android:colorAccent">#8654e2</item>
</style>

Then declare following two buttons in the xml file, where first button has enabled state, and second button has disabled state.

<Button
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1"
    android:theme="@style/PurpleTheme" />

<Button
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:enabled="false"
    android:text="Button 2"
    android:theme="@style/PurpleTheme" />

Pay attention, that buttons are applied:

  • style="@style/Widget.AppCompat.Button.Colored"
  • android:theme="@style/PurpleThemeOverlay"

Then you'll get following output:

enter image description here

like image 63
azizbekian Avatar answered Oct 18 '22 23:10

azizbekian


You can use selector like below to achieve this

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_enabled="true">
    <shape
        android:shape="rectangle">
      <solid android:color="Your Color"></solid>
      <corners android:radius="5dp"></corners>
      <padding android:bottom="2dp" android:left="2dp"
          android:right="2dp" android:top="2dp"></padding>
    </shape>
  </item>
  <item android:state_enabled="false" android:drawable="@color/colorAccent"/>

</selector>
like image 2
Raja Jawahar Avatar answered Oct 18 '22 23:10

Raja Jawahar