I have a class that I am building programmatically and I need to adjust the 2 button layout to look like the other classes I have.
In this class AttributeWizard I'm doing through code, because I do database manipulation and I need to build the GUI like this.
I want to adjust the layout of the buttons (layoutButtons) to fit the full width of the screen. Currently they do not fill the entire space.
public class AttributeWizard extends Activity {
private LinearLayout linearLayoutAttribute;
private LinearLayout layoutBotoes;
private Button bNext;
private Button bPrev;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uisarde_attributes);
linearLayoutAttribute = findViewById(R.id.linearLayoutAttribute);
createLayoutForButtons();
linearLayoutAttribute.addView(layoutBotoes);
layoutBotoes.getLayoutParams().height = LinearLayout.LayoutParams.FILL_PARENT;
layoutBotoes.getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;
}
public void createLayoutForButtons() {
layoutBotoes = new LinearLayout(AttributeWizard.this);
bNext = new Button(AttributeWizard.this);
bNext.setText("Próximo");
bPrev = new Button(AttributeWizard.this);
bPrev.setText("Anterior");
layoutBotoes.setOrientation(LinearLayout.HORIZONTAL);
layoutBotoes.addView(bPrev);
layoutBotoes.addView(bNext);
}
}
The activity_uisarde_percurso.xml layout file goes as an example to see how I want the buttons to be at the bottom (if anyone knows how to remove the tab between them, thank you)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
style="?android:textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Seleção de percurso"
android:textStyle="bold" />
<TextView
android:id="@+id/tvInfoProperty"
style="?android:textAppearanceSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/inst_percurso" />
<RadioGroup
android:id="@+id/rgPercurso"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" >
<RadioButton
android:id="@+id/rbPadrao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Padrão" />
<RadioButton
android:id="@+id/rbPersonalizado"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Personalizado" />
</RadioGroup>
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="horizontal">
<Button
android:id="@+id/prev_button_percurso"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selectable_item_background"
android:text="@string/prev" />
<Button
android:id="@+id/next_button_percurso"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selectable_item_background"
android:text="@string/next" />
</LinearLayout>
</LinearLayout>
When should I change properties? After adding to the main layout? Before? Am I setting the right property?
This is my solution:
public class AttributeWizard extends Activity {
private LinearLayout linearLayoutAttribute;
private LinearLayout layoutBotoes;
private Button bNext;
private Button bPrev;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uisarde_attributes);
createLayoutForButtons();
linearLayoutAttribute = findViewById(R.id.linearLayoutAttribute);
linearLayoutAttribute.addView(layoutBotoes);
}
public void createLayoutForButtons() {
layoutBotoes = new LinearLayout(this);
bNext = createButton();
bNext.setText("Próximo");
bPrev = createButton();
bPrev.setText("Anterior");
layoutBotoes.setOrientation(LinearLayout.HORIZONTAL);
layoutBotoes.addView(bPrev);
layoutBotoes.addView(bNext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT, 0);
params.weight = 1;
layoutBotoes.setLayoutParams(params);
}
private Button createButton() {
Button button = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, WRAP_CONTENT);
params.weight = 1;
params.gravity = Gravity.BOTTOM;
button.setLayoutParams(params);
return button;
}
}
When creating new Views/ViewGroups, you'd usually create new LayoutParams, set layout properties, and call setLayoutParams()
.
When changing layout properties of already added Views/ViewGroups, you may get its LayoutParams, set properties, and then call setLayoutParams()
. For example:
LinearLayout.LayoutParams params = layoutBotoes.getLayoutParams();
params.weight = 0;
layoutBotoes.setLayoutParams(params);
When should I change properties? After adding to the main layout? Before?
Does not matter, as long as you call setLayoutParams()
. You need to always call setLayoutParams()
to make changes to layout properties take effect.
I want to adjust the layout of the buttons (layoutButtons) to fit the full width of the screen. Currently they do not fill the entire space.
You need to set each button's weight to 1. According to its javadoc description, weight:
Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
Giving each button a weight of 1, allows both to fill the extra space in equal propotion.
I want the buttons to be at the bottom
First give layoutBotoes
a weight of 1 to fill extra space. Then, set layout_gravity
of each button to BOTTOM
. To do this programmatically:
params.gravity = Gravity.BOTTOM;
if anyone knows how to remove the tab between them, thank you
The image used for the button's background has an extra padding. You could change its background to another drawable without one. You'll need to use StateList Drawable to manually set drawable for each state like when the button is pressed, hovered, etc.