1

I'd like to ask you what global code do I have to create to highlight text when pressed in various places of the app. Or do I have to just add color line to onClick method in every single text body which is going to be highlighted?

Thanks for your advice.

To be more specific with my question, just have a look at this code:

public class MainActivity extends AppCompatActivity {
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView text = (TextView) findViewById(R.id.textView2);
        text.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                text.setTextColor(Color.GREEN);
            }
        });
    }
}

I have 54 answers in sets of 3 answers on every page/screen and have to use the same method for each answer - being highligted when pressed. I wonder how can I do it properly. If I add to findViewById(R.id.textView2); another text id right after textView2 this isn't working.

Kubs
  • 21
  • 1
  • 8
  • Welcome to Stack Overflow! Please review our [SO Question Checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) to help you to ask a good question, and thus get a good answer. – Joe C Jan 28 '17 at 18:47
  • You need to be much more specific with your question. Which component(s) are you working with? Do you want all the text to be highlighted in all components? What have you tried so far? – DevilsHnd - 退職した Jan 28 '17 at 22:31

1 Answers1

1

You need to use textSelector for this.

Please refer the link below for how to write selector -

Android customized button; changing text color

In your case, if you want the text color to be green after selection, your selector should be like this -

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false" android:color="defalutColor" />
    <item android:state_selected="true" android:color="greenColor" />
</selector>

And your textView will have textColor="@drawable/textSelector"

And in code you need to write OnClickListener for textView and in OnClick you just need indicate textView.setSelected(true) this will make the textColor green.

Community
  • 1
  • 1
Nagesh Jatagond
  • 344
  • 2
  • 13