Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set textview focus color programmatically / change focus color in themes

1) Is it possible to set a TextView's color programmatically? If so what's the easiest way?

I want something else other that the default 4.0+ light-blue color.

I found and tried the following code without success:

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed}, new ColorDrawable(0x1A000000));
states.addState(new int[] {android.R.attr.state_focused}, new ColorDrawable(0x1A000000));

if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    tv.setBackgroundDrawable(states);
} else {
    tv.setBackground(states);
}

I do not wish any XML involved.

2) Can I change the focus color in my themes in general? If yes how?

XML is obviously fine here.

like image 207
Diolor Avatar asked Dec 26 '22 09:12

Diolor


1 Answers

You can use ColorStateList, to specify the state color programmatically.

    int[][] states = new int[][] {
        new int[] { android.R.attr.state_pressed}, // pressed
        new int[] { android.R.attr.state_focused}, // focused
        new int[] { android.R.attr.state_enabled} // enabled
    };

    int[] colors = new int[] {
        Color.parseColor("#008000"), // green
        Color.parseColor("#0000FF"), // blue
        Color.parseColor("#FF0000")  // red
    };

    ColorStateList list = new ColorStateList(states, colors);
    textView.setTextColor(list);        
    textView.setClickable(true);
    textView.setFocusableInTouchMode(true);
like image 151
Manish Mulimani Avatar answered Apr 13 '23 01:04

Manish Mulimani