TableLayout - Remove space between columns - java

I'm having a problem with the TableLayout.
First, take a look at the screenshot:
As you can see, there is a pretty big space in the middle of the TableLayout.
I don't know how to reduce the space in the middle, so that the TableRows will have more Width to cover.
And also, I want to reduce the space between a TableRow and the one below it.
I'm adding the views to the TableLayout programmatically.
Also, I've already set the 'layout_weight' of the content of the TableRow to 1f:
TableRow tr = (TableRow) new TableRow(mTableLayout.getContext());
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
NormalCard card = new NormalCard();
card.setLayoutParams(new TableRow.LayoutParams(0, 740, 1f));
tr.addView(card);
mTableLayout.addView(tr, params);
XML declaration of the TableLayout:
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*"
android:dividerPadding="0dp"
android:showDividers="none"
android:divider="#null">
</TableLayout>
How do I reduce the space in the middle of the TableLayout.
And also, How to reduce the space between a TableRow and the one below it.
Thank you upfront.

Please specify your table layout xml file you have just shown images.
try this if it works
android:shrinkColumns="*"
android:stretchColumns="*"
in your TableLayout tag.
okay try to set padding as 0 to your table row if no of column in each row is same then specify table width as wrap_content.

Have a try with this:
params.setMargins(0, 0, 0, 0);

Related

Empty space at the end of RecyclerView

I use LinearLayoutManager to show RecyclerView list horizontally
XML
<android.support.v7.widget.RecyclerView
android:id="#+id/dailyOffersList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Code
dailyOffersList.adapter = DailyOffersListAdapter(dailyOffer)
dailyOffersList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
It is XML of item and it has width as wrap_content
https://codeshare.io/5XnldM
But there empty space int the end of list, and it happens only if it horizontally, like this
in the end
Change the match_parent in the xml to wrap_content.
match_parent is supposed to fill the whole with of the screen.

What is this Android Snippet doing?

EDIT** Figured out my own answers except for my major question as follows:
TableLayout tableLayout = new TableLayout(this);
TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
// inner for loop
for (int j = 1; j <= courseHoleCount; j++) {
**TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));**
tv.setBackgroundResource(R.drawable.cell_shape);
tv.setTextSize(30);
tv.setPadding(5, 5, 5, 5);
tv.setText(String.valueOf(players[i-1].getScore()[j-1]));// (-1) because j = 1 and not 0.
row.addView(tv);
}
tableLayout.addView(row);
linearLayout.addView(tableLayout);
In the above code snippet, what does this do and what/why are TableRow.Layout params being added to a text view layout param?
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams are being used because tv is a child of row which is a TableRow.
If you look at the Android API reference, it says
Set the layout parameters associated with this view. These supply parameters to the parent of this view specifying how it should be arranged.
And the two parameters for the LayoutParams constructor are used to set the width and height of tv. Since both width and height is set to WRAP_CONTENT, the size of tv will be big enough to enclose its content (plus padding).
The TextView's parent is a TableRow, therefore the TextViews layout params should be of type TableRow.LayoutParams.
This is the exact same as specifying:
<TableRow ...>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
If the TextView was added to some other ViewGroup like a LinearLayout then it would need LinearLayout.LayoutParams set.

How to set random TextView's position inside linear layout?

I have a linearlayout on xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
And I add dynamic 100 textviews inside it by code below:
llayout = (LinearLayout) findViewById(R.id.rootlayout);
for (int i = 0; i < 100; i++) {
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
int randomInt = new Random().nextInt(100) +1 ;
tv.setText(""+randomInt);
llayout.addView(tv);
}
The result is 100 textviews added and display vertically. This is not as my expect. I want these textviews display with random position inside the layout look like the image below:
How to do it? Thank you!
LinearLayout is for layouting its children linearly (as the name suggests).
As there is no RandomLayout, you can use a RelativeLayout with random left and top layout margins, or an AbsoluteLayout and set random x and y.
Edit: Avoid overlapping texts
Random positions can of course lead to overlapping and it would be up to you to adjust the positions or ignore positions too similar to previous ones. Or you might actually compare the bounding boxes (left, top, width, height) of the view you're about to add to all other views in the container and if there is any overlapping, find another place for it.
You can use tv.setX(position) and tv.setY(position) for show view on specific position

Android: Nested Linearlayouts will not display a grid of buttons, Matching parent

I have an app under construction. In one of the sub-menus I have a need for generic display of buttons, and therefor I want to make an activity that can display the given number of needed buttons.
I have succesfully made this happen, programmatically, but I want the total grid of buttons to fill up the entire parent they are placed in, which happens to be 3/4 of a landscape screen. The number of buttons varies from 16-38.!
I have also succesfully made this happen with other grids of buttons, in xml, with weight values, and match_parent values of entries.
When I assign buttons or rows the match_parent value programatically, it occupies the entire parent layout, not sharing it like i expect it to do, even though they have the same weight value of 1.0f
The relevant code follows below. I would like to post images as well, but I have not the reputation to do so.
`LinearLayout layout = (LinearLayout) findViewById(R.id.linear_custom_draw);
layout.setOrientation(LinearLayout.VERTICAL);
int columns = Math.min(6, 4+category); //sets number of buttons per row to 4-6
for (int i = 0; i < 4+category; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new android.view.ViewGroup.LayoutParams(android.view.WindowManager.LayoutParams.MATCH_PARENT,
android.view.WindowManager.LayoutParams.MATCH_PARENT));
//the line above is the line that fills up the entirety of the linearlayout, even though there are more entries, unlike my xml-defined attempts.
row.setOrientation(LinearLayout.HORIZONTAL);
row.setWeightSum(1.0f);
if(i%2 == 0){
row.setBackgroundColor(getResources().getColor(R.color.listview_red_backgr_color));
}
for (int j = 0; j < columns; j++) {
int index = (i*columns)+j;
if(formations.size() > index){
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText(formations.get(index).getName());
btnTag.setTextColor(getResources().getColor(R.color.black_overlay));
btnTag.setId(formations.get(index).getId());
row.addView(btnTag);
}
}
layout.addView(row);`
Try to use TableLayout. Each Row will enforce the entire elements to match the parent with the same wights. You can control number of Buttons into each Row programatically with counter. Loop for end of Counter adding your buttons then add new Table Row
TableLayout tbl=new TableLayout(context);//create table
TableRow tr=new TableRow(context);//create table row
tr.addView(view);//add your button instead of the view
tbl.addView(tr);//add the row into the Table
In the XML file
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/keypad"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stretchColumns="*">
<TableRow>
<Button android:id="#+id/keypad_1" android:text="#string/_1"></Button>
<Button android:id="#+id/keypad_2" android:text="#string/_2"></Button>
<Button android:id="#+id/keypad_3" android:text="#string/_3"></Button>
</TableRow>
</TableLayout>

How to set the layout margins of edit text boxes?

In the table layout i have a tablerow and in that tablerow i have 6 edit text boxes and i want to set the layout margins for that 6 edit text boxes
TableLayout t1=(TableLayout)findViewById(R.id.table_layout01);
TableRow tr1=new TableRow(inventory.this);
tr1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tr1.setBackgroundColor(Color.BLACK);
EditText ed6=new EditText(inventory.this);
//ed6.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
/*ViewGroup.MarginLayoutParams editmargin=new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.FILL_PARENT,ViewGroup.MarginLayoutParams.WRAP_CONTENT);
editmargin.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);*/
ed6.setTextColor(Color.BLACK);
ed6.setBackgroundColor(Color.WHITE);
ed6.setText("1");
tr1.addView(ed6);
EditText ed7=new EditText(inventory.this);
//ed7.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed7.setTextColor(Color.BLACK);
ed7.setBackgroundColor(Color.WHITE);
ed7.setText("2");
tr1.addView(ed7);
EditText ed8=new EditText(inventory.this);
//ed8.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed8.setTextColor(Color.BLACK);
ed8.setBackgroundColor(Color.WHITE);
ed8.setText("3");
tr1.addView(ed8);
EditText ed9=new EditText(inventory.this);
//ed9.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed9.setTextColor(Color.BLACK);
ed9.setBackgroundColor(Color.WHITE);
ed9.setText("4");
tr1.addView(ed9);
EditText ed10=new EditText(inventory.this);
//ed10.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed10.setTextColor(Color.BLACK);
ed10.setText("5");
ed10.setBackgroundColor(Color.WHITE);
tr1.addView(ed10);
EditText ed11=new EditText(inventory.this);
//ed11.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed11.setTextColor(Color.BLACK);
ed11.setText("6");
ed11.setBackgroundColor(Color.WHITE);
tr1.addView(ed11);
t1.addView(tr1);
first of all something you should know: According to the Official Android Dev Pages, Views (and a TextView derives from View) do not support the setting of Margin, but ViewGroups (such as LinearLayout, RelativeLayout etc...) do.
So what you could do is the following:
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.setMargins(5, 5, 5, 5);
TextView view = new TextView(this);
view.setLayoutParams(params);
This would set the margin for all children to 5 pixels - I tried it and it worked for me (albeit with a LinearLayout with vertical alignment). Give it a shot and let me know if I can help further :) .
Cheers,
Ready4Fajir
EDIT:
I would try with the XML below (you'd, of course, update the id's etc.). The "magic" in the xml is that it distributes all available width evenly among the TextView's (and the EditText's on the second row).
<?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="match_parent"
android:orientation="vertical">
<!-- The first "row" -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView 01"
android:id="#+id/textView01" />
<!-- Here you'd add your other five TextView's accordingly -->
</LinearLayout>
<!-- The second "row" -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView 01"
android:id="#+id/editText01" />
<!-- Here you'd add your other five EditText's accordingly -->
</LinearLayout>
</LinearLayout>
In your Java code you could then access your EditText views like:
EditText editText01 = (EditText) findViewById(R.id.editText01);
editText01.setText("1");
I have now ignored the fact that you need to create your EditText's programatically. Do you really, really need to create them in Java? (Why?)
OLD ANSWER:
If you just want to set the layout margins to your EditText view i quess you could use the setMargins(left, top, right, bottom) function call on the LayoutParams variable.
int left = 6;
int top = 12;
int right = 6;
int bottom = 6;
TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
EditText edXY = new EditText(inventory.this);
edXY.setLayoutParams(params);
If you ultimately wish to distribute all available space evenly among the six EditText views in a table row I would suggest you have a look at the following post: 2-column TableLayout with 50% exactly for each column

Categories