Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

Android checkboxes missing text

like image 250
Matt Huggins Avatar asked Oct 07 '11 15:10

Matt Huggins


2 Answers

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);
like image 157
Matt Huggins Avatar answered Oct 03 '22 23:10

Matt Huggins


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);
like image 24
jazz Avatar answered Oct 04 '22 01:10

jazz