Adjust EditText to font size

31,642

Solution 1

Check this

<EditText 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:id="@+id/editText1" 
    android:textSize="5sp"
    android:inputType="textMultiLine"
    android:text="hello h r u hello  h r u hello  h r u hello  h r u hello  h r u "
></EditText>

Solution 2

set edit text height as wrap_content

android:layout_height="wrap_content"

Solution 3

You are providing 20 dp constraint to the height of EditText. It you want the textView to adjust the height in regard of Text size use

android:layout_height="wrap_content"

Also by default single line is false. Which ensures multiple line true.

Share:
31,642
Jakub Dyszkiewicz
Author by

Jakub Dyszkiewicz

Updated on July 09, 2020

Comments

  • Jakub Dyszkiewicz
    Jakub Dyszkiewicz almost 4 years

    I'm wondering how to adjust height of edit text to te font size. I mean editText is always same hight no matter if i change fontSize to 5sp or leave default. Of course i could use something like this:

    <EditText
             android:id="@+id/msgInput"
             android:layout_width="wrap_content"
             android:layout_height="20dp"
             android:inputType="textMultiLine"
             android:textSize="14sp"
             android:hint="@string/hintMsgInput">
    </EditText>
    

    but i also want to stretch if there are more than 1 line. How can i do it?

    [EDIT]:

    Ok,i solved it in this way:

    msgInput = (EditText) findViewById(R.id.msgInput);
            msgInput.addTextChangedListener(new TextWatcher()
            {
    
                public void afterTextChanged(Editable s) {
                    if (msgInput.getLineCount() > 1)
                    {
                        msgInput.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
                    }
                    else
                    {
                        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
                        msgInput.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, height));
                    }
                }
    
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {         
                }
    
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {         
                }
    
            });
    
    • Jakub Dyszkiewicz
      Jakub Dyszkiewicz over 12 years
      Ok, maybe example will help dl.dropbox.com/u/18780140/edittexts.png of course there is layout_height="wrap_content" everywhere. Question was why the "10sp editText" has the same height as default one
  • Jakub Dyszkiewicz
    Jakub Dyszkiewicz over 12 years
    What i descriped it was when i use wrap_content so it's not the solution to me
  • Jakub Dyszkiewicz
    Jakub Dyszkiewicz over 12 years
    But i want to stretch only if characters fill single line. By default i want to have 1 line
  • Jakub Dyszkiewicz
    Jakub Dyszkiewicz over 12 years
    20dp was only example, look at comment
  • Weloo
    Weloo over 12 years
    see this link it can help you stackoverflow.com/questions/1361332/…