BRDroid
BRDroid

Reputation: 4388

Dynamically set height of an EditText based on the content

I have an Edit text in which i am using setText to load the data on an edit form. Currently the editText height is fixed and if the text Is larger than the editText height i can't see the full text at once. Is there a way based on the content i can dynamically set the height of an EditText.

Thanks in advance for your help and suggestions

Regards R

Upvotes: 1

Views: 3649

Answers (5)

Ramesh Kumar
Ramesh Kumar

Reputation: 1249

You can use this code for your xml file:

<EditText
   android:id="@+id/edtFirstName"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="@string/first_name"
   android:padding="5dp"
   android:textColor="@color/black"
   android:textSize="16sp" />

while your edit text contain comes it automatically fit the text size.

Do not use android:singleLine="true" or android:minLines="1" and android:maxLines="1"

Upvotes: 5

UltimateDevil
UltimateDevil

Reputation: 2847

This may help you,

Using android:layout_height ="wrap_content" and android:inputType="textMultiLine" are best approach

 <EditText
    android:id ="@+id/editText"
    android:layout_width ="match_parent"
    android:layout_height ="wrap_content"
    android:inputType="textMultiLine"
    android:hint ="Hint"
    android:scrollHorizontally="false" />

I will suggest you to use ScrollView as a parent layout of EditText

It prevents your EditText to cut off (outside from the screen) from screen when you have a larger text

Upvotes: 3

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Set android:layout_height wrap_content is best Approach .

The view should be only big enough to enclose its content .

Use DisplayMetrics for Dynamically set the height of an EditText.

    DisplayMetrics metrics = getResources().getDisplayMetrics();

    int DeviceTotalWidth = metrics.widthPixels;
    int DeviceTotalHeight = metrics.heightPixels;

How ?

if(length()>n) // n is your String length
editTEXT.getLayoutParams().height=DeviceTotalHeight*10/100;

Upvotes: 2

mehul chauhan
mehul chauhan

Reputation: 1802

Try, this one may help -:

  edittext.getLayoutParams().width=50

    et= (CustomEditText) findViewById(R.id.et);
    ViewGroup.LayoutParams lp = et.getLayoutParams();
    lp.width = 100;
    lp.height = 100;
    et.setLayoutParams(lp);

Upvotes: 1

Akhilesh Awasthi
Akhilesh Awasthi

Reputation: 2708

try setting the height of edit text as following :

android:layout_height="wrap_content"

Upvotes: 2

Related Questions