RadioButton's Text not displaying after setTextSize call

66 Views Asked by At

I have programmatically added several RadioButtons to a pre-existing RadioGroup. The buttons themselves are there and are clickable, but their texts just won't appear. I have set their texts using the setText method, and by using Toast and RadioButton's getText method, I was able to confirm that all of the buttons do possess the texts they should have - they just don't display them.

I have made sure to set their alpha to 1, the text size to 20sp, their visibility to View.Visible, and I tried changing the text color and the background color several times to contrasting colors, but the text still didn't appear.

Can someone please help me figure out how to make the text appear?


The Java code:

        for(int i = 0; i < CERTAIN_CONSTANT_VALUE; i++)
        {
            RadioButton r = new RadioButton(this);
            android.widget.LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            r.setLayoutParams(params);
            r.setId(View.generateViewId());
            r.setText(answers[i]);
            r.setOnClickListener(this::onBtnClicked);
            r.setTextSize(20, TypedValue.COMPLEX_UNIT_SP);
            r.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
            r.setTextColor(Color.BLACK);
            r.setAlpha(1);
            r.setVisibility(View.VISIBLE);
            r.setBackgroundColor(Color.GREEN);
            this.radioGroup.addView(r);
        }

The RadioGroup in the XML file:

<RadioGroup
    android:id="@+id/radioGroup"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="200dp">
</RadioGroup>

Minimal, Reproducible Example

Java:

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RadioGroup radioGroup = findViewById(R.id.radioGroup);
        String[] answers = {"first", "second", "third", "fourth"};
    
        for (int i = 0; i < 4; i++)
        {
            RadioButton r = new RadioButton(this);
            android.widget.LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            r.setLayoutParams(params);
            r.setId(View.generateViewId());
            r.setText(answers[i]);
            r.setOnClickListener(this::onBtnClicked);
            r.setTextSize(20, TypedValue.COMPLEX_UNIT_SP);
            r.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
            r.setTextColor(Color.BLACK);
            r.setAlpha(1);
            r.setVisibility(View.VISIBLE);
            r.setBackgroundColor(Color.GREEN);
            radioGroup.addView(r);
        }
    }

    public void onBtnClicked(View view){}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </RadioGroup>
</LinearLayout>
1

There are 1 best solutions below

1
farshad rezaei On BEST ANSWER

just change setTextSize to this :

r.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f);