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?
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);
LayoutParams param = txtv.getLayoutParams();
param.width = 45;
txtv.setLayoutParams(param);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With