0

I am using TextView as a Button (flat UI) in my android application. Below is the code

<TextView
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:background="@drawable/button_background"
            android:enabled="false"
            android:gravity="center"
            android:paddingBottom="16dp"
            android:paddingTop="16dp"
            android:text="Sign Up"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold" />

The background drawable 'button_background' is

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#FCD5A5" android:state_enabled="false" />
<item android:drawable="#F7941E" />

So when Button is enabled it should have dark orange background otherwise light orange background.

Background color is working fine with both the states (enabled and disabled) but text color is also getting changed. It remains white in enabled state but changes to dark grey in disabled state. I want to keep it white in both the states.

hoijui
  • 3,615
  • 2
  • 33
  • 41
  • [try this](http://stackoverflow.com/questions/5371719/change-clickable-textviews-color-on-focus-and-click) this willo deffinately help you. – Shivanshu Verma Nov 02 '15 at 07:33

2 Answers2

0

I'm currently looking at how to do this for your selector. But for now, you could always do this and call it once to initialize and then when the state changes:

private void updateTextColor(TextView view, Context context) {
    if (!view.isEnabled()) {
        view.setTextColor(context.getResources().getColor(android.R.color.white));
    }
}
vguzzi
  • 2,420
  • 2
  • 15
  • 19
0

textcolorselector.xml <= put this file in drawable

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

<item android:color="@color/general_blue" android:state_enabled="true"></item>
<item android:color="@color/general_gray" android:state_enabled="false"></item>
<item android:color="@color/general_blue"></item>

add this line to color.xml file

 <drawable name="textviewcolor">@drawable/textcolorselector</drawable>

and finally apply this to your layout

<TextView
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:background="@drawable/button_background"
        android:enabled="false"
        android:gravity="center"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:text="Sign Up"
        android:textColor="@color/textviewcolor" // <== i made change here!
        android:textSize="16sp"
        android:textStyle="bold" />
Rajesh
  • 2,618
  • 19
  • 25