7

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

halfer
  • 19,824
  • 17
  • 99
  • 186
Kylo Ren
  • 486
  • 6
  • 24

7 Answers7

4

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"/>
Deep Patel
  • 2,584
  • 2
  • 15
  • 28
3

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" />
Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
2

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"/>                  
Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
  • Thats what i don't want to do actually. I want it to be adjustable on all devices – Kylo Ren Feb 22 '18 at 07:53
  • 1
    Then write style for it and give fix different height for every screen size. like for hdpi android:layout_height="75dp" for mdpi android:layout_height="60dp" xhdpi android:layout_height="90dp" – Ankita Chavan Feb 22 '18 at 08:40
2

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

Rahul Singh Chandrabhan
  • 2,531
  • 5
  • 22
  • 33
1

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.

rf43
  • 4,385
  • 3
  • 23
  • 28
zeekhuge
  • 1,594
  • 1
  • 13
  • 23
  • I know this but in that case if the device is of higher resolution or of bigger screensize, the layout height will remain same which i want to change according to the screensize. – Kylo Ren Feb 22 '18 at 09:33
  • 1
    In that case, you might wanna see [this](https://stackoverflow.com/questions/2025282/what-is-the-difference-between-px-dip-dp-and-sp). Just as a summary, the unit `dp` is `density independent pixel` and it would scale perfectly on screens with different pixel-densities, which mostly represent the size (resolution) of the screen – zeekhuge Feb 22 '18 at 09:44
  • 1
    And you can create different layouts for `mdpi`, `ldpi`, `hdpi`, `xhdpi` and `xxhdpi` if that is a big concern. Take a look [here](https://stackoverflow.com/questions/28507609/image-resolution-for-mdpi-hdpi-xhdpi-and-xxhdpi) – zeekhuge Feb 22 '18 at 09:46
  • I knew about mdpi and hdpi etc but didnt knew that dp is density independent. Thank you so much – Kylo Ren Feb 22 '18 at 12:25
0

One Solution : include these two lines in your XML file

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

Akash Jaiswal
  • 31
  • 1
  • 2
0

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>
Wellington
  • 151
  • 1
  • 3