Solved

Android TableLayout issue

Posted on 2012-03-30
32
611 Views
Last Modified: 2012-04-03
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
0
Comment
Question by:zolf
  • 20
  • 12
32 Comments
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37786352
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
 

Author Comment

by:zolf
ID: 37788216
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
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37788267
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
 

Author Comment

by:zolf
ID: 37790017
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
 

Author Comment

by:zolf
ID: 37790175
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
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37794557
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
 

Author Comment

by:zolf
ID: 37794669
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
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37794685
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
 

Author Comment

by:zolf
ID: 37794708
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
 

Author Comment

by:zolf
ID: 37794711
the view looks like this now
4-2-2012-10-53-18-AM.gif
0
 

Author Comment

by:zolf
ID: 37794719
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
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37794731
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
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37794741
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
 

Author Comment

by:zolf
ID: 37794775
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
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37794793
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
 

Author Comment

by:zolf
ID: 37794804
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:zolf
ID: 37794824
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
 

Author Comment

by:zolf
ID: 37794840
>>Yes ..These value are pixels...
can we set them in dp so that we have the same view on different devices
0
 

Author Comment

by:zolf
ID: 37795026
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
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37795115
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
 

Author Comment

by:zolf
ID: 37795302
thanks for your feedback.
what is the mContext.

also can you please comment on my other questions.
0
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37795650
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
 

Author Comment

by:zolf
ID: 37799582
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
 

Author Comment

by:zolf
ID: 37799586
and here is the code
Order.java
0
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37799603
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
 

Author Comment

by:zolf
ID: 37799640
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
 

Author Comment

by:zolf
ID: 37799644
delete_Button.setTextSize(10);

please see shot
a3.gif
0
 
LVL 8

Expert Comment

by:santhanasamy
ID: 37799705
Hi Zolf,

yes, I used text size as 10 (delete_Button.setTextSize(10);)
0
 

Author Comment

by:zolf
ID: 37799718
but it does not show the text completely as i have shown in the screenshot
0
 

Author Comment

by:zolf
ID: 37799866
please help
0
 
LVL 8

Accepted Solution

by:
santhanasamy earned 500 total points
ID: 37800220
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
 

Author Closing Comment

by:zolf
ID: 37800486
Thanks a lot
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
android wifi service info 12 163
change to a different email 5 37
VOIP client on iOS/Android 6 55
Android drivers on W7 or W10 7 81
Android SDK is used for creating an android software application. It includes document and tools which are necessary to develop applications with java. In fact Android SDK is harder than other software infrastructure, because there is a virtual devi…
Introduction Everyone is using a smartphone these days. Contacts, messages, applications, documents, bookmarks, pictures, videos and other stuff which you created and are present only on your device come under the umbrella called “Personal Data”.…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now