Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set width to the textview doesn't work

Tags:

android

views

I want to set the width of my text-view in code to width =119

tv.setWidth(width);
int width2 = tv.getWidth();

but I get the result

width2 =12 

I have this code in the onlayout method and when onlayout called again (I think when the text-view is actually on screen )by the android system the result will be correct

width2 =119 

I think if the width is set to fixed value you can set it again to the another value before it is drawn in the UI.

How can I know when onlayout will be called because sometimes it doesn't called again I mean after the textview is actually on screen?

like image 727
Mohammed Subhi Sheikh Quroush Avatar asked Dec 02 '13 12:12

Mohammed Subhi Sheikh Quroush


2 Answers

You have to try set the width with LayoutParams option like this :

TextView tv = new TextView(getContext());
LayoutParams params = new LayoutParams(119,LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(params);
like image 81
Harshit Rathi Avatar answered Oct 02 '22 14:10

Harshit Rathi


LayoutParams param = txtv.getLayoutParams();
param.width = 45;
txtv.setLayoutParams(param);
like image 24
Gayathiri Avatar answered Oct 02 '22 16:10

Gayathiri