I have the following RadioButtons:
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 :
How can I fix this ?
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