Zolf
asked on
Android TableLayout issue
Hello there,
I have a tableLayout in my activity which is working fine as required.but i have some question.
1. do i need to setID for each of the views in my table row.
2. how do i get the currect size of the button in my table row.there is a method called getDrawingRect(),can somebody tell me how to use it to return the button details.
3. what is this Layout.Params function.
please see my attached code.
cheers zolf
Order.java
I have a tableLayout in my activity which is working fine as required.but i have some question.
1. do i need to setID for each of the views in my table row.
2. how do i get the currect size of the button in my table row.there is a method called getDrawingRect(),can somebody tell me how to use it to return the button details.
3. what is this Layout.Params function.
please see my attached code.
cheers zolf
Order.java
ASKER
santhanasamy,
Thanks a lot for your feedback.it was very helpful.
Regarding question 1 i.e setId. Do i remove setid from my code.then when i click on the delete button how can i remove that row from the table.i dont understand this.can i set id to my table rows.can i also get to know the row id.can you please help me with reference to my code.i will appreciate your help
Thanks a lot for your feedback.it was very helpful.
Regarding question 1 i.e setId. Do i remove setid from my code.then when i click on the delete button how can i remove that row from the table.i dont understand this.can i set id to my table rows.can i also get to know the row id.can you please help me with reference to my code.i will appreciate your help
Hi zolf,
If you can able to ensure that the id you are setting to the TableRow is unique then their is no problem to set an id to the TableRow view.
I have tried your sample code. Add this piece of code ...
Button button = new Button(this);
button .setOnClickListener(new OnClickListener()){
public void onClick(View aView){
orderTable.removeView(tr);
}
});
Here i am removing the tr view from the orderTable
If you can able to ensure that the id you are setting to the TableRow is unique then their is no problem to set an id to the TableRow view.
I have tried your sample code. Add this piece of code ...
Button button = new Button(this);
button .setOnClickListener(new OnClickListener()){
public void onClick(View aView){
orderTable.removeView(tr);
}
});
Here i am removing the tr view from the orderTable
ASKER
cheers!! appreciate your help.
one last issue i have is with the header spacing of the table.please see the shot i have attahced. how can i set the size of the headers. i tried using setwidth but it dows not seem to be working. am i missing something.
by the way the Layout.Param is only used when you set the widgets from java code.correct??
also the set the size of the button, i have to set its parent size i.e the parent of button is TableRow.correct??
device-2012-03-31-093617.png
one last issue i have is with the header spacing of the table.please see the shot i have attahced. how can i set the size of the headers. i tried using setwidth but it dows not seem to be working. am i missing something.
by the way the Layout.Param is only used when you set the widgets from java code.correct??
also the set the size of the button, i have to set its parent size i.e the parent of button is TableRow.correct??
device-2012-03-31-093617.png
ASKER
santhanasamy,
the delete row thingy is not working as expected. it deletes only the last row,no matter which delete button i clcik in the table row.please have a look at my code.
Order.java
the delete row thingy is not working as expected. it deletes only the last row,no matter which delete button i clcik in the table row.please have a look at my code.
Order.java
Hi zolf,
1) Delete row thingy is not working as expected
For this instead of creating this ( final TableRow tr = new TableRow(this); ) tr as class level variable just declare it as method local variable...
2) One last issue i have is with the header spacing of the table
Change the layout params as mentioned below.....( I have changed for 1 or 2 view in the below code snippet.. You need to change it for all the views in the TableRow)
private void createRow(int qty2, String productSelected2)
{
TableRow.LayoutParams lRowParam = new TableRow.LayoutParams( 0, 40, 1.0f);
final TableRow tr = new TableRow(this);
//Don't set Layout param here
//LayoutParams lp = new LayoutParams(LayoutParams. FILL_PAREN T, LayoutParams.WRAP_CONTENT) ;
//tr.setLayoutParams(lp);
.......................... ...
.......................... ...
TextView labelQty = new TextView(this);
labelQty .setLayoutParams(lRowParam );
labelQty .setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL) ;
labelQty.setText(Integer.t oString(qt y));
labelQty.setTextColor(Colo r.BLACK);
tr.addView(labelQty);
Button delete_Button = new Button(this);
delete_Button .setLayoutParams(lRowParam );
delete_Button.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL) ;
delete_Button.setText("Del ete");
delete_Button.setTextSize( 10);
delete_Button.setHeight(5) ;
tr.addView(delete_Button);
..................
}
public void createTableHeader()
{
TableRow.LayoutParams lRowParam = new TableRow.LayoutParams( 0, 30, 1.0f);
TableRow colHeader = new TableRow(this);
colHeader.setBackgroundCol or(Color.G RAY);
// colHeader.setLayoutParams( new LayoutParams(LayoutParams. FILL_PAREN T,LayoutPa rams.WRAP_ CONTENT)); //Don't set layout param here
TextView label_srno = new TextView(this);
label_srno.setLayoutParams ();
label_srno.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL) ;
// label_srno.setId(20);
label_srno.setText("Sr.No" );
label_srno.setBackgroundCo lor(Color. BLUE);
label_srno.setTextColor(Co lor.WHITE) ;
// label_srno.setPadding(5, 5, 5, 5);
colHeader.addView(label_sr no);// add the column to the table row here
.......................
.......................
}
1) Delete row thingy is not working as expected
For this instead of creating this ( final TableRow tr = new TableRow(this); ) tr as class level variable just declare it as method local variable...
2) One last issue i have is with the header spacing of the table
Change the layout params as mentioned below.....( I have changed for 1 or 2 view in the below code snippet.. You need to change it for all the views in the TableRow)
private void createRow(int qty2, String productSelected2)
{
TableRow.LayoutParams lRowParam = new TableRow.LayoutParams( 0, 40, 1.0f);
final TableRow tr = new TableRow(this);
//Don't set Layout param here
//LayoutParams lp = new LayoutParams(LayoutParams.
//tr.setLayoutParams(lp);
..........................
..........................
TextView labelQty = new TextView(this);
labelQty .setLayoutParams(lRowParam
labelQty .setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL)
labelQty.setText(Integer.t
labelQty.setTextColor(Colo
tr.addView(labelQty);
Button delete_Button = new Button(this);
delete_Button .setLayoutParams(lRowParam
delete_Button.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL)
delete_Button.setText("Del
delete_Button.setTextSize(
delete_Button.setHeight(5)
tr.addView(delete_Button);
..................
}
public void createTableHeader()
{
TableRow.LayoutParams lRowParam = new TableRow.LayoutParams( 0, 30, 1.0f);
TableRow colHeader = new TableRow(this);
colHeader.setBackgroundCol
// colHeader.setLayoutParams(
TextView label_srno = new TextView(this);
label_srno.setLayoutParams
label_srno.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL)
// label_srno.setId(20);
label_srno.setText("Sr.No"
label_srno.setBackgroundCo
label_srno.setTextColor(Co
// label_srno.setPadding(5, 5, 5, 5);
colHeader.addView(label_sr
.......................
.......................
}
ASKER
santhanasamy,
cheers mate.
can you please tell me about this code
labelQty .setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL) ;
why do we need both of these in the setGravity
also
TableRow.LayoutParams lRowParam = new TableRow.LayoutParams( 0, 30, 1.0f);
what does 1.0f mean
cheers mate.
can you please tell me about this code
labelQty .setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL)
why do we need both of these in the setGravity
also
TableRow.LayoutParams lRowParam = new TableRow.LayoutParams( 0, 30, 1.0f);
what does 1.0f mean
Hi zolf,
1) why do we need both of these in the setGravity
In a row , each items wiill be aligned vertically & horizondally center.( It is used to align text With in a Textview in vertically & horizontally center)
2) what does 1.0f mean
(TableRow.LayoutParams lRowParam = new TableRow.LayoutParams( 0, 30, 1.0f);)
Internally TableRow is a Linear layout( It has extended LinearLayout). So we can set weight parameter to the child view.
So here i am trying to give equvalent width to each items in a Row.
1.0f is - Layout Weight
1) why do we need both of these in the setGravity
In a row , each items wiill be aligned vertically & horizondally center.( It is used to align text With in a Textview in vertically & horizontally center)
2) what does 1.0f mean
(TableRow.LayoutParams lRowParam = new TableRow.LayoutParams( 0, 30, 1.0f);)
Internally TableRow is a Linear layout( It has extended LinearLayout). So we can set weight parameter to the child view.
So here i am trying to give equvalent width to each items in a Row.
1.0f is - Layout Weight
ASKER
can you please tell me how can i reduce the size of the button so that it does not occupy so much space and also reduce the width of the rows
ASKER
the view looks like this now
4-2-2012-10-53-18-AM.gif
4-2-2012-10-53-18-AM.gif
ASKER
cheers mate for your comments.
>>So here i am trying to give equvalent width to each items in a Row.
what if i want to give more space to the name col and less width to the sr.no since this col will not have more then 10 digit,but the name come can have long names
>>So here i am trying to give equvalent width to each items in a Row.
what if i want to give more space to the name col and less width to the sr.no since this col will not have more then 10 digit,but the name come can have long names
Hi Zolf,
Set different layout params according to your requirment
Button delete_Button = new Button(this);
lRowParam = new TableRow.LayoutParams( 0, 40, 0.5f);
delete_Button .setLayoutParams(lRowParam );
delete_Button.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL) ;
delete_Button.setText("Del ete");
delete_Button.setTextSize( 10);
delete_Button.setHeight(5) ;
tr.addView(delete_Button);
Set different layout params according to your requirment
Button delete_Button = new Button(this);
lRowParam = new TableRow.LayoutParams( 0, 40, 0.5f);
delete_Button .setLayoutParams(lRowParam
delete_Button.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL)
delete_Button.setText("Del
delete_Button.setTextSize(
delete_Button.setHeight(5)
tr.addView(delete_Button);
Hi Zolf,
Create individual layout params to eachone of the TableRow elements
some thig like this ......
TableRow.LayoutParams lRowParamSrNo = new TableRow.LayoutParams( 0, 40, 0.5f);
TableRow.LayoutParams lRowParamName = new TableRow.LayoutParams( 0, 40, 2.0f);
TableRow.LayoutParams lRowParamQty = new TableRow.LayoutParams( 0, 40, 0.5f);
TableRow.LayoutParams lRowParamEdit = new TableRow.LayoutParams( 0, 40, 0.5f);
Create individual layout params to eachone of the TableRow elements
some thig like this ......
TableRow.LayoutParams lRowParamSrNo = new TableRow.LayoutParams( 0, 40, 0.5f);
TableRow.LayoutParams lRowParamName = new TableRow.LayoutParams( 0, 40, 2.0f);
TableRow.LayoutParams lRowParamQty = new TableRow.LayoutParams( 0, 40, 0.5f);
TableRow.LayoutParams lRowParamEdit = new TableRow.LayoutParams( 0, 40, 0.5f);
ASKER
thanks for your comment. these values are in pixill or dip.
by the way do i also have to set the text size to smaller size to be able to reduce the height of the col headers or no?
by the way do i also have to set the text size to smaller size to be able to reduce the height of the col headers or no?
Hi Zolf,
Yes ..These value are pixels...
Do i also have to set the text size to smaller size to be able to reduce the height of the col headers or no?
By default views will have minimum width and height....
I guess if i create a view bigger than that size then it will reflect when i try to reduce the text size....
Yes ..These value are pixels...
Do i also have to set the text size to smaller size to be able to reduce the height of the col headers or no?
By default views will have minimum width and height....
I guess if i create a view bigger than that size then it will reflect when i try to reduce the text size....
ASKER
cheers mate!!
one last question.
in this code
TableRow.LayoutParams lRowParamName = new TableRow.LayoutParams(0, 40, 2.0f);
why do we set the width to 0 and layout weight to 2.0f and some place 1.0f
one last question.
in this code
TableRow.LayoutParams lRowParamName = new TableRow.LayoutParams(0, 40, 2.0f);
why do we set the width to 0 and layout weight to 2.0f and some place 1.0f
ASKER
santhanasamy
if you see my scren shot,the header and row data are not aligned.also when i play around with the button layout sixe.it does not reduce in size. what am i doing wrong.please help.
02-Apr-12-11-57-40-AM.gif
Order.java
if you see my scren shot,the header and row data are not aligned.also when i play around with the button layout sixe.it does not reduce in size. what am i doing wrong.please help.
02-Apr-12-11-57-40-AM.gif
Order.java
ASKER
>>Yes ..These value are pixels...
can we set them in dp so that we have the same view on different devices
can we set them in dp so that we have the same view on different devices
ASKER
i have attached my code for the col header sr. no. please see the image i have attached to understand what i am tryin to achieve. i want to reduct the width of the sr. no but dont seem to get this. i have given the width a size 0.
by the way is there a relation between the size of the text and the view. i mean i set the text size to 8 and also set the height of the view to 8,thinking they will have same height,but it did not work that way.can you please explain.appreicate your help.
TextView label_srno = new TextView(this);
TableRow.LayoutParams lRowParamSrNo = new TableRow.LayoutParams(0, 20, 0);
label_srno.setLayoutParams (lRowParam SrNo);
label_srno.setGravity(Grav ity.CENTER _VERTICAL | Gravity.CENTER_HORIZONTAL) ;
label_srno.setText("Sr.No" );
label_srno.setTextSize(8);
label_srno.setBackgroundCo lor(Color. BLUE);
label_srno.setTextColor(Co lor.WHITE) ;
colHeader.addView(label_sr no);
a1.gif
by the way is there a relation between the size of the text and the view. i mean i set the text size to 8 and also set the height of the view to 8,thinking they will have same height,but it did not work that way.can you please explain.appreicate your help.
TextView label_srno = new TextView(this);
TableRow.LayoutParams lRowParamSrNo = new TableRow.LayoutParams(0, 20, 0);
label_srno.setLayoutParams
label_srno.setGravity(Grav
label_srno.setText("Sr.No"
label_srno.setTextSize(8);
label_srno.setBackgroundCo
label_srno.setTextColor(Co
colHeader.addView(label_sr
a1.gif
Hi Zolf
Yes we can...
float mDensityFactor = mContext.getResources().ge tDisplayMe trics().de nsity;
int height = mDensityFactor * height;
Use this code to set the height in device independent way....
Yes we can...
float mDensityFactor = mContext.getResources().ge
int height = mDensityFactor * height;
Use this code to set the height in device independent way....
ASKER
thanks for your feedback.
what is the mContext.
also can you please comment on my other questions.
what is the mContext.
also can you please comment on my other questions.
Hi Zolf
mContext - context of the activity;
If you increase the text size it will affect the layout of the TextView. I tested this aby setting text size as 20.
I did n't add any text size to the textview. i am using default text size....
Change you layout params as i mentioned below...Following sizes are perfectly working to me....
TableRow.LayoutParams lRowParamSrNo = new TableRow.LayoutParams( 0, 40, 0.7f);
TableRow.LayoutParams lRowParamName = new TableRow.LayoutParams( 0, 40, 2.0f);
TableRow.LayoutParams lRowParamQty = new TableRow.LayoutParams( 0, 40, 1.0f);
TableRow.LayoutParams lRowParamEdit = new TableRow.LayoutParams( 0, 40, 0.8f);
Apply these layout params to both header and each row...
mContext - context of the activity;
If you increase the text size it will affect the layout of the TextView. I tested this aby setting text size as 20.
I did n't add any text size to the textview. i am using default text size....
Change you layout params as i mentioned below...Following sizes are perfectly working to me....
TableRow.LayoutParams lRowParamSrNo = new TableRow.LayoutParams( 0, 40, 0.7f);
TableRow.LayoutParams lRowParamName = new TableRow.LayoutParams( 0, 40, 2.0f);
TableRow.LayoutParams lRowParamQty = new TableRow.LayoutParams( 0, 40, 1.0f);
TableRow.LayoutParams lRowParamEdit = new TableRow.LayoutParams( 0, 40, 0.8f);
Apply these layout params to both header and each row...
ASKER
santhanasamy
thanks for your comments. i added those values but the button view takes a lot of space and the row values are not aligned properly below their headings. i have attached a screenshot to help you understand.
a2.gif
thanks for your comments. i added those values but the button view takes a lot of space and the row values are not aligned properly below their headings. i have attached a screenshot to help you understand.
a2.gif
ASKER
and here is the code
Order.java
Order.java
Hi Zolf,
Please check your code you are not setting proper layout param to delete_Button
Button delete_Button = new Button(this);
TableRow.LayoutParams lRowParamEdit = new TableRow.LayoutParams(0, 40, 0.8f);
// labelQty.setLayoutParams(l RowParamEd it);
delete_Button .setLayoutParams(lRowParam Edit);
delete_Button.setGravity(G ravity.CEN TER_VERTIC AL | Gravity.CENTER_HORIZONTAL) ;
delete_Button.setText("Del ete");
tr.addView(delete_Button);
Please check your code you are not setting proper layout param to delete_Button
Button delete_Button = new Button(this);
TableRow.LayoutParams lRowParamEdit = new TableRow.LayoutParams(0, 40, 0.8f);
// labelQty.setLayoutParams(l
delete_Button .setLayoutParams(lRowParam
delete_Button.setGravity(G
delete_Button.setText("Del
tr.addView(delete_Button);
ASKER
cheers mate,thanks for correcting me.
by the way now the delete button is showing perfectly well,but the text on the delete is not showing properly. do i just set the Text Size to small value say 8 to solv this issue. or you would have used some other technique
by the way now the delete button is showing perfectly well,but the text on the delete is not showing properly. do i just set the Text Size to small value say 8 to solv this issue. or you would have used some other technique
ASKER
Hi Zolf,
yes, I used text size as 10 (delete_Button.setTextSize (10);)
yes, I used text size as 10 (delete_Button.setTextSize
ASKER
but it does not show the text completely as i have shown in the screenshot
ASKER
please help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot
1. Do i need to setID for each of the views in my table row.
TableRow tr = new TableRow(this);
tr.setId()
Through this method we can set the unique identifier to a view. You shoudl ensure that the id is unique in the entire view hierarchy
I guess you are asking this because you want to delete the row while clicking the delete button...
Instead of that you can simply remove the row from the parent table
final TableRow tr = new TableRow(this);
Button button = new Button(this);
button .setOnClickListener(new OnClickListener()){
public void onClick(View aView){
orderTable.removeView(tr);
});
2) How do i get the currect size of the button in my table row
The geometry of a view is that of a rectangle.
A view has a location, expressed as a pair of left and top coordinates,
and two dimensions, expressed as a width and a height.
The unit for location and dimensions is the pixel.
It is possible to retrieve the location of a view by invoking the methods
getLeft() - Returns the left, or X, coordinate of the rectangle representing the view.
getTop() - Returns the top, or Y, coordinate of the rectangle representing the view.
getRight() and getBottom() - These methods return the coordinates of the right and bottom edges of the rectangle representing the view.
calling getRight() is similar to the following computation: getLeft() + getWidth()
These methods both return the location of the view relative to its parent. For instance, when getLeft() returns 20, that means the view is located 20 pixels to the right of the left edge of its direct parent.
3. what is this Layout.Params function
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
LayoutParams are used by views to tell their parents how they want to be laid out.
http://developer.android.com/guide/topics/ui/declaring-layout.html
Every ViewGroup class implements a nested class that extends ViewGroup.LayoutParams. This subclass contains property types that define the size and position for each child view, as appropriate for the view group.
When an Activity receives focus, it will be requested to draw its layout.
Drawing the layout/View/ViewGroup is a two pass process
1) Layout Pass
2) Measure Pass
The measure pass uses two classes to communicate dimensions. The View.MeasureSpec class is used by Views to tell their parents how they want to be measured and positioned. The base LayoutParams class just describes how big the View wants to be for both width and height.
For more deatils about view drawing and LayoutParams read this doc..
http://developer.android.com/guide/topics/ui/how-android-draws.html