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.
Related
Is there any way to add new buttons dynamically to linear layout with their unique id's. Considering a predefined button to perform this action.
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button 1");
btnTag.setId(1);
row.addView(btnTag);
layout.addView(row);
Yes,you can dynamically add button to linear layout.
For Eg:
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT,AbsListView.LayoutParams.WRAP_CONTENT));
layout.setId(5000);
Random random = new Random();
//Five button
for(int i=0;i<5;i++)
{
Button button = new Button(this);
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
button.setText("Button");
button.setId(random.nextInt(5)); //generate unique number between 0 to 4 and set it as id
layout.addView(button);
}
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);
I have a LinearLayout inside a FrameLayout for tab purposes. And I'm trying to add TableRow's to the LinearLayout in the code.
LinearLayout testLayout = (LinearLayout)findViewById(R.id.testLayout);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
This gets my LinearLayout and creates a TableRow to the specifications I want. I know this part is working.
TextView textOne = new TextView(this);
textOne.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textOne.setText("One");
TextView textTwo = new TextView(this);
textTwo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textTwo.setText("Two");
Here I make my two TextViews with no problem.
tableRow.addView(textOne);
tableRow.addView(textTwo);
testLayout.addView(tableRow, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Here is where I assume everything goes wrong. What happens is it only shows the textTwo. I don't know why it's not showing both of them in order like a normal TableRow would in XML. And I repeat, this must be done in code.
Please help, thank you.
Have you imported this import android.widget.TableRow.LayoutParams
Below code works for me
TableLayout tl = (TableLayout) findViewById(R.id.spreadsheet);
TableRow tr = new TableRow(this);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
TextView tvLeft = new TextView(this);
tvLeft.setLayoutParams(lp);
tvLeft.setBackgroundColor(Color.WHITE);
tvLeft.setText("OMG");
TextView tvCenter = new TextView(this);
tvCenter.setLayoutParams(lp);
tvCenter.setBackgroundColor(Color.WHITE);
tvCenter.setText("It");
TextView tvRight = new TextView(this);
tvRight.setLayoutParams(lp);
tvRight.setBackgroundColor(Color.WHITE);
tvRight.setText("WORKED!!!");
tr.addView(tvLeft);
tr.addView(tvCenter);
tr.addView(tvRight);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
I think you want to create your TextView dynamically and should're getting error "removeView () parent." Here is a good solution for that:
TableView tlSkills = (TableView) findViewById(R.id.myTableView);
if(listSkills.size() > 0) {
TableRow tableRow = new TableRow(getContext());
int i = 0;
for (Skills s : listSkills) {
TextView textView = new TextView(getContext());
textView.setText("" + s.getName());
tableRow.addView(textView);
if(i > 0) {
tlSkills.removeView(tableRow);//this is to avoid the error I mentioned above.
}
tlSkills.addView(tableRow);
i++;
}
}
This question already has an answer here:
How can i put my buttons like that?
(1 answer)
Closed 9 years ago.
I want to make a calculator with TableLayout. I know it fits better with GridLayout but TableLayout is a must.
I try to get this design:
As you seae, the numbers(1 to 9) are in the same size and order with connect and disconnect buttons. However, in my current design, Its like that:
but button in my design look like:
This is my code:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
TextView titleView = new TextView(this);
titleView.setText("Table Layout");
titleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
layout.addView(titleView);
LinearLayout layout2 = new LinearLayout(this);
layout2.setOrientation(LinearLayout.HORIZONTAL);
layout2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
Button btnConnect = new Button(this);
btnConnect.setText("Connect");
btnConnect.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
layout2.addView(btnConnect);
TextView titleViewSpace = new TextView(this);
titleViewSpace.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
layout2.addView(titleViewSpace);
Button btnDisconnect = new Button(this);
btnDisconnect.setText("Disconnect");
btnDisconnect.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
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 make my buttons in same order and size with connect and disconnect buttons
i simply do that when i create button in loop
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);
b.setLayoutParams(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1));
tblrow.addView(b);
}
but when i put this LayoutParams:
b.setLayoutParams(new LayoutParams(0,LayoutParams.WRAP_CONTENT,1));
buttons(1 to 9) are disappered in the screen. Why? How can i overcome with this problem? How can i make button like that in the same screen?
this is all i needed:
tblLayout.setStretchAllColumns(true);
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