I have implemented checkbox programtically via for loop, how to change them to black color since other textviews are in black color its lucking odd, they are not in the xml ... there is one linear layout where these are getting added.
for (int i = 0; i < list_sted.size(); i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText(""+list_sted.get(i));
cb.setTextColor(R.color.Black);
cb.setTextAppearance(getBaseContext(), android.R.attr.checkboxStyle);
checkbox_lay.addView(cb);
}
You can't do this.
cb.setTextColor(R.color.Black);
Use this one.
cb.setTextColor(Color.BLACK);
or
cb.setTextColor(Color.parseColor("#000000"));
Try using this:
cb.setTextColor(Color.BLACK);
or From color.xml
cb.setTextColor(getResources().getColor(R.color.black));
Related
Using Theme.AppCompat in Gingerbread (API 10), programmatically added buttons do not match buttons added through XML. It works fine in all newer APIs, its only an issue with Gingerbread. This image shows the issue.
Here is the code that adds the buttons:
for (int i = 0; i < btnFiles.length; i++) {
btnFiles[i] = new Button(this);
btnFiles[i].setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
btnFiles[i].setGravity(Gravity.CENTER);
btnFiles[i].setId(100 + i);
btnFiles[i].setText(fileList.get(i).replace(".xml", ""));
btnFiles[i].setTag(fileList.get(i));
registerForContextMenu(btnFiles[i]);
btnFiles[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Continue(v); //Start next activity when button is pressed
}
});
l.addView(btnFiles[i]);
setTitle(getString(R.string.title_activity_load_menu));
}
Make a layout file with just the Button and use a LayoutInflater to inflate it.
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
... />
LayoutInflater inflater = getLayoutInflater();
for (int i = 0; i < btnFiles.length; i++) {
btnFiles[i] = (Button) inflater.inflate(R.layout.button, l, false);
// everything else, except the LayoutParams stuff because that's in the layout file
}
Just to clarify for anyone passing by, the issue is probably that <Button> tags from XML get replaced by instances of AppCompatButton and <item name="buttonStyle">...</item> (assuming that's what had been used) applies to that. Same thing happens for many other Views.
So, an alternative possibility would be either to put both <item name="buttonStyle">...</item> and <item name="android:buttonStyle">...</item> into the style, so that Button and AppCompatButton can be combined, which would be quite a mess.
Somewhat better option would be to instantiate AppCompatButton for the APIs using AppCompat, but using the XML layout with just a single Button seems like the safest and most portable solution, so go for that, assuming you have no reason not to do that.
How to set FragmentTabHost tab text color. I tried the following code but, it didn't work.
((TextView) mTabHost.getCurrentTabView()
.findViewById(android.R.id.title)).setTextColor(0xFFFFFFFF);
It gives NPE saying it couldn't find the TextView.
It was a bit tricky. I used the following code and it worked for me.
for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
final TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i)
.findViewById(android.R.id.title);
// Look for the title view to ensure this is an indicator and not a divider.(I didn't know, it would return divider too, so I was getting an NPE)
if (tv == null)
continue;
else
tv.setTextColor(0xFFFFFFFF);
}
let's try this :
for example when you add your tab make your Indicator :
TextView view = ....
vew.setTextColor(...)
then setIndicator with your custom view :
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator(view),
FragmentStackSupport.CountingFragment.class, null);
I'm trying to programmatically add a set of RadioButtons to a RadioGroup like so:
for (int i = 0; i < RANGE; i++) {
RadioButton button = new RadioButton(this);
button.setId(i);
button.setText(Integer.toString(i));
button.setChecked(i == currentHours); // Select button with same index as currently selected number of hours
button.setButtonDrawable(Color.TRANSPARENT);
button.setBackgroundResource(R.drawable.selector);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
((RadioGroup)view.getParent()).check(view.getId());
currentHours = view.getId();
}
});
radioGroupChild.addView(button);
}
and I need to set the button drawable to null (I want just text on top of my background). Manually doing this in the XML with android:button="#null" works great, but I don't want to hardcode each radio button. I've tried just doing button.setButtonDrawable(null) but it doesn't change anything.
Any help is greatly appreciated!
You need to set an empty StateListDrawable as the drawable. So the Java equivalent of android:button="#null" is:
radioButton.setButtonDrawable(new StateListDrawable());
You should do this:
button.setBackgroundDrawable(null);
If your drawable has a reference to selector you can make it transparent through your selector xml:
<item android:drawable="#android:color/transparent" />
as you'd probably found the solution ;)
setButtonDrawable(getResources().getDrawable(android.R.color.transparent))
should do the trick.
Only this worked for me.
What worked for me in multiple devices and APIs and is as simple as it gets was setButtonDrawable(android.R.color.transparent);
in Kotlin you can do that
radioButton.buttonDrawable = null
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.
This Code snippet may help ypu
CheckBox mCheckBox= new CheckBox(this);
mCheckBox.setTextColor(R.color.textcolor);
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.
I need to create checkboxes dynamically in android. I am getting the value in my code but unable to set that value to checkbox.
My Code:
CheckBox[] cbs = new CheckBox[20];
for(int k=0; k<stringList3.size(); k++)
{
System.out.println("stringlist3 in for loop"+stringList3.get(0));
arr = stringList3.get(k);
cbs[k] = new CheckBox(getContext());
System.out.println("arr values"+arr.get(0));
System.out.println("arr values"+arr.get(1));
System.out.println("arr values"+arr.get(2));
cbs[k].setText((CharSequence) arr.get(2));
Rl.addView(cbs[k]);
}
Here when I am setting the value arr.get(2) to checkbox it is not setting ...please help me regarding this...
Thanks in advance
I don't know how you written remaining code, just check your code with below example.
Dynamically adding views to layout
I think in your code, the problem may be stringList3.size() returning more than 20, so that you are getting force close. Just check it once.