TextView: width of text

Put your problem here if it does not fit any of the other categories.

TextView: width of text

Postby AndreySerj » Thu Dec 27, 2007 4:15 pm

Hi, How can I calculate width (not length) of text in TextView ? I need to cut a very long text in TextView
AndreySerj
Developer
Developer
 
Posts: 31
Joined: Wed Dec 26, 2007 9:57 am
Location: Minsk

Top

Postby cabernet1976 » Fri Dec 28, 2007 10:30 am

Hi,
I am not clear your meaning, want a scroller for your EditBox?
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby cabernet1976 » Fri Dec 28, 2007 10:33 am

Or maybe you want this:
textView.getText().toSting().length()
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby AndreySerj » Fri Dec 28, 2007 10:40 am

I need to cut off a too long text in my TextView (text may have a different size, width ...) and add "..." to end of text string.
textView.getText().toSting().length() - returns a length, but not a width of text.
AndreySerj
Developer
Developer
 
Posts: 31
Joined: Wed Dec 26, 2007 9:57 am
Location: Minsk

Postby tum0rc0re » Fri Dec 28, 2007 11:09 am

AndreySerj wrote:I need to cut off a too long text in my TextView (text may have a different size, width ...) and add "..." to end of text string.
textView.getText().toSting().length() - returns a length, but not a width of text.


I'm not sure, but maybe you need call method getTextSize() for your TextView, it returns the size (in pixels) of the default text size in this TextView.
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby plusminus » Fri Dec 28, 2007 4:54 pm

Hello AndreySerj,

in awt/swing we had the possibility to use the FontMetrics-Class used like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 FontMetrics fm =  this.getFontMetrics(this.itsFont);
  2.                 int width = fm.stringWidth("Test String");
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

Unfortunately I could not find the android-equivalent :(

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby cabernet1976 » Tue Jan 15, 2008 2:34 am

I don't know if this issue has been resolved, and I am also sorry for not testing it before posting it here.
I find this thread ( :arrow: http://groups.google.com/group/android- ... 76212875e2) in google android group today.

The method said as below:
Use Paint.measureText() to compute the width.
Use Paint.ascent() and Paint.descent() to compute the height.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby plusminus » Tue Jan 15, 2008 11:06 pm

Hello cabernet,

thx :)
So there the functions are hiding ^^.

So this will work, here are they all:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         float   measureText(String text)
  2.  
  3.         float   measureText(String text, int start, int end)
  4.  
  5.         float   measureText(char[] text, int index, int count)
  6.  
  7.         float   measureText(CharSequence text, int start, int end)
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


ascent() takes no parameters:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         float           ascent()
  2.  
  3.         // Return the distance above (negative) the baseline (ascent) based on the current typeface and text size.
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby m.allam » Wed Feb 10, 2010 9:51 am

how to Return the hieght of the text.
m.allam
Junior Developer
Junior Developer
 
Posts: 15
Joined: Thu Dec 17, 2009 5:02 pm

Postby android88 » Sun Mar 07, 2010 2:55 pm

m.allam wrote:how to Return the hieght of the text.


using paint.ascent and paint.descent
android88
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Jan 05, 2010 9:06 pm

Re: TextView: width of text

Postby alpesh_harsoda » Wed Jan 26, 2011 9:38 pm

Hello Friends,
how can i get the Dynamic width of label in android like we can get textview dynamic height in iphone..
Regards,
Alpesh Patel,
Logistic Infotech
http://www.logisticinfotech.com
User avatar
alpesh_harsoda
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat Sep 20, 2008 12:35 pm
Location: Rajkot,Gujarat(India)

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 19 guests