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
zolfAsked:
Who is Participating?
 
SanthanaConnect With a Mentor Technical LeadCommented:
Hi Zolf,

I am checking this in mdpi device. It is perfectly working to me.

Button delete_Button = new Button(this);
TableRow.LayoutParams lRowParamEdit = new TableRow.LayoutParams(0, 40, 0.8f);
delete_Button .setLayoutParams(lRowParamEdit);
delete_Button.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
delete_Button.setText("Delete");
delete_Button.setTextSize(10);
tr.addView(delete_Button);

Still if you face issues...just increate the height of the Button
some thing like this
TableRow.LayoutParams lRowParamEdit = new TableRow.LayoutParams(0, 50, 0.8f);
0
 
SanthanaTechnical LeadCommented:
Hi  zolf,

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
0
 
zolfAuthor Commented:
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
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
SanthanaTechnical LeadCommented:
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
0
 
zolfAuthor Commented:
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
0
 
zolfAuthor Commented:
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
0
 
SanthanaTechnical LeadCommented:
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_PARENT, 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.toString(qty));
   labelQty.setTextColor(Color.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("Delete");
   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.setBackgroundColor(Color.GRAY);
   //   colHeader.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.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.setBackgroundColor(Color.BLUE);
  label_srno.setTextColor(Color.WHITE);
  //  label_srno.setPadding(5, 5, 5, 5);  
  colHeader.addView(label_srno);// add the column to the table row here
   .......................
   .......................
}
0
 
zolfAuthor Commented:
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
0
 
SanthanaTechnical LeadCommented:
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
0
 
zolfAuthor Commented:
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
0
 
zolfAuthor Commented:
the view looks like this now
4-2-2012-10-53-18-AM.gif
0
 
zolfAuthor Commented:
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
0
 
SanthanaTechnical LeadCommented:
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("Delete");
   delete_Button.setTextSize(10);
   delete_Button.setHeight(5);
   tr.addView(delete_Button);
0
 
SanthanaTechnical LeadCommented:
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);
0
 
zolfAuthor Commented:
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?
0
 
SanthanaTechnical LeadCommented:
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....
0
 
zolfAuthor Commented:
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
0
 
zolfAuthor Commented:
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
0
 
zolfAuthor Commented:
>>Yes ..These value are pixels...
can we set them in dp so that we have the same view on different devices
0
 
zolfAuthor Commented:
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(lRowParamSrNo);
            label_srno.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
            label_srno.setText("Sr.No");
            label_srno.setTextSize(8);
            label_srno.setBackgroundColor(Color.BLUE);
            label_srno.setTextColor(Color.WHITE);
            colHeader.addView(label_srno);
a1.gif
0
 
SanthanaTechnical LeadCommented:
Hi Zolf

Yes we can...

float mDensityFactor = mContext.getResources().getDisplayMetrics().density;
int height = mDensityFactor * height;
Use this code to set the height in device independent way....
0
 
zolfAuthor Commented:
thanks for your feedback.
what is the mContext.

also can you please comment on my other questions.
0
 
SanthanaTechnical LeadCommented:
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...
0
 
zolfAuthor Commented:
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
0
 
zolfAuthor Commented:
and here is the code
Order.java
0
 
SanthanaTechnical LeadCommented:
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(lRowParamEdit);          
            delete_Button .setLayoutParams(lRowParamEdit);                
           delete_Button.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
            delete_Button.setText("Delete");
            tr.addView(delete_Button);
0
 
zolfAuthor Commented:
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
0
 
zolfAuthor Commented:
delete_Button.setTextSize(10);

please see shot
a3.gif
0
 
SanthanaTechnical LeadCommented:
Hi Zolf,

yes, I used text size as 10 (delete_Button.setTextSize(10);)
0
 
zolfAuthor Commented:
but it does not show the text completely as i have shown in the screenshot
0
 
zolfAuthor Commented:
please help
0
 
zolfAuthor Commented:
Thanks a lot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.