right alignment text radio button android programaticly

1.9k Views Asked by At

Hi I need to Create a custom radio button class with two different picture for each state and RTL text. I made the button but i couldn't make text RTL for api 8 and upper . i would be happy if some one could help me

RadioGroup crdg = (RadioGroup) findViewById(R.id.radioGroup11);

make Radio button

rdbt1 = new RadioButton(new ContextThemeWrapper(
            getApplicationContext(), R.style.styleName));

    rdbt1.setButtonDrawable(android.R.color.transparent);

rdbt1.setText("for example(RTL text)");

add RadioButton to Radio Group

crdg.addView(rdbt1);

styleName is

<style name="styleName" parent="android:Widget.CompoundButton.RadioButton"> <item name="android:button">@null</item> <item name="android:drawableRight">@drawable/rdb_question_selector</item> </style>

2

There are 2 best solutions below

0
On

You just have to set the gravity to Gravity.END So for example:

 RadioButton tempRadioButton = new RadioButton(this);
 tempRadioButton.setGravity(Gravity.END);
 tempRadioButton.setText("for example(RTL text)");

This will tell Android to align the text to the "end" of the view respecting RTL languages.

0
On

add this

forceRTLIfSupported();

in your oncreate()

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void forceRTLIfSupported()
    {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
            getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }
    }