Reputation: 6090
I'm trying to create custom RadioButtons/Checkboxes programmatically with info from a Http request (creating a survey in-app). I've tried the suggested solutions in Android Checkbox text not displaying but the text still isn't appearing.
I'm able to create the custom button views, but for some reason the text doesn't display. Any idea where I'm going wrong?
checkbox_selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/multiselected" android:state_checked="true"/>
<item android:drawable="@drawable/multiunselected" android:state_checked="false"/>
</selector>
radio_selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/singleselected" android:state_checked="true"/>
<item android:drawable="@drawable/singleunselected" android:state_checked="false"/>
</selector>
Here's how I'm creating them:
if (mQuestion != null && mQuestion.isSingleResponse()
&& mAnswerTitles.size() > 0) {
for (int i = 0; i < mAnswerTitles.size(); i++) {
Answer answer1 = mAnswerTitles.get(i);
RadioButton answer = new RadioButton(getActivity());
answer.setText(answer1.getTitle().toString());
answer.setTextSize(24);
answer.setTextColor(Color.BLACK);
answer.setId(answer1.getId());
answer.setButtonDrawable(R.drawable.radio_selector);
answer.setLayoutParams(mRadioGroupAnswers.getLayoutParams());
answer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mNextQuestion.setEnabled(true);
}
});
mRadioGroupAnswers.addView(answer);
}
} else {
if (mAnswerTitles != null && mAnswerTitles.size() > 0) {
for (int i = 0; i < mAnswerTitles.size(); i++) {
Answer answer1 = mAnswerTitles.get(i);
CheckBox answer = new CheckBox(getActivity());
answer.setText(answer1.getTitle().toString());
answer.setTextSize(24);
answer.setTextColor(Color.BLACK);
answer.setId(answer1.getId());
answer.setButtonDrawable(R.drawable.checkbox_selector);
answer.setLayoutParams(mRadioGroupAnswers.getLayoutParams());
answer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mNextQuestion.setEnabled(true);
}
});
if (mAnswerHolder != null) {
mAnswerHolder.addView(answer);
}
}
}
And the group/layout I'm adding them to:
<RadioGroup
android:layout_marginTop="8dp"
android:id="@+id/answerGroup"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RadioGroup>
<LinearLayout
android:id="@+id/answerTextHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
It looks like the text is actually just getting hidden by the image - rather than being put on the image and re-sizing the image with the text length:
Upvotes: 1
Views: 1164
Reputation: 6090
I figured it out! The trick was using setBackgroundResource instead of setButtonDrawable:
answer.setBackgroundResource(R.drawable.radio_selector);
Thought after doing some testing - I do advise also overriding the Radio/Checkbox button drawable as well if you're doing it completely custom.
Upvotes: 1