Android: How can I change the text size of dynamically created radio buttons?
Asked Answered
B

3

6

I didn't find any solution regarding this problem in stack overflow that's why I am posting it here. I am creating radio buttons dynamically as follows.

RadioButton rb;

//generating radio buttons
for (int i = 0; i < 5; i++) {
    rb = new RadioButton(this);
    rb.setText(question.getOptions().get(i));
    rb.setTextColor(getResources().getColor(R.color.rbTextColor));
    rb.setId(i);
    rg.addView(rb);
}

It's working perfectly fine but now I want to change the text size of the text of radio buttons through code which should be something like this an per my poor understanding.

rb.setTextSize(20);

but it's now working because radio button does not have this property. So please help me out how can I change the size of the text of the radio buttons in android?

Any help would be greatly appreciated.

Thanks,

momersaleem

Birdsong answered 27/1, 2015 at 19:50 Comment(1)
What you mean? RadioButtons extend TextView so I think it should work.Hollishollister
S
4

You use text appearance like so:

    RadioButton rb;

    //generating radio buttons
    for (int i = 0; i < 5; i++) {
        rb = new RadioButton(context);
        rb.setText("");
        rb.setTextColor(getResources().getColor(R.color.rbTextColor));
        rb.setId(i);
        rb.setTextAppearance(context, android.R.style.TextAppearance_Large);
    }
Sapling answered 27/1, 2015 at 22:44 Comment(1)
Do you have something that is API 21 compliant?Typographer
H
1

in your xml file, add the below line in the radio button view

android:textAppearance="?android:attr/textAppearanceLarge"
Hardwood answered 27/1, 2015 at 20:5 Comment(1)
OP wanted dynamically created buttons to change text size.Sapling
D
1

With Kotlin, I used the below:

  1. Built Extension file for styling:

RadioButtonExt.kt:

fun RadioButton.StyleMe() {
    this.setTextAppearance(android.R.style.TextAppearance_Holo_Medium)
    this.setPaddingRelative(10,0,0,0)
    this.setPadding(30,0,0,0)
    }

Then built my element as:

myRadioButons.kt

import android.content.Context
import android.support.v7.widget.CardView
import android.view.Gravity
import android.widget.*

import android.widget.LinearLayout
import oryx.tecna.locateme.extensons.StyleMe

class Gender : CardView {
    lateinit var m: RadioButton
    lateinit var f: RadioButton

    constructor(context: Context) : super(context) {
        init(context)
    }

    private fun init(context: Context) {

        val parent = LinearLayout(context)
        m = RadioButton(context).apply {
            text = "Male"
            StyleMe()
        }
        f = RadioButton(context).apply {
            text = "Female"
            StyleMe()
        }

        var keywordAction = RadioGroup(context).apply {
            addView(m)
            addView(f)
            check(m.id)
        }

        parent.apply {
            layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT).apply {
                orientation = LinearLayout.VERTICAL
                gravity = Gravity.CENTER


                addView(keywordAction).apply {
                    id = generateViewId()
                    layoutParams = LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT).apply {
                    }
                }
            }
        }
        this.apply {
            addView(parent)
        }
    }
}

And in the MainActivity.kt I called it is:

val gender = Gender(this).apply {
    id = generateViewId()
}

gender.m.setOnClickListener {
    // Do something
}
Drear answered 21/4, 2018 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.