Akshay
Akshay

Reputation: 2534

How to highlight selected text in android?

I have created Text View dynamically.I have to set focus/highlight the particular text when i click on that Text View.please suggest me how to do this?

Upvotes: 0

Views: 2747

Answers (4)

Rasel
Rasel

Reputation: 15477

Try this

text.setFocusable(true);

also

text.requestFocus();

Upvotes: 0

Phil
Phil

Reputation: 36289

Have you tried this:

TextView tv = (TextView) findVieById(R.id.my_textview); //replacing my_textview with the correct resource id
tv.setSelectAllOnFocus(true);

Upvotes: 0

mthpvg
mthpvg

Reputation: 3809

int blue = 0xff0000ff;
int red = 0xffff0000;
text.setBackgroundColor(blue);
text.setTextColor(red);

Upvotes: 0

Alê Oliveira
Alê Oliveira

Reputation: 6439

You could set a listener to control the desired behavior on the "click" event. Something like this:

textView.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
       v.setBackgroundColor(Color.GREEN);
    }
});

If you want to deselect the selected TextView when you click in a new one, just change the background colors of the preciously selected TextView.

Android TextView's API here.

Hope it helps :D

Upvotes: 1

Related Questions