setting margin of a TableRow issue - java

I have the following code, which tries to set the margin:
TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=20;
int topMargin=50;
int rightMargin=0;
int bottomMargin=50;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);
TextView name = new TextView(this);
name.setText(Html.fromHtml(venues.get(j).name + "<br><br>" + venues.get(j).getFullAddress()));
However this doesn't set the margin. What am I doing wrong here?

Edit. Whoops, my bad, TableLayout.LayoutParams is fine. Use TableRow.LayoutParams on the children of the TableRow.
I just ran this code (which is, for the most part, your code) and the margin sets correctly
TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
int leftMargin=100;
int topMargin=50;
int rightMargin=0;
int bottomMargin=50;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);
TextView tv = new TextView(this);
tv.setText("EXAMPLE ROW");
TableRow.LayoutParams textParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(textParams);
tr.addView(tv);
tlayout.addView(tr);

isn't it like so?
TableRow tr = new TableRow(Activity.this)
And I thik you'll nee a table for that row too.
Why not go for XML ?
<TableRow
android:id="#+id/tr1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableRow>

Related

Add Space between LinearLayouts

I'm trying to make a list of database items like the following..
for (int i=0; i < jArray.length(); i++)
{
JSONObject row = jArray.getJSONObject(i);
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,100));
Button btn = new Button(MainActivity.this);
//btn.setText(row.getString("subject"));
btn.setText(String.valueOf(i));
btn.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.8));
File imgFile = new File(file_path);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView iv = new ImageView(MainActivity.this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setImageBitmap(myBitmap);
iv.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.2));
ll.addView(btn);
ll.addView(iv);
LinearLayout sv = findViewById(R.id.sv_layout);
sv.addView(ll);
}
The result is like this:
But I want to have space between Linear Layouts and make Image height shorter to be equal with button height
How can I achieve this? Thank you,
Please Try this code
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
params.setMargins(0, 5, 5, 10);
ll.setLayoutParams(params);
you can add your specific margins in this line params.setMargins(0, 5, 5, 10);
Take button and image in linear layout with horizontal orientation. Set Button and Imageview height to wrap content. Now for gap between Linear Layouts give appropriate margin.
Set the margins in the layout params and pass them to your ImageView object.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.2);
int dp = getDps(8);
layoutParams.setMargins(0,dp,0,dp);
iv.setLayoutParams(layoutParams);
Function getDps():
public int getDps(int sizeInDp){
float scale = getResources().getDisplayMetrics().density;
return (int) (sizeInDp*scale + 0.5f);
}
This looks like:

Objects added to a tablerow don't show up?

I'm trying to add some EditTexts to a TableRow programmatially, yet they don't show up on the screen. If I add the EditTexts directly to the TableLayout it works fine but I do want to have them in one row afterall...
public void createPlayers(){
EditText[] editTextSpieler = new EditText[anzahl_Spieler];
TableRow tableRow = new TableRow(getActivity());
tableRow.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tableLayout.addView(tableRow);
for(int i = 0; i < anzahl_Spieler; i++){
editTextSpieler[i] = new EditText(getActivity());
editTextSpieler[i].setLayoutParams(layoutparams);
tableRow.addView(editTextSpieler[i]);
editTextSpieler[i].getLayoutParams().width = dWidth/(anzahl_Spieler + 1);
editTextSpieler[i].setHint("Name");
}
}
Thanks in advance.
Solution:
layoutparams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
specifying MATCH_PARENT and WRAP_CONTENT with TableRow.LayoutParams did the job
Can you try this change.
editTextSpieler[i].getLayoutParams().width = dWidth/(anzahl_Spieler + 1);
editTextSpieler[i].setHint("Name");
//Move to last line in the loop.
tableRow.addView(editTextSpieler[i]);

Adding second TextView replaces the first TextView in a LinearLayout

I am adding TextViews programatically to a LinearLayout. But when the second TextView is added, it seems to replace the first.
Here is the code:
LinearLayout l = (LinearLayout) findViewById(R.id.contacts_container);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
String username = object.getString("username");
String status = object.getString("status");
// create wrapper
LinearLayout wrapper = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
wrapper.setOrientation(LinearLayout.HORIZONTAL);
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());
wrapper.setPadding(padding,padding,padding,padding);
wrapper.setLayoutParams(lp);
l.addView(wrapper);
// add Imageview to wrapper
ImageView image = new ImageView(getApplicationContext());
image.setBackgroundResource(R.drawable.icon_only_dark_crop);
lp = new LinearLayout.LayoutParams((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));
lp.setMargins(0, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()), 0);
image.setLayoutParams(lp);
wrapper.addView(image);
// add linearLayout text wrapper to main wrapper
LinearLayout textWrapper = new LinearLayout(getApplicationContext());
textWrapper.setOrientation(LinearLayout.VERTICAL);
textWrapper.setPadding(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()), 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 0.9f);
textWrapper.setLayoutParams(params);
wrapper.addView(textWrapper);
// add username TextView to textWrapper
TextView usernameText = new TextView(getApplicationContext());
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
usernameText.setLayoutParams(lp);
usernameText.setText(username);
usernameText.setTextColor(Color.parseColor("#FFFFFF"));
usernameText.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
textWrapper.addView(usernameText);
// add status TextView to textWrapper
TextView statusText = new TextView(getApplicationContext());
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
usernameText.setLayoutParams(lp);
usernameText.setText(status);
usernameText.setTextColor(Color.parseColor("#FFFFFF"));
usernameText.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
textWrapper.addView(statusText);
}
It loops twice and two "wrapper" LinearLayouts get added to the main LinearLayout fine. But for each wrapper LinearLayout it should add two TextViews, but when I run the application, only the statusView displays. If I remove the statusView, the usernameView displays fine.
Why is that when the statusView is added to the wrapper, the usernameView seems to be hidden or removed?
You copy pasted the same layout variable name when initializing the second textview
use this instead
// add username TextView to textWrapper
TextView usernameText = new TextView(getApplicationContext());
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
usernameText.setLayoutParams(lp);
usernameText.setText(username);
usernameText.setTextColor(Color.parseColor("#FFFFFF"));
usernameText.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
textWrapper.addView(usernameText);`
// add status TextView to textWrapper
TextView statusText = new TextView(getApplicationContext());
lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
statusText.setLayoutParams(lp);
statusText.setText(status);
statusText.setTextColor(Color.parseColor("#FFFFFF"));
statusText.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
textWrapper.addView(statusText);
in your second textview, check the below text.
TextView **statusText** = new TextView(getApplicationContext());
**usernameText**.setLayoutParams(lp);
**usernameText**.setText(status);
**usernameText**.setTextColor(Color.parseColor("#FFFFFF"));
**usernameText**.setTextSize(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics()));
textWrapper.addView(statusText);
Probably because your textWrapper does'nt have a orientation specified. Define it as vertical or horizontal depending on your needs and it should work fine.

How to populate the TableLayout with ImageView dynamically in android jdk?

I've a TableLayout element in my main.xml:
<TableLayout android:id="#+id/TableLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TableLayout>
I don't want to hardcode the ImageView in main.xml.
How to embed the ImageView in .java ?
Hope this helps.
TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
for (int r=1; r<=rowCount; r++){
TableRow tr = new TableRow(this);
for (int c=1; c<=columnCount; c++){
ImageView im = new ImageView (this);
im.setImageDrawable(getResources().getDrawable(R.drawable.image_name));
im.setPadding(0, 0, 0, 0); //padding in each image if needed
//add here on click event etc for each image...
//...
tr.addView(im, imageWidth,imageHeight);
}
table.addView(tr);
}

How can I get an Android TableLayout to fill the screen?

I'm battling with Android's awful layout system. I'm trying to get a table to fill the screen (simple right?) but it's ridiculously hard.
I got it to work somehow in XML like this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent">
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1">
<Button android:text="A" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
<Button android:text="B" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1">
<Button android:text="C" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
<Button android:text="D" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/>
</TableRow>
However I can not get it to work in Java. I've tried a million combinations of the LayoutParams, but nothing ever works. This is the best result I have which only fills the width of the screen, not the height:
table = new TableLayout(this);
// Java. You suck.
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
table.setLayoutParams(lp); // This line has no effect! WHYYYY?!
table.setStretchAllColumns(true);
for (int r = 0; r < 2; ++r)
{
TableRow row = new TableRow(this);
for (int c = 0; c < 2; ++c)
{
Button btn = new Button(this);
btn.setText("A");
row.addView(btn);
}
table.addView(row);
}
Obviously the Android documentation is no help. Anyone have any ideas?
There are two mistakes in the above discussion.
It is possible to programatically set the weight by specifying TableLayout.LayoutParams and TableRow.LayoutParams and using the appropriate constructor, e.g.
TableLayout.LayoutParams rowInTableLp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f);
A widget must have the LayoutParams of its parent. Therefore, the rows must use TableLayout.LayoutParams.
This gives you the following working version of your initial code:
TableLayout table = new TableLayout(this);
// Java. You succeed!
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
table.setLayoutParams(lp);
table.setStretchAllColumns(true);
TableLayout.LayoutParams rowLp = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT,
1.0f);
TableRow.LayoutParams cellLp = new TableRow.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT,
1.0f);
for (int r = 0; r < 2; ++r)
{
TableRow row = new TableRow(this);
for (int c = 0; c < 2; ++c)
{
Button btn = new Button(this);
btn.setText("A");
row.addView(btn, cellLp);
}
table.addView(row, rowLp);
}
setContentView(table);
Thanks to Romain Guy's comment on Android developer's forum for the solution.
Finally worked out how to do this. Gave up on TableLayout and just used horizontal LinearLayouts inside a vertical one. The critical key is to set the weight. If you specify FILL_PARENT but with the default weight, it doesn't work:
LinearLayout buttonsView = new LinearLayout(this);
buttonsView.setOrientation(LinearLayout.VERTICAL);
for (int r = 0; r < 6; ++r)
{
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.HORIZONTAL);
for (int c = 0; c < 4; ++c)
{
Button btn = new Button(this);
btn.setText("A");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
lp.weight = 1.0f;
row.addView(btn, lp);
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
lp.weight = 1.0f;
buttonsView.addView(row, lp);
}
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
setContentView(buttonsView, lp);
Found the answer: apparently it is the layout_weight that makes it work and there is no way to set it from Java. Damnit.
See How can I get an Android TableLayout to fill the parent in landscape mode?
You never set the row or button layout parameters whereas in the posted xml you do that..change the details of the for loops to set both the row layout parameters and the button layout parameters than it should give the same result as your xml.
To set TableLayout LayoutParams, we logically expect to use TableLayout.LayoutParams class, but you will get a cast error stating that TableLayout.LayoutParams cannot be casted into FrameLayout.LayoutParams.
So, you should use FrameLayout.LayoutParams, if you want to programmatically set TableLayout properties. For example:
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(80, 0, 0, 0);
TableLayout tableLayout = (TableLayout) findViewById(R.id.header_detail);
tableLayout.setLayoutParams(layoutParams);

Categories

Resources