TextView.getWidth() = 0 ?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

TextView.getWidth() = 0 ?

Postby ploki » Thu Sep 24, 2009 12:34 pm

Hello,

I have problem with dynamic layout - with getWidth() method. Example:

//on MainActivity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. LinearLayout ll = new LinearLayout(this);
  3.  
  4. TextView tv1 = new TextView(this);
  5.  
  6. TextView tv2 = new TextView(this);
  7.  
  8.  
  9.  
  10. tv2.setText("ExmlpTxt");
  11.  
  12. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams([...]);
  13.  
  14. params.setMargin(tv2.getWidth(), 0, 0, 0); // i need value here
  15.  
  16. tv2.setLayoutParams(params);
  17.  
  18.  
  19.  
  20. tv1.setText(Integer.toString(tv2.getWidth()); //value is 0
  21.  
  22.  
  23.  
  24. ll.addView(tv2);
  25.  
  26. ll.addView(tv1);
  27.  
  28. setContentView(ll);
  29.  
  30.  
Parsed in 0.056 seconds, using GeSHi 1.0.8.4


How can i get the width of the TextView? I understand why the value is 0. Do you have any ideas?
Thanks!

//Edit 12 oct
//EOT, konic thx!
Last edited by ploki on Mon Oct 12, 2009 5:45 pm, edited 2 times in total.
ploki
Freshman
Freshman
 
Posts: 4
Joined: Thu Sep 24, 2009 12:06 pm

Top

Postby sandis84 » Thu Oct 01, 2009 8:47 am

the views dont get height or width until they are actually drawn on screen.
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby konic » Thu Oct 01, 2009 4:41 pm

Do you need to know width of View (TextView) or just the length of the text in that View?
konic
Developer
Developer
 
Posts: 32
Joined: Mon Aug 24, 2009 3:16 pm

Postby ploki » Sun Oct 11, 2009 2:17 pm

Thanks for the answers.

I need to know width of View (TextView). I must calculate the position of the label(because I align the words) and position is depends on labels length. Example:
mmm //3 letters but it is longer than
llllll //5 letter, so I can't calculate width from number of letters in the word, so I must have pixels-width.

Thanks!
ploki
Freshman
Freshman
 
Posts: 4
Joined: Thu Sep 24, 2009 12:06 pm

Postby konic » Mon Oct 12, 2009 1:41 am

ploki wrote:Thanks for the answers.
I need to know width of View (TextView). I must calculate the position of the label(because I align the words) and position is depends on labels length. Example:
mmm //3 letters but it is longer than
llllll //5 letter, so I can't calculate width from number of letters in the word, so I must have pixels-width.

I did not say "number of letters" I said "length of the text".
In fact I meant text length in pixels.
As for the View (i.e. TextVeiw), it may also contain paddings and margins in all directions.
The length of text in pixels:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. FontMetrics fm =  this.getFontMetrics(this.itsFont);
  2. int width = fm.stringWidth("Test String");
  3.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

This is from here: textview_width_of_text-t382.html
Just do some search next time.
konic
Developer
Developer
 
Posts: 32
Joined: Mon Aug 24, 2009 3:16 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests