3

I have the following code:

TextView tv = (TextView) findViewById(R.id.example);

if (condition) {
    tv.setEnabled(false);
}

tv.setOnClickListener(new View.OnClickListener() { ... });

If condition is true, this causes the text to appear in a lighter grey colour but also to stop responding to clicks.

How can I make the TextView take on the 'disabled' appearance whilst still responding to click events? I would like to accomplish this without manually adjusting the colours, something like:

tv.setEnabled(false);
tv.setClickable(true);

(which doesn't work)

Is there a way to do this?

Ellis
  • 3,048
  • 2
  • 17
  • 28

3 Answers3

1

I think you are making minor mistake. Try this...

TextView tv = (TextView) findViewById(R.id.example);

if (condition) {
   tv.setEnabled(false);
   tv.setClickable(false);
}

tv.setOnClickListener(new View.OnClickListener() { ... });
Harshal Benake
  • 2,391
  • 1
  • 23
  • 40
  • I want it to remain clickable though! Calling `setEnabled(false)` by itself stops the view from being clickable. – Ellis Mar 27 '14 at 11:25
  • @Ellis so you want to avoid setEnabled() right?? and just disable the textview but keep it clickable.?? – Harshal Benake Mar 27 '14 at 11:31
  • Basically, yes - I want to change its appearance to how it looks when I use `setEnabled(false)` but still allow for interaction. – Ellis Mar 27 '14 at 11:33
1

As far as I can make out, there is not a way to accomplish what I wanted; the appearance of the text must be manually changed so as to match disabled TextViews.

Making the text look disabled:

tv.setTextColor(Color.parseColor("#bbbbbb"));

Making the text look enabled:

tv.setTextColor(context.getResources().getColor(android.R.color.primary_text_light));

The downside to this approach is that, I imagine, there will be some devices that don't use #BBBBBB as the colour for disabled TextViews. This means that text adjusted using the above method will not match text adjusted using setEnabled(false).

Ellis
  • 3,048
  • 2
  • 17
  • 28
0

This is a little late but there is a better way to do this, you can use TouchInterceptorLayout.

Just add it to your project, put your TextView inside TouchInterceptorLayout, make TextView's setEnabled() true and set a click listener to the interceptorLayout.

You can find the Java - Kotlin classes and XML codes here

Timisorean
  • 1,388
  • 7
  • 20
  • 30
Mertcan Seğmen
  • 903
  • 2
  • 11
  • 26