I have a TableLayout which I add rows programmatically and I'm trying to set "weight" of elements contained in rows programmatically.
I'm trying to do that by setting weightSum on the TableRow and setting "weight" of the columns using the last parameter of "LayoutParams", but nothing appears; instead if I give a fixed width to elements, code works well.
This is my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center" >
</TableLayout>
</LinearLayout>
This is the code:
public class HomeFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.home, null);
}
#Override
public void onStart() {
super.onStart();
TableLayout tl = (TableLayout)getActivity().findViewById(R.id.sample);
TableRow tr = new TableRow(getActivity());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.setGravity(Gravity.CENTER);
tr.setWeightSum(1f);
TableRow.LayoutParams lp;
TextView tv1 = new TextView(getActivity());
lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.6f);
tv1.setText("AAA");
tv1.setLayoutParams(lp);
TextView tv2 = new TextView(getActivity());
lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.4f);
tv2.setText("BBB");
tv2.setLayoutParams(lp);
tr.addView(tv1);
tr.addView(tv2);
tl.addView(tr);
}
}
Here is the updated code for setting the weightsum and column weight programmatically for a tableLayout.
TableLayout tl = (TableLayout)getActivity().findViewById(R.id.sample);
TableRow tr = new TableRow(getActivity());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.setGravity(Gravity.CENTER);
tr.setWeightSum(4); //total row weight
TableRow.LayoutParams lp;
lp.weight = 1; //column weight
TextView tv1 = new TextView(getActivity());
lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.6f);
tv1.setText("AAA");
tv1.setLayoutParams(lp);
TextView tv2 = new TextView(getActivity());
lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.4f);
tv2.setText("BBB");
tv2.setLayoutParams(lp);
tr.addView(tv1);
tr.addView(tv2);
tl.addView(tr);
Related
i wanted to add textview and edittext params by code.
here is my code
private void draw_table() {
// TODO Auto-generated method stub
TableLayout ll = (TableLayout) findViewById(R.id.input_table_2);
for (int i = 0; i < 2; i++) {
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
// setting textVIEW
textview = new TextView(this);
textview.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
//textview.setPadding(0, 0, 5, 10);
//textview.setTextAppearance(this,
// android.R.style.TextAppearance_Medium);
textview.setText("Hello");
// setting editText
edittext = new EditText(this);
edittext.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
//edittext.setEms(10);
//edittext.setHint("gpa");
//edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
row.addView(textview);
row.addView(edittext);
ll.addView(row, i);
}
}
but nothing appear on the screen. whats wrong in this code ?thanks in advance :)
Use TableRow.LayoutParams to set your widgets LayoutParams. Like this
textview.setLayoutParams(new TableRow.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
also see how add and delete rows to table layout in java programically
The following TableLayout matches exactly my requirements (it fills its parent and columns are stretched evenly):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#7434a6" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#836492" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#103456" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#958453" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#756aa9" />
</TableRow>
</TableLayout>
However, as I want to have the number of rows being configurable I started to create my layout programmatically:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Context context = getActivity();
TableLayout.LayoutParams tableParams =
new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT);
TableRow.LayoutParams rowParams =
new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 0, 1f);
TableRow.LayoutParams itemParams =
new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT, 1f);
TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(tableParams);
tableLayout.setBackgroundColor(0xff7434a6);
for (int row = 0; row < 2; row++) {
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(rowParams);
for (int column = 0; column < 2; column++) {
Random color = new Random();
int randomColor =
Color.argb(255, color.nextInt(256),
color.nextInt(256),
color.nextInt(256));
TextView textView = new TextView(context);
textView.setLayoutParams(itemParams);
textView.setBackgroundColor(randomColor);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
return tableLayout;
}
Up to a certain point this works not bad as well. Only issue I have is, that my table collapses vertically:
What is wrong here? Thought my code is doing the same as my XML. I searched and tried quite a lot but didn't get the right idea.
Try this..
LinearLayout.LayoutParams tableParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams rowParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
LinearLayout.LayoutParams itemParams =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT, 1f);
LinearLayout tableLayout = new LinearLayout(context);
tableLayout.setLayoutParams(tableParams);
tableLayout.setOrientation(LinearLayout.VERTICAL);
tableLayout.setBackgroundColor(0xff7434a6);
for (int row = 0; row < 2; row++) {
LinearLayout tableRow = new LinearLayout(context);
tableRow.setOrientation(LinearLayout.HORIZONTAL);
tableRow.setLayoutParams(rowParams);
for (int column = 0; column < 2; column++) {
Random color = new Random();
int randomColor =
Color.argb(255, color.nextInt(256),
color.nextInt(256),
color.nextInt(256));
TextView textView = new TextView(context);
textView.setLayoutParams(itemParams);
textView.setBackgroundColor(randomColor);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
OR In your rowParams change TableRow to TableLayout and try.
TableLayout.LayoutParams tableParams =
new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT, 1f);
TableLayout.LayoutParams rowParams =
new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
TableRow.LayoutParams itemParams =
new TableRow.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, 1f);
TableLayout tableLayout = new TableLayout(MainActivity.this);
tableLayout.setLayoutParams(tableParams);
tableLayout.setBackgroundColor(0xff7434a6);
for (int row = 0; row < 2; row++) {
TableRow tableRow = new TableRow(MainActivity.this);
tableRow.setLayoutParams(rowParams);
for (int column = 0; column < 2; column++) {
Random color = new Random();
int randomColor =
Color.argb(255, color.nextInt(256),
color.nextInt(256),
color.nextInt(256));
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(itemParams);
textView.setBackgroundColor(randomColor);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
How can I do the following:
Create a row in a TableLayout programmatically
Create three cells in the row
Add arbitrary text in each cell
Thanks for your help!
Try this..
TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
tableLayout.setWeightSum(3);
for (int i = 0; i < 3; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setGravity(Gravity.CENTER);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, 1.0f));
for (int j = 0; j < 3; j++) {
TextView button = new TextView(this);
final int buttonNumber = (j + i * 4);
button.setText("" + buttonNumber);
button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
tableRow.addView(button);
}
tableLayout.addView(tableRow);
}
main_lay.addView(tableLayout);
TableLayout table = (TableLayout) findViewById(R.id.TableName);
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView cell1= new TextView(getApplicationContext());
cell1.setText("one");
cell1.setTextColor(Color.BLACK);
cell1.setTextSize(16f);
cell1.setTypeface(tf);
TextView cell2= new TextView(getApplicationContext());
cell2.setText("two");
cell2.setTextColor(Color.BLACK);
cell2.setTextSize(16f);
cell2.setTypeface(tf);
TextView cell3= new TextView(getApplicationContext());
cell3.setText("three");
cell3.setTextColor(Color.BLACK);
cell3.setTextSize(16f);
cell3.setTypeface(tf);
tableRow.addView(cell1);
tableRow.addView(cell2);
tableRow.addView(cell3);
talbe.addView(tableRow , new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
If you want you can use a function like this to update your tableLayout which adds a new row with some text.
private void addRowToTableLayout(TableLayout tableLayout, String text){
TableRow tr = new TableRow(MyActivity.this);
//if you want you can customize the table row
tr.setBackgroundColor(getResources().getColor(R.color.backgroundColor));
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView textView = new TextView(MyActivity.this);
//if you want you can customize the textview also
textView.setText(text);
tr.addView(textView);
tableLayout.addView(tr, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
}
My layout.xml file looks like this:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="270dp"
android:layout_weight="0.47" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
I use the following code to create TextViews and Buttons inside a for loop:
View linearLayout = findViewById(R.id.linearLayout2);
for (int i=0 ; i<10; i++){
TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);
textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(textview);
Button button = new Button(this);
button.setText("View");
button.setId(i);
int width=90;
int height=60;
button.setLayoutParams(new LayoutParams(width, height));
((LinearLayout) linearLayout).addView(button);
}
I'm getting the Button below the TextView. Can anyone help me with replacing this LinearLayout with RelativeLayout so that I get the TextView and the Button side by side?
Modify your layout so you have a RelativeLayout instead of the LinearLayout:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="270dp"
android:layout_weight="0.47" >
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</RelativeLayout>
</ScrollView>
Then try this to make the TextViews and Buttons sit on the same row:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
for (int i = 0; i < 10; i++) {
Button button = new Button(this);
button.setText("View" + i);
button.setId(1000 + i);
int width = 90;
int height = 60;
TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(2000 + i);
if (i == 0) {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
textview.setLayoutParams(rlp2);
rl.addView(textview);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
width, height);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(rlp1);
rl.addView(button);
} else {
RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp2.addRule(RelativeLayout.BELOW, button.getId() - 1);
textview.setLayoutParams(rlp2);
rl.addView(textview);
RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
width, height);
rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlp1.addRule(RelativeLayout.BELOW, button.getId() - 1);
button.setLayoutParams(rlp1);
rl.addView(button);
}
}
View linearLayout = findViewById(R.id.linearLayout2);
for (int i=0 ; i<10; i++){
TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);
textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(textview);
Button button = new Button(this);
button.setText("View");
button.setId(i);
int width=90;
int height=60;
button.setLayoutParams(new LayoutParams(width, height));
((LinearLayout) linearLayout).addView(button);
}
replace the above with below code::
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout2);
for (int i=0 ; i<10; i++){
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.HORIZONTAL);
TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);
l.addView(textview);
Button button = new Button(this);
button.setText("View");
button.setId(i);
button.setWidth(90);
button.setHeight(60);
l.addView(button);
linearLayout.addView(l);//if you want you can layout params linearlayout
}
try this solution::;
have a new xml file as below in res/layout of your project.
Save this as linear.xml
< LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/text"
android:layout_width=" wrap_content "
android:layout_height=" wrap_content " />
<Button android:id="#+id/button"
android:layout_width="90dp"
android:layout_height="60dp" />
</ LinearLayout >
and the below code:::
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout2);
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
for (int i=0 ; i<10; i++){
View view = inflater.inflate(R.layout.linear,null);
view.findViewById(R.id.text).setText("Text view" + i);
view.findViewById(R.id.text).setTextColor(Color.BLUE);
view.findViewById(R.id.button).setText(“View");
view.findViewById(R.id.button).setTextColor(Color.BLUE);
linearLayout.addView(view);
}
You are defining LinearLayout with android:orientation="vertical", so it will tend to be one below another.
Add your textview and button to individual linear layouts, with orientation as horizontal, and add this linearLayout to the original vertical LinearLayout.
Edit: Try the following:
View linearLayout = findViewById(R.id.linearLayout2);
for (int i=0 ; i<10; i++){
View indiLyt = new View(this);
indiLyt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
indiLyt.setOrientation(LinearLayout.HORIZONTAL);
TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);
textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
((LinearLayout) indiLyt).addView(textview);
Button button = new Button(this);
button.setText("View");
button.setId(i);
int width=90;
int height=60;
button.setLayoutParams(new LayoutParams(width, height));
((LinearLayout) indiLyt).addView(button);
((LinearLayout) linearLayout).addView(indiLyt);
}
I'm trying to create a TableLayout dynamically which has all its rows stretched like shown in this tutorial. I've accomplished it via XML, but I'd like to do it from the Activity. Here is the code I've tried so far without success:
public View getTableWithAllRowsStretchedView() {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
tableLayout.setWeightSum(4);
for (int i = 0; i < 4; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setGravity(Gravity.CENTER);
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, 1.0f));
for (int j = 0; j < 4; j++) {
Button button = new Button(this);
final int buttonNumber = (j + i * 4);
button.setText("" + buttonNumber);
button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
tableRow.addView(button);
}
tableLayout.addView(tableRow);
}
linearLayout.addView(tableLayout);
return linearLayout;
}
Thank you very much in advance.
EDIT
Screenshot of what I have
what I expect
Your rows need the parent's LayoutParams:
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.FILL_PARENT, 1.0f));
Luksprog's answer is correct .
for Stretch in horizontal , do this :
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));
for Stretch in vertical , do this :
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.FILL_PARENT, 1.0f));
for Stretch in width and height layout , do this :
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.FILL_PARENT, 1.0f));