Kylo Ren
Kylo Ren

Reputation: 486

EditText height changes according to the text size

If I change the textsize in editext, the height of edittext changes accordingly. I want the size of edittext to be same and I want a smaller sized hint or text to be in it. What should I do? Following is the code and I am also posting the screenshot of this layout :

  <RelativeLayout
            android:id="@+id/normal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#4594e4"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/set"
                android:layout_width="wrap_content"
                android:layout_height="75dp"
                android:layout_marginTop="05dp"
                android:padding="5dp"
                android:src="@drawable/bari" />

            <EditText
                android:id="@+id/etOrigin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/_35sdp"
                android:layout_marginTop="@dimen/_10sdp"
                android:layout_toEndOf="@+id/set"
                android:layout_toRightOf="@+id/set"
                android:background="#5fa7f1"
                android:inputType="textPersonName"
                android:text="My Location"
                android:textColor="#434343"
                android:textIsSelectable="true" />

            <EditText
                android:id="@+id/etDestination"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/etOrigin"
                android:layout_marginRight="@dimen/_35sdp"
                android:layout_marginTop="@dimen/_5sdp"
                android:layout_toEndOf="@+id/set"
                android:layout_toRightOf="@+id/set"
                android:background="#5fa7f1"
                android:inputType="textPersonName"
                android:onClick="wow"
                android:text="Where to go ?"
                android:textColor="#fff"
                android:textColorHint="#fff" />

            <ImageView
                android:id="@+id/swipe"
                android:layout_width="30dp"
                android:layout_height="27dp"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="@dimen/_5sdp"
                android:background="@drawable/flip" />

        </RelativeLayout>

Screenshot of the layout before changing:

Screenshot of the layout before changing

Screenshot of layout after changing textsize:

Screenshot of layout after changing textsize

Upvotes: 7

Views: 5329

Answers (7)

Wellington
Wellington

Reputation: 307

Using the EditText inside of a ConstraintLayout will fix this.

...
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="17">
    <androidx.appcompat.widget.AppCompatEditText
        android:layout_width="match_parent"
        android:layout_height="0dp"                        
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHeight_percent="1"
        android:textSize="24sp"
        android:text="foo"
    />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Akash Jaiswal
Akash Jaiswal

Reputation: 31

One Solution : include these two lines in your XML file

android:maxLines="1" android:singleLine="true"

Upvotes: 0

zeekhuge
zeekhuge

Reputation: 1594

You can explicitly set the height and size of your EditText view as well as the text in it.

To set the heigh of the EditText simply use :

android:layout_height="<height_you_want>dp"

And to set height of the text in your EditText view :

android:textSize="<height_you_want>sp"

And you can simultaneously view the changes in the preview tab of AndroidStudio.

Upvotes: 1

Abhinav Gupta
Abhinav Gupta

Reputation: 2265

Use padding for this :-

         <EditText
            android:id="@+id/etOrigin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="12dp"
            android:layout_marginRight="@dimen/_35sdp"
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_toEndOf="@+id/set"
            android:layout_toRightOf="@+id/set"
            android:background="#5fa7f1"
            android:inputType="textPersonName"
            android:text="My Location"
            android:textColor="#434343"
            android:textIsSelectable="true" />

Upvotes: 3

Rahul Singh Chandrabhan
Rahul Singh Chandrabhan

Reputation: 2496

Here, try this third party library. Your TextView height would be fixed but your textView size will be adjusted

dependencies {
    compile 'me.grantland:autofittextview:0.2.+'
}

Enable any View extending TextView in code:

AutofitHelper.create(textView);

Enable any View extending TextView in XML:

<me.grantland.widget.AutofitLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        />
</me.grantland.widget.AutofitLayout>

Use the built in Widget in code or XML:

<RootElement
    xmlns:autofit="http://schemas.android.com/apk/res-auto"
    ...
<me.grantland.widget.AutofitTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:maxLines="2"
    android:textSize="40sp"
    autofit:minTextSize="16sp"
    />

For more information, visit below link:

https://github.com/grantland/android-autofittextview

Upvotes: 2

Ankita Chavan
Ankita Chavan

Reputation: 64

Give fix height to the layout android:layout_height="75dp"

            <EditText
            android:id="@+id/etOrigin"
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:layout_marginRight="@dimen/_35sdp"
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_toEndOf="@+id/set"
            android:layout_toRightOf="@+id/set"
            android:background="#5fa7f1"
            android:inputType="textPersonName"
            android:text="My Location"
            android:textColor="#434343"
            android:textIsSelectable="true"/>                  

Upvotes: 2

Deep Patel
Deep Patel

Reputation: 2644

Try using android:minHeight and android:maxHeight attributes :

                            <EditText
                            android:id="@+id/etUserName"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="@string/hint_username"
                            android:imeOptions="actionNext"
                            android:inputType="textPersonName"
                            android:maxLines="1"
                            android:gravity="center_vertical"
                            android:minHeight="?actionBarSize"
                            android:maxHeight="?actionBarSize"
                            android:textColor="@color/textPrimary"
                            android:textSize="12sp"/>

                            <EditText
                            android:id="@+id/etUserEmail"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:hint="@string/hint_username"
                            android:imeOptions="actionNext"
                            android:inputType="textEmailAddress"
                            android:maxLines="1"
                            android:gravity="center_vertical"
                            android:minHeight="?actionBarSize"
                            android:maxHeight="?actionBarSize"
                            android:textColor="@color/textPrimary"
                            android:textSize="14sp"/>

Upvotes: 4

Related Questions