I have a vertical linear layout that is a part of a scrollView
. In the vertical linear layout I add horizontal linear layouts with 2 buttons in each, one larger button with a name and a small delete button. When the name becomes too big the delete buttons gets squished or essentially removed. I want to avoid this by causing the delete button to always remain the same size and constraining the text button to the space remaining in the layout.
Here is my save files, I want to prevent the delete button from getting any smaller. This could be either achieved by making the text be on multiple lines, or changing the font size, but none of my attempts have worked as of yet. All the functionality of my program is perfect, but the sizing is making it difficult to have on multiple devices.
Here is the XML
for the linearLayout
and scrollView
:
<ScrollView
android:id="@+id/loadScrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toTopOf="@+id/returnToMainButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/loadGameText"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:id="@+id/loadLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
And Here is the code for adding the buttons:
public void addButtons() {
LinearLayout linearLayout = findViewById(R.id.loadLinearLayout);
int i = 0;
for(File file : files) {
LinearLayout innerLayout = new LinearLayout(this);
innerLayout.setOrientation(LinearLayout.HORIZONTAL);
Button newButton = new Button(this);
String filename = file.getName();
newButton.setId(i);
newButton.setText(filename);
newButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadGame(view);
}
});
innerLayout.addView(newButton);
ImageButton deleteButton = new ImageButton(this);
deleteButton.setImageResource(android.R.drawable.ic_menu_delete);
deleteButton.setId(500+i);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteSave(view);
}
});
innerLayout.addView(deleteButton);
linearLayout.addView(innerLayout);
i++;
}
}
In order to avoid the left button of being greedy on the delete button and avoid the delete button to be squeezed, you need a couple of things:
LinearLayout
to WRAP_CONTENT
LinearLayout innerLayout = new LinearLayout(this);
innerLayout.setOrientation(LinearLayout.HORIZONTAL);
Button newButton = new Button(this);
String filename = file.getName();
newButton.setId(i);
newButton.setText(filename);
newButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadGame(view);
}
});
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f); // layout_weight of 1
innerLayout.addView(newButton, params);
ImageButton deleteButton = new ImageButton(this);
deleteButton.setImageResource(android.R.drawable.ic_menu_delete);
deleteButton.setId(500+i);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteSave(view);
}
});
innerLayout.addView(deleteButton);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, // WRAP_CONTENT the widtj
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(innerLayout, layoutParams);
i++;
Testing that: