Width column tablelayout dynamically - android

I have a problem regarding not using XML in the TableLayout.
Someone has already asked the same as I do but the answer provided has been unable to help me.
how to set width of column dynamically in android tablelayout?
Right now I have this
TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Dynamic Button");
b.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
Button a = new Button(this);
a.setText("Dynamic Button");
a.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
a.setGravity(2);
tr.addView(a);
But I fail to see how changing
tr.setLayoutParams
Will do the job of making for example the first button column 70% and the other button the 30%

You can create an linear layout with weight and keep those two buttons inside linear layout and then set linear to your table row.
Check below code:
TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
/* Create a new row to be added. */
TableRow.LayoutParams params = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
TableRow tr = new TableRow(this);
tr.setLayoutParams(params);
params = new TableRow.LayoutParams(0,
TableRow.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
params.weight = 1;
layout.setLayoutParams(params);
layout.setBackgroundColor(Color.WHITE);
layout.setWeightSum(1);
/* Create a Button to be the row-content. */
LinearLayout.LayoutParams chiledParams = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT);
chiledParams.weight = (float) 0.7;
Button b = new Button(this);
b.setText("Button");
b.setLayoutParams(chiledParams);
/* Add Button to row. */
LinearLayout.LayoutParams chiledParams1 = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT);
chiledParams1.weight = (float) 0.3;
Button a = new Button(this);
a.setText("Button");
a.setLayoutParams(chiledParams1);
layout.addView(b);
layout.addView(a);
tr.addView(layout);
tl.addView(tr);

you need setLayoutParams to your widget like following code:
TableRow.LayoutParams tlp = new TableRow.LayoutParams(width,heigh);
b.setLayoutParams(tlp);
TableRow.LayoutParams tlp1 = new TableRow.LayoutParams(width/3,heigh/3);
a.setLayoutParams(tlp1);

Related

How to put buttons like that? [duplicate]

This question already has an answer here:
How can i put my buttons like that?
(1 answer)
Closed 9 years ago.
Hello i want to put my number buttons(1 to 9) like that
but its now like that:
This is my code:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
LinearLayout layout2 = new LinearLayout(this);
layout2.setOrientation(LinearLayout.HORIZONTAL);
layout2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
TextView titleView = new TextView(this);
titleView.setText("Table Layout");
titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(titleView);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
Button btnConnect = new Button(this);
btnConnect.setText("Connect");
btnConnect.setLayoutParams(param2);
layout2.addView(btnConnect);
TextView titleViewSpace = new TextView(this);
titleViewSpace.setLayoutParams(param2);
layout2.addView(titleViewSpace);
Button btnDisconnect = new Button(this);
btnDisconnect.setText("Disconnect");
btnDisconnect.setLayoutParams(param2);
layout2.addView(btnDisconnect);
layout.addView(layout2);
TableLayout tblLayout = new TableLayout(this);
tblLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT));
TableRow tblrow = null;
for (int i = 1; i <= 9; i++) {
if (i % 3 == 1) {
tblrow = new TableRow(this);
tblLayout.addView(tblrow);
}
Button b = new Button(this);
b.setText("" + i);
tblrow.addView(b);
}
TableRow tr = new TableRow(this);
Button btnZero = new Button(this);
btnZero.setText("0");
Button btnHash = new Button(this);
btnHash.setText("#");
Button btnStar = new Button(this);
btnStar.setText("*");
tr.addView(btnZero);
tr.addView(btnHash);
tr.addView(btnStar);
tblLayout.addView(tr);
layout.addView(tblLayout);
setContentView(layout);
In order to have above view, i put buttons this layout
new LayoutParams(0,LayoutParams.WRAP_CONTENT,1)
However, when i put this layout to buttons(1 to 9), buttons in loop disappear. What might be the solution?
You can look up weight sum and layout weight in google. These two properties will make the buttons take up the whole space of the screen.

View not showing in layout when layoutparams is set programatically

Every time I set the layout param of TextView to wrap content or fill parent it's not showing in the view, but when I set it to pixels it's working why is that?
Here is the relevant code:
items = (LinearLayout) findViewById(R.id.llmain);
tb = new TableLayout(MainActivity.this);
tb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tb.setGravity(Gravity.CENTER);
TableRow tr = new TableRow(MainActivity.this);
ImageButton ima = new ImageButton(MainActivity.this);
ImageButton imr = new ImageButton(MainActivity.this);
TextView tvin = new TextView(this);
TableRow.LayoutParams trp = new TableRow.LayoutParams();
trp.setMargins(0, 0, 0, 10);
tr.setLayoutParams(trp);
tr.setGravity(Gravity.CENTER);
ima.setBackgroundColor(Color.TRANSPARENT);
ima.setImageResource(R.drawable.add);
imr.setBackgroundColor(Color.TRANSPARENT);
imr.setImageResource(R.drawable.remove);
tvin.setWidth(100);
tvin.setHeight(45);
tvin.setGravity(Gravity.CENTER);
tvin.setText(sil[i]);
tvin.setBackgroundColor(Color.rgb(200, 0, 255));
tr.addView(ima);
tr.addView(tvin);
tr.addView(imr);
tb.addView(tr);
items.addView(tb);
I tried using this
tvin.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
and this
tvin.setWidth(LayoutParams.FILL_PARENT);
tvin.setHeight(LayoutParams.FILL_PARENT);
but none of these work.
Try:
tvin.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
and remove the tvin.setWidth and height.
You should always set the layout params of view's parent, TableRow in this case.
MATCH_PARENT makes no difference but it's a new name and you should be using it instead of FILL_PARENT.
In this Line u have to use height and width .. TableRow.LayoutParams trp = new TableRow.LayoutParams(); Like this
TableRow row = new TableRow(context);
row.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT));
Try to use the LinearLayout.LayoutParams
TextView tvin = new TextView(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
tvin.setLayoutParams(llp);

How to dynamically add components

I have to display a grid of pictures taken from the database. Depending on the screen size of the user, I want to show more or less pictures.
1) How to change the number of pictures shown dynamically according to the number of pics from the database?
1a) Which layout would be adequate?
2) If there are more pictures than that can fit on a single screen, obviously it has to be scrolled. How do I define a page wise scrolling instead of scrolling little by little, meaning after each scrolling, next page will have all new members (just like we scroll Applications in Android)
At the moment, I have a TabHost layout for the main Activity and a LinearLayout for the grid type display activity.
I am using API version 10, so GridLayout is not available.
Any suggestions would be very helpful. Thanks in advance.
1) How to change the number of pictures shown dynamically according to the number of pics from the database?
As far as this part is concerned use a for loop based on the number of pics available in your database, the only thing you need is to know the number of elements present in your database, which you then use as I have used numberOfElements here
for(int i = 1; i <= numberOfElements ; i++) {
LinearLayout lHor = new LinearLayout(this);
lHor.setOrientation(LinearLayout.HORIZONTAL);
//lHor.setBackgroundColor(Color.rgb(238, 233, 191));
// Text View
TextView tv = new TextView(this);
tv.setText("FAULT "+i);
tv.setTextSize(20);
// tv.setGravity(Gravity.CENTER);
tv.setTextColor(Color.rgb(255,255,255));
tv.setPadding(12, 12, 12, 12);
tv.setId(i);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
lHor.addView(tv); // Adding the TextView to the LinearLayout
//CheckBox
CheckBox cb = new CheckBox(this);
cb.setId(i);
cb.setTag("CheckBox");
cb.setClickable(false);
// cb.setChecked(true);
cb.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
cb.setGravity(Gravity.CENTER);
checkBoxes.add(cb);
lHor.addView(cb);
l.addView(lHor);
}
You might already know how to get the number of elements in your database, if not I used this
// Method 3: Getting total number of entries present in the database
public int getTotalNumberOfEntries() {
String[] columns = new String[]{ KEY_ROWID, KEY_TYPE, KEY_DATE};
Cursor c = myDataBase.query(DATABASE_TABLE, columns, null, null, null, null, null);
int count = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
count++;
}
c.close();
return count;
}
========== EDIT ================
You can call this method in your onCreate() method of your activity
private void setDynamicContentViewOfThisPage() {
// Defining the Scroll View and the LinearLayout
ScrollView sv = new ScrollView(this);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
sv.addView(l);
// You will need to collect data from the previous Intent :-)
TextView introduction = new TextView(this);
introduction.setText("Set Text Here");
introduction.setGravity(Gravity.LEFT);
introduction.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.addView(introduction);
Button instructionsButton = new Button(this);
instructionsButton.setTag("Some Button");
instructionsButton.setId(987654321); // Random ID set to avoid conflicts :-D
instructionsButton.setText("Click to read all the instructions");
instructionsButton.setGravity(Gravity.CENTER);
instructionsButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.addView(instructionsButton);
instructionsButton.setOnClickListener(this);
// Creates a line
TableLayout tl1 = new TableLayout(this);
View v1 = new View(this);
v1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v1.setBackgroundColor(Color.rgb(51, 51, 51));
tl1.addView(v1);
l.addView(tl1);
// Version 2 (Creating Different Layouts)
for(int i = 1; i <= 3 ; i++) {
// Creates a line
TableLayout tl2 = new TableLayout(this);
View v2 = new View(this);
v2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v2.setBackgroundColor(Color.rgb(51, 51, 51));
tl2.addView(v2);
l.addView(tl2);
LinearLayout lHor = new LinearLayout(this);
lHor.setOrientation(LinearLayout.HORIZONTAL);
//lHor.setBackgroundColor(Color.rgb(238, 233, 191));
LinearLayout lVer1 = new LinearLayout(this);
lVer1.setOrientation(LinearLayout.VERTICAL);
lVer1.setGravity(Gravity.RIGHT);
lVer1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
// Text View
TextView tv = new TextView(this);
tv.setText("TV "+i);
tv.setTextSize(20);
// tv.setGravity(Gravity.CENTER);
tv.setTextColor(Color.rgb(255,255,255));
tv.setPadding(12, 12, 12, 12);
tv.setId(i);
// tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
lVer1.addView(tv); // Adding the TextView to the LinearLayout
//CheckBox
CheckBox cb = new CheckBox(this);
cb.setClickable(false);
// cb.setChecked(true);
cb.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
cb.setGravity(Gravity.CENTER);
lVer1.addView(cb);
lHor.addView(lVer1);
LinearLayout lVer = new LinearLayout(this);
lVer.setOrientation(LinearLayout.VERTICAL);
lVer.setGravity(Gravity.RIGHT);
lVer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
Button showsomeOtherButton = new Button(this);
showsomeOtherButton.setTag("showSomeButton");
showsomeOtherButton.setId(i);
showsomeOtherButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
showsomeOtherButton.setText("View Image");
// showsomeOtherButton.setEnabled(false);
lVer.addView(showsomeOtherButton);
Button someOtherDataButton = new Button(this);
someOtherDataButton.setId(i);
someOtherDataButton.setTag("someOtherButton");
someOtherDataButton.setText("Do this action " + i);
// someOtherDataButton.setEnabled(false);
someOtherDataButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
lVer.addView(someOtherDataButton);
showsomeOtherButton.setOnClickListener(this);
someOtherDataButton.setOnClickListener(this);
lHor.addView(lVer);
l.addView(lHor);
// Creates a line
TableLayout tl3 = new TableLayout(this);
View v3 = new View(this);
v3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v3.setBackgroundColor(Color.rgb(51, 51, 51));
tl3.addView(v3);
l.addView(tl3);
}
// Creates a line
TableLayout tl3 = new TableLayout(this);
View v3 = new View(this);
v3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v3.setBackgroundColor(Color.rgb(51, 51, 51));
tl3.addView(v3);
l.addView(tl3);
Button nextPageButton = new Button(this);
nextPageButton.setTag("goToNExtPageButton");
nextPageButton.setId(98765432);
nextPageButton.setText("Go To Next Page");
nextPageButton.setGravity(Gravity.CENTER);
//nextPageButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity=Gravity.CENTER;
nextPageButton.setLayoutParams(layoutParams);
l.addView(nextPageButton);
// Set the content View to this
this.setContentView(sv);
}
}
I solved the problem by using a LinearLayout. I dynamically added views to it using:
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
TextView ChildView = new TextView(getApplicationContext());
ChildView.setText("I am the child");
parentLayout.addView(ChildView);
I loop upto the available view as suggested by Sumit.
I had to enclose the whole activity's layout into a ScrollView and it made up for the invisible children in the dynamic layout

Android - Dynamically Adding TableRow to TableLayout using an existing TableRow layout

I'm trying to adding TableRows to a TableLayout, which is working fantastically. However, I'm running into some problems with layout parameters. The spacing between the TextViews within a TableRow isn't working out like I thought it would.
My current code looks as follows.
paymentTable = (TableLayout) findViewById(R.id.paymentTable);
for(i = 0; i < cursor.getCount(); i++) {
TableRow row = new TableRow(this);
TextView payAmount = new TextView(this);
payAmount.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_AMOUNT)));
payAmount.setPadding(payAmount.getPaddingLeft(),
payAmount.getPaddingTop(),
textView.getPaddingRight(),
payAmount.getPaddingBottom());
TextView payDate = new TextView(this);
payDate.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_DATE)));
row.addView(payDate);
row.addView(payAmount);
paymentTable.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
cursor.moveToNext();
}
Afterwards, the TableLayout looks something along the lines of:
January$500
February$500
March$405
But I want it to look like:
January $500
February $500
March $405
To clarify things, I want each new TableRow and the TextViews it contains to inherit layout properties of an existing TableRow and TextView(s).
yawus this is a good tutorial will help you set the table layout
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/
this is the xml layout
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/
this is the activity code
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/8/
TableRow tableRow= new TableRow(this);
ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(row.get(0).toString());
tableRow.addView(idText);
TextView textOne = new TextView(this);
textOne.setText(row.get(1).toString());
tableRow.addView(textOne);
TextView textTwo = new TextView(this);
textTwo.setText(row.get(2).toString());
tableRow.addView(textTwo);
dataTable.addView(tableRow);
You need to add a RelativeLayout to each Table
TableRow row = new TableRow(this);
row.setId(tag);
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// as the parent of that RelativeLayout is a TableRow you must use TableRow.LayoutParams
TableRow.LayoutParams rlp = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT);
rlp.setMargins(0, 0, 0, 0);
row.addView(relativeLayout, rlp);
Then you add your TextView to the RelativeLayout. Like this
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payAmount = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_LEFT);
payAmount.setLayoutParams(lp);
relativeLayout.addView(payAmount);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView payDate = new TextView(this);
//lp.setMargins(left, top, right, bottom)
lp.setMargins(0, 0, 16, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
payDate.setLayoutParams(lp);
relativeLayout.addView(payDate);
This is the way I handle positioning interface components within a table row.

Android: Programmatically Setting width of Child View using Percentage

I've read this article:
Setting width of Child View using Percentage
So far it works great if we set the weight and width in XML but I want to do it programmatically. I've a table in my layout (XML) and I am adding rows to it programmatically. In each row, I am adding 2 Text Views. I tried doing like this:
TableLayout tl = (TableLayout)findViewById(R.id.myTable);
for(int i = 0; i < nl.getLength(); i++){
NamedNodeMap np = nl.item(i).getAttributes();
TableRow trDetails = new TableRow(this);
trDetails.setBackgroundColor(Color.BLACK);
TextView tvInfo = new TextView(this);
tvInfo.setTextColor(Color.WHITE);
tvInfo.setGravity(Gravity.LEFT);
tvInfo.setPadding(3, 3, 3, 3);
tvInfo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
tvInfo.setText("info");
TextView tvDetails = new TextView(this);
tvDetails.setTextColor(Color.WHITE);
tvDetails.setGravity(Gravity.LEFT);
tvDetails.setPadding(3, 3, 3, 3);
tvDetails.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
tvDetails.setText(np.getNamedItem("d").getNodeValue());
trDetails.addView(tvInfo, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
trDetails.addView(tvDetails, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
tl.addView(trDetails,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
but with no luck, it shows blank screen but if I do it in XML then it shows perfectly. Please help me sorting out this issue where I am doing wrong?
Thanks
You should use TableRow.LayoutParams and not Linearlayout.LayoutParams
trDetails.addView(tvInfo, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50));
trDetails.addView(tvDetails, new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50));

Categories

Resources