Home > Back-end >  how do I add padding in EditText hints
how do I add padding in EditText hints

Time:10-20

I have made a login page and in it there are two fields, one for username and one for password, and I can't seem to figure out a way to add padding to hints so the hints in the field look like this

and if I add the android:paddingTop="30dp"

it looks like this

this EditText is using a background source (field_bg) and also a user icon which changes its colors when the field is selected (custom_username_field).

here is the xml code for the username field.

<EditText
        android:id="@ id/usernameField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autofillHints=""
        android:background="@drawable/field_bg"
        android:cursorVisible="false"
        android:drawableStart="@drawable/custom_username_field"
        android:drawablePadding="-30dp"
        android:ems="10"
        android:fontFamily="@font/staatliches"
        android:hint="USERNAME"
        android:includeFontPadding="true"
        android:inputType="textPersonName"
        android:textAlignment="center"
        android:textColor="#6E6E6E"
        android:textSize="20sp"
        android:paddingTop="30dp"
        app:boxStrokeWidth="@null"
        app:boxStrokeWidthFocused="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.507"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.421"
        tools:ignore="TouchTargetSizeCheck" />

CodePudding user response:

there is a poor solution to this problem:

android:textAlignment="center"
android:gravity="top"
tools:ignore="RtlCompat" 

But in this case, the text that the user will enter will replace the hint and will be at the very top, which will not look nice. It would be possible to change the position of the text after the user has entered the text but for this you need to switch to code, not XML.

I also suspect that you plan to leave a prompt even after the user enters the text.

CodePudding user response:

create hint in string.xml and then use in view

 <EditText android:hint="@string/yourhint" />

CodePudding user response:

You can try like this.

    <EditText
        android:id="@ id/usernameField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/round_10_background3"
        android:drawableStart="@drawable/aa"
        android:drawableTint="@color/white"
        android:ems="10"
        android:fontFamily="@font/caveat_semi_bold"
        android:hint="USERNAME"
        android:inputType="textPersonName"
        android:padding="20dp"
        android:textAlignment="center"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:textSize="20sp"
        app:boxStrokeWidth="@null"
        app:boxStrokeWidthFocused="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.507"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.421"
        tools:ignore="TouchTargetSizeCheck" />

  • Related