Nexussim Lements
Nexussim Lements

Reputation: 787

Android RadioButton not centering

I have the following RadioButtons:

enter image description here

They are generated programmatically in this method:

 private void initializeAbilityComponents() {
    rgAbility = view.findViewById(R.id.rgAbility);

    for (int i = 0; i < abilities.size(); i++) {
        RadioButton radioButton = new RadioButton(context);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(4,4,4,4);
        radioButton.setLayoutParams(layoutParams);
        radioButton.setText(abilities.get(i).getName());
        radioButton.setId(i);
        radioButton.setGravity(Gravity.CENTER);
        PokemonUtils.setRoundedBackgroundColorToView(radioButton,selectedPokemon.getColor());
        rgAbility.addView(radioButton);
    }

And added to this xml RadioGroup:

  <RadioGroup
                android:id="@+id/rgAbility"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="6dp"
                android:layout_marginEnd="6dp"
                android:layout_marginBottom="4dp"
                android:layoutAnimation="@anim/default_layout_right_to_left_animation"
                android:orientation="vertical" />

However , as you can see , they are not really centered , the output I expect when I center is this :

enter image description here

How can I fix this ?

Upvotes: 0

Views: 235

Answers (2)

KKKKK
KKKKK

Reputation: 284

You have to add:

radioButton.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
radioButton.setPadding(0,0,100,0); //left, top, right, bottom

With the value 100 for right you move your text to the left

Upvotes: 1

grig
grig

Reputation: 847

This happens because both dot and text are the part of the view. If you want the text be "more centered", you have to add paddingEnd. Of course this padding depends on the size of the dot.

Upvotes: 1

Related Questions