baron_bartek baron_bartek - 3 months ago 26
Java Question

setEnabled doesnt work in android

I have this code:

public TextView main_text;//begining of the class

main_text = (TextView) findViewById(R.id.TextMain); //inside OnCreate

main_text.setEnabled(false); //inside button handler


And now the part of Xml

<TextView
android:id="@+id/TextMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:textColor="#FFFFFF"
android:text="@string/home_load" >

</TextView>


Why doesnt SetEnable work? It seems so obvious that it should.

Answer

What do you expect setEnabled(false) to do to a TextView?

If you want to hide the TextView, you should rather call setVisibility(View.INVISIBLE)

If you want to disable clicks, you should rather call setOnClickListener(null)

If you want the text to display in a disabled state, then you need to define the states for the view in a separate XML file.

For example textView.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:color="@color/enabled" />
    <item android:state_enabled="false" android:color="@color/disabled" />
</selector>

And then in your TextView definition use

android:textColor="@drawable/textView"
Comments