textview - Android text over-enlarging text size -


i'm new android , i'm working on "about" page giving me weird behavior. has text when clicked expands , collapses when clicked again. collapsed text of larger size expanded text. when click expand, text waaay larger larger expanded text , when go collapse again collapsed text keeps huge text size too. (it changes actual text content fine)

the xml textview

<textview             ...             android:textsize="@dimen/about_filler"             android:id="@+id/stuff"             android:text="@string/about_filler"             android:clickable="true"             android:onclick="collapsetoggle"             ... /> 

the method clicking calls

public void collapsetoggle(view view){     textview text = (textview) view;     if( ! text.gettext().equals(getstring(r.string.about_filler))){ //if not collapsed         text.settext(getstring(r.string.about_filler));         text.settextsize(getresources().getdimension(r.dimen.about_filler));     }      else if(text.getid() == r.id.stuff){         text.settext(getstring(r.string.about_contents_stuff));             text.settextsize(getresources().getdimension(r.dimen.about_contents));     } 

and dimen folder

... <dimen name="about_contents">20sp</dimen> <dimen name="about_filler">40sp</dimen> 

i know there million better ways implement kind of thing why happening killing me.

by default text.settextsize(float size) assumes you're passing size in sp units, it's converting sp px internally.

on other hand getresources().getdimension() returning unit in px, internally doing conversion whatever value set.

so what's happening in case

  • you've set 20sp in dimens.xml
  • getdimension() returning 100px (depending on screen)
  • settextsize() assumes you've set 100sp , resizes huuge.

to fix use other option settextsize() accepts units: text.settextsize(typedvalue.complex_unit_px, getresources().getdimension(r.dimen.about_contents));


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -