re-setting a TextView height programmatically
66,285
Solution 1
You should change it via LayoutParams
:
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
textView.setLayoutParams(params);
EDIT
You should not use sizes in pixels in you code, use dimensions for this:
dimens.xml:
<dimen name="text_view_height">50dp</dimen>
In code:
params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);
Solution 2
Pragmatically you can set textview height like:
private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;
Solution 3
you can dynamically set width and height of textview by
private TextView mTxtView;
private int screenWidth, screenHeight;
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
LayoutParams params = mTxtView.getLayoutParams();
params.width = screenWidth-30;
mTxtView.setLayoutParams(params);
Solution 4
the sample way for this if you want to use dimen
first, you should set size in dimen XML file.
<dimen name="text_height">50dp</dimen>
<dimen name="text_width">50dp</dimen>
and
text_l.getLayoutParams().height =
getResources().getDimensionPixelSize(R.dimen.text_height);
text_l.getLayoutParams().width =
getResources().getDimensionPixelSize(R.dimen.text_width);
Or
if you want to set just int (for example we wanna set 50dp height and 100dp width)
text_l.getLayoutParams().height = 50 * 3;
text_l.getLayoutParams().width= 100 * 3;
Solution 5
In Kotlin with DP to pixels translation
changeTextHeight.setOnClickListener { view ->
// random height for testing
val randomHeightInDP = Random().nextFloat() * (50.0f - 10.0f) + 10
// set Height in pixels
hello.layoutParams.height = convertDpToPixel(randomHeightInDP, applicationContext)
//refresh layout
hello.requestLayout()
}
Convert DP to pixels, see this post:
fun convertDpToPixel(dp: Float, context: Context): Int {
return (dp * (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)).toInt()
}
Author by
Q8yDev
Updated on November 08, 2020Comments
-
Q8yDev over 3 years
I want to reset a textView height after I have added it to the main window in the xml file.
inside a RelativeLayout,
<TextView android:id="@+id/text_l" android:layout_width="50sp" android:layout_height="50sp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginLeft="10sp" android:layout_marginTop="145dp" android:gravity="center" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#000000" > </TextView>
I just want to change it from 50 to 70:
I tried:
TextView text = (TextView)findViewById(R.id.text_l); text.setHeight(70);
but nothing changed.
-
Nantoka almost 11 yearsThe bizare thing here is: textview.setheight(...) did work on some of my test devices, even though in contrast to the documentation of the setHeight function of TextView it does NOT change the layout parameters of the view. But on one of my test devices (Xperia ray, Android 4.0.4) this worked only in landscape but not in portrait mode. Changing the height in the layout params as you suggested worked fine on all of my test devices.
-
Roisgoen over 8 yearsI prefer to do this, rather than finding the correct Layoutparams class
-
Dhruv Raval over 8 yearsthat's true @Roisgoen
-
cesards about 7 yearsIt didn't work for me. Im afraid it doesn't work in newer APIs. Once the TextView has been layered, setting height in the TextView has no effect.
-
BertKing over 6 yearsWhat calls for special attention is that if you use the textView to getLayoutParams(), it would be better to judge null?
-
BertKing over 6 yearsNote that TextView textView = new TextView(context); textView.getLayoutParame() weill be null. You should judge null before use it.
-
Milos almost 5 yearsYou helped me, its too much old but you never know :D