Issue
I'm trying to programmatically fill a TableLayout with buttons. However I want them to fill the entire available space.
But the Buttons are not using the entireheight they could, and their margins are ignored. See this screenshot.
How can I force them to fill all the available space, and have some margin beween them? The result should be basically a grid of buttons with some margin between them, but stretching the entire screen.
Heres my code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout myTable = findViewById(R.id.myTable);
TableLayout.LayoutParams tableRowLayoutParams = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
int counter = 1;
for (int i = 0; i < 6; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(tableRowLayoutParams);
for (int j = 0; j < 5; j++) {
Button b = new Button(this, null, R.attr.myButtonStyle);
b.setText("Button " + counter);
tableRow.addView(b, j);
counter++;
}
myTable.addView(tableRow);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myTable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*"
tools:context="com.example.tabletest.MainActivity">
</TableLayout>
And the button style which is referenced via the attrs:
<style name="myButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_margin">5dp</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_gravity">fill</item>
<item name="android:gravity">center</item>
<item name="android:padding">0dp</item>
<item name="android:textColor">@android:color/primary_text_dark</item>
<item name="android:background">@android:color/holo_blue_dark</item>
<item name="android:textSize">45sp</item>
</style>
Solution
I can't get this to work like that either.
However a workaround for this would be replacing the TableLayout
with a vertical LinearLayout
and then replacing the TableRow
with another LinearLayout
, this time horizontal. Then set the Buttons to layout_width = 0
and layout_weight = 1
. (Also add margins if necessary)
You should then have the following code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout myTable = findViewById(R.id.myTable);
LinearLayout.LayoutParams tableRowLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f);
LinearLayout.LayoutParams tableBtnParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1f);
int tableBtnMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
tableBtnParams.setMargins(tableBtnMargin,tableBtnMargin,tableBtnMargin,tableBtnMargin);
int counter = 1;
for (int i = 0; i < 6; i++) {
LinearLayout tableRow = new LinearLayout(this);
tableRow.setLayoutParams(tableRowLayoutParams);
for (int j = 0; j < 5; j++) {
Button b = new Button(this, null, R.attr.myButtonStyle);
b.setLayoutParams(tableBtnParams);
b.setText("Button " + counter);
tableRow.addView(b, j);
counter++;
}
myTable.addView(tableRow);
}
}
}
Answered By - petroni
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.