0

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 ?

Nexussim Lements
  • 535
  • 1
  • 15
  • 47

2 Answers2

1

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.

grig
  • 848
  • 6
  • 15
1

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

KKKKK
  • 273
  • 2
  • 18