I want to change the text color of dynamically created checkbox in android.
Please guide
CheckBox chk = new CheckBox(this); chk.setText("Testing"); chk.setTextColor(Color.BLUE); or From color.xml
chk.setTextColor(getResources().getColor(R.color.blue)); 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.
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.