Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust EditText to font size

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) {         
            }

        });
like image 840
Jakub Dyszkiewicz Avatar asked Feb 11 '12 10:02

Jakub Dyszkiewicz


3 Answers

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>
like image 150
Parag Chauhan Avatar answered Nov 12 '22 13:11

Parag Chauhan


set edit text height as wrap_content

android:layout_height="wrap_content"
like image 38
Venkata Krishna Avatar answered Nov 12 '22 12:11

Venkata Krishna


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.

like image 37
Arpit Garg Avatar answered Nov 12 '22 13:11

Arpit Garg