Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert an icon in the beginning of a Material Text Field

I am trying to insert an icon in the beginning of a Material Text Field. I couldn't find anything suitable in material.io's documentation as well.

This is what I am trying to achieve.

Image

like image 443
Ishaan Ohri Avatar asked Mar 04 '23 08:03

Ishaan Ohri


2 Answers

Use a TextInputLayout with the app:startIconDrawable

<com.google.android.material.textfield.TextInputLayout
    android:hint="Select Time"
    app:startIconDrawable="@drawable/ic_add_24px"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

    <com.google.android.material.textfield.TextInputEditText
        ../>

</com.google.android.material.textfield.TextInputLayout>

enter image description here

Note: it requires the version 1.1.0 of the library.

like image 198
Gabriele Mariotti Avatar answered Apr 27 '23 03:04

Gabriele Mariotti


Use the drawableStart attribute:

https://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableStart

https://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesRelativeWithIntrinsicBounds(int,%20int,%20int,%20int)

like image 42
dominicoder Avatar answered Apr 27 '23 01:04

dominicoder