Jakub Dyszkiewicz
Jakub Dyszkiewicz

Reputation: 534

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

        });

Upvotes: 9

Views: 34677

Answers (7)

Parag Chauhan
Parag Chauhan

Reputation: 35946

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>

Upvotes: 6

Chanaka Weerasinghe
Chanaka Weerasinghe

Reputation: 5742

you can add @style page name

<style name="value_big" parent="@android:style/TextAppearance.Medium">
  <item name="android:textSize">35sp</item>
  <item name="android:textColor">@color/white</item>
  <item name="android:textStyle">bold</item>
  <item name="android:typeface">sans</item>
  <item name="android:scrollbarAlwaysDrawVerticalTrack">true</item>
</style>

call that inside your edit text

                  <EditText
                    android:id="@+id/msgtext"
                    android:layout_width="fill_parent"
                    style="@style/value_big"
                    android:layout_alignParentLeft="true"
                    android:layout_marginLeft="10dip"
                    android:layout_marginTop="10dip"
                    android:layout_marginRight="10dip"
                    android:inputType="textMultiLine"
                    android:layout_height="150dip"
                    android:gravity="top"
                    android:padding="5dip"> 
                  </EditText>

Upvotes: 0

jags chaudhary
jags chaudhary

Reputation: 1

set android:focusable="false" in the editText

Upvotes: -1

Venkata Krishna
Venkata Krishna

Reputation: 1603

set edit text height as wrap_content

android:layout_height="wrap_content"

Upvotes: 3

AliSh
AliSh

Reputation: 10619

you can use a scrollView, and add your editText in a ScrollView, and use this :

android:inputType="textImeMultiLine"

Upvotes: 0

Arpit Garg
Arpit Garg

Reputation: 8546

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.

Upvotes: 2

Weloo
Weloo

Reputation: 625

If you want to make EditText height more than one line you can put in your EditText:

android:layout_height="fill_parent" and set android:layout_weight="1" to get more space for adding more views in activity

Upvotes: 0

Related Questions