Android CheckBox text not displaying

I'm trying to dynamically create some CheckBoxes in one of my Android activities, but it's not rendering the text.

Here is my simplified code...

  1. Layout XML:

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip">
    
        ...
        <LinearLayout
            android:id="@+id/register_attracted_to"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
        ...
    </LinearLayout>
    
  2. Activity code:

    final LinearLayout attractedTo = (LinearLayout) findViewById(R.id.register_attracted_to);
    
    final CheckBox male = new CheckBox(this);
    male.setText("Male");
    attractedTo.addView(male);
    
    final CheckBox female = new CheckBox(this);
    female.setText("Female");
    attractedTo.addView(female);
    

My "real" code is a little more complex (any dynamic) than this, which is why I haven't simply included the checkboxes in the layout itself. However, even dumbing down my code still doesn't render the checkbox text properly.

Here's a screenshot to demonstrate (see the "Attracted To" section), with a little extra to demonstrate that my vertical layout appears to be working properly otherwise:

Of course I figure this out shortly after posting a bounty. ;) It turns out that since I was setting my container view's background color to white, the default white text was blending in. The solution was to set the text color of each checkbox. i.e.:

final LinearLayout attractedTo = (LinearLayout) findViewById(R.id.register_attracted_to);

final CheckBox male = new CheckBox(this);
male.setText("Male");
male.setTextColor(getResources().getColor(R.color.foreground_text));
attractedTo.addView(male);

final CheckBox female = new CheckBox(this);
female.setText("Female");
female.setTextColor(getResources().getColor(R.color.foreground_text));
attractedTo.addView(female);

Checkboxes, Transferring data without draining the battery To create each checkbox option, create a CheckBox in your layout. android:text="@string/meat" the android: onClick attribute must have a signature exactly as shown above. Fullscreen Video Modal Boxes Delete Modal Timeline Scroll Indicator Progress Bars Skill Bar Range Sliders Tooltips Display Element Hover Popups Collapsible Calendar HTML Includes To Do List Loaders Star Rating User Rating Overlay Effect Contact Chips Cards Flip Card Profile Card Product Card Alerts Callout Notes Labels Circles Style HR Coupon

you are not setting the Layout parameters, Layout parameter says how the control will be shown

final CheckBox female = new CheckBox(this);
female.setText("Female");
female .setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
attractedTo.addView(female);

CheckBox, A checkbox is a specific type of two-states button that can be either checked or unchecked. A example usage of a checkbox inside your activity� You are actually telling LinearLayout to capture all available space, which is causing you last textview not showing. Instead use "wrap_content" which tells to capture space required to show the available content.

Maybe it's due to the simplification of your real code, but did you set width and height of your checkbox ?

Checkbox won't check when label is clicked, Matches any checked/selected radio, checkbox, or option */ :checked <div> < input type="radio" name="my-input" id="yes"> <label to let the user toggle content based on the state of a checkbox, all without using JavaScript. Style the button */ #expand-btn { display: inline-block; margin-top: 12px;� Instead of adjusting the text for Checkbox, I have done following thing and it worked for me for all the devices. 1) In XML, add checkbox and a textview to adjacent to one after another; keeping some distance. 2) Set checkbox text size to 0sp. 3) Add relative text to that textview next to the checkbox.

I just did the same and found that I was using setText("") in the initialisation code rather than setChecked(false). Dur!

:checked, They both display a list of options which a user can select from. Checkbox fields allow the user to select one or more options and boxes can be checked and unchecked. Radio Allow users to enter free text with an 'Other' option. Note: Do not leave the saved value blank on a radio or checkbox option. How to get checkbox checked value in android Select CheckBox selected values.Checkbox values and show a toast message on screen with checkbox selection.

Checkboxes and Radio Buttons, The user is not able to tick the Policy Privacy checkbox.… Checkboxes are not working in the registration form. Anyone .um-icon-android-checkbox-outline:: before { I am having this same issue, but with a custom text and drop down field . If checkbox id : “ chkIos ” is checked, display a floating box with message “Bro, try Android”. If button is is clicked, display a floating box and display the checkbox states.

Checkbox in the registration form is not working, I'm trying to dynamically create some CheckBoxes in one of my Android activities, but it's not rendering the text. Here is my simplified code Layout XML: CheckBox Tutorial With Example In Android Studio. In Android, CheckBox is a type of two state button either unchecked or checked in Android. Or you can say it is a type of on/off switch that can be toggled by the users.

Android CheckBox text not displaying, Base for label styling */ [type="checkbox"]:not(:checked), UI of a radio button or checkbox, and display a custom version right on top of it. When the user selects a checkbox, the CheckBox object receives an on-click event. To define the click event handler for a checkbox, add the android:onClick attribute to the <CheckBox> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.

Comments
  • I wouldn't dynamically add UI components, because there is a difficulty reviewing them at development time. a developer needs to run the whole app just to see the added components. I humbly suggest adding them with visibility gone. It is very lightweight because they are never drawn but they can still be found (findViewById).
  • The checkboxes are generated via an HttpRequest, meaning that your suggestion is impossible.
  • this too happened to me. It seems like the default text color for checkboxes is white on pre-honeycomb devices
  • yea, just like the text views
  • I tried doing this previously, which did not work for me: male.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); I'll try your solution though as soon as I get a moment.
  • It looks like checkbox only accepts ViewGroup.LayoutParams, and there is no third parameter for this constructor, as you supply here. I tried the two-parameter constructor using LayoutParams.WRAP_CONTENT for each, and that didn't work either.
  • Not for me. I toyed with it all weekend to no avail, both in my emulator and on my phone.
  • See my comments in @jazz's response.