Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

re-setting a TextView height programmatically

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.

like image 487
Q8yDev Avatar asked Feb 07 '12 11:02

Q8yDev


People also ask

How do I Auto Resize TextView?

To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.

How do I update TextView?

If you have a new text to set to the TextView , just call textView. setText(newText) , where newText is the updated text. Call this method whenever newText has changed.

Can we change the text in TextView?

TextView tv1 = (TextView)findViewById(R. id. textView1); tv1. setText("Hello"); setContentView(tv1);


2 Answers

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);
like image 179
Jin35 Avatar answered Oct 22 '22 18:10

Jin35


Pragmatically you can set textview height like:

private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;
like image 34
Dhruv Raval Avatar answered Oct 22 '22 16:10

Dhruv Raval