Change EditText height to fit text size

If I change textize in editext, the height of the edittext changes accordingly. I want the size of the edittext to be the same, and I want it to have a hint or text of a smaller size. What should I do? Below is the code, and I also post a 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 before change:

Screenshot of the layout before changing

Screenshot after changing text:

Screenshot of layout after changing textsize

+4
source share
5 answers

Use 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" />
+1
source

Try this third-party library here. The height of the TextView will be fixed, but your text view size will be adjusted.

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

Include any view that extends TextView in code:

AutofitHelper.create(textView);

Allow 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>

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"
    />

. :

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

+2

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"/>                  
+1

android:minHeight android:maxHeight:

                            <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"/>
+1

EditText, .

EditText, :

android:layout_height="<height_you_want>dp"

EditText:

android:textSize="<height_you_want>dp"

AndroidStudio.

+1

Source: https://habr.com/ru/post/1693965/


All Articles