0

I want to change the text color of dynamically created checkbox in android.

Please guide

4 Answers 4

9
CheckBox chk = new CheckBox(this); chk.setText("Testing"); chk.setTextColor(Color.BLUE); 

or From color.xml

chk.setTextColor(getResources().getColor(R.color.blue)); 
Sign up to request clarification or add additional context in comments.

1 Comment

it will hide the text only check box apper
1

Here is how i used to add CheckBox Dynamically to RadioGroup.

CheckBox mCheckBox = new CheckBox(this); //mCheckBox.setText(String.format("%s",header)); //mCheckBox.setId(index1); //mCheckBox.setLayoutParams(lp); // mCheckBox.setOnClickListener(this); //mCheckBox.setPadding(mCheckBox.getTotalPaddingLeft() + 10, 0, 0, 10); mCheckBox.setTextColor(Color.GREEN); 

in above code you can see how CheckBox Text Color is set and also you can refer it by it's id.

1 Comment

my checkbox are in for loop like for (k = 0; k < strwsite.size(); k++) { Strwebsitename = strwsite.get(k); chkbx[k] = new CheckBox(this); chkbx[k].setText(Strwebsitename); chkbx[k].setTextColor(color.black);
1

This Code snippet may help ypu

CheckBox mCheckBox= new CheckBox(this); mCheckBox.setTextColor(R.color.textcolor); 

Comments

0

If you want to change text color for checkbox you need to do it last thing, after methods like: setText(), setListener...

try { mCheckBox.setTextColor(mContext.getResources().getColor(R.color.red, null)); } catch (NoSuchMethodError e) { Log.d(TAG, e.toString()); } 

Some old APIs will trigger NoSuchMethodError exception so it will be good to catch it. And don't forget to set your theme or go with null if so.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.