4

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.

baron_bartek
  • 1,073
  • 2
  • 20
  • 39
  • 1
    Without seeing more code in activity, it is hard to predict what is wrong. – kosa Nov 15 '12 at 16:14
  • http://stackoverflow.com/questions/1342410/android-text-view-color-doesnt-change-when-disabled – Talha Nov 15 '12 at 16:26
  • refer this url [http://stackoverflow.com/questions/1342410/android-text-view-color-doesnt-change-when-disabled][1] [1]: http://stackoverflow.com/questions/1342410/android-text-view-color-doesnt-change-when-disabled – Talha Nov 15 '12 at 16:26
  • Well, the code is now preety big. But basicly the pieces I presented refer to elemend I want to hide. What other pieces should I present, what can matter in this simple function? Hmmm, in difrent words: I try this code without anything (I just try to hide TextView)... and nothing happens (while setVisibility(GONE) works just fine in the same configuration) – baron_bartek Nov 15 '12 at 17:54

2 Answers2

14

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"
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
JeffG
  • 3,312
  • 1
  • 26
  • 34
  • I want to create activity that contains couple functions like: Show descripton, show contact, show gallery. When I create gallery I add some pictures to the linear view, but when I want to sho descriptin (again) I need to hide pictures from gallery. I know this might be not best solution. I tried wit INVISIBLE/GONE, and it works, but I need sth that is universal so I now try to use setEnabled (and loop throught childrew of linear view), and for now it doesnt work – baron_bartek Nov 15 '12 at 17:51
  • 1
    You should have 3 LinearLayouts - (1) for descriptions, (2) for gallery images. Wrap both those LinearLayouts in the 3rd another LinearLayout (3) (or FrameLayout depending on how u want it displayed). Then you can hide the entire inner LinearLayout (1) or (2) using layout.setVisiblity(View.GONE). This way you can hide the parent of gallery or descriptions and all the children in the layout will hide too – JeffG Nov 15 '12 at 18:03
  • Thanx, I will give it a try, and post some codes. Definite +1. – baron_bartek Nov 15 '12 at 22:08
  • Jeff, this is not really andswear to my initial question. But You definatelly solved my problem, and my app works fine now :D. I click you answear as accepted. Thanx again. – baron_bartek Nov 16 '12 at 10:05
  • I found some interesting behavior with `state_enabled` as well when using them as selectors. Basically ensure that you have either `state_pressed` or `state_enabled`. Having both wont allow the enabled state to even register. Anyone else notice this? – Andy Aug 13 '17 at 22:59
0

Views can be composed by multiple touchable elements. You have to disable them all, like this:

for(View lol : text_view.getTouchables() ) {
    lol.setEnabled(false);
}

If it is a simple one since it also returns itself:

Find and return all touchable views that are descendants of this view, possibly including this view if it is touchable itself.

View#getTouchables()

josetapadas
  • 2,589
  • 3
  • 19
  • 19