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

3

7

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

Labdanum answered 6/10, 2019 at 19:12 Comment(1)
Does this answer your question? How to position drawable start icon at the start of the material text input layoutSalient
I
6

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.

Inevitable answered 6/10, 2019 at 21:38 Comment(7)
The app:startIconDrawable attribute doesn't work for me. The icon does not show up in design view, and I get an error like this: AAPT: error: attribute app:startIconDrawable not found. Besides this, the material component library seems to work fine.Nobleminded
@Nobleminded It requires the version 1.1.0 of the Material Components LibraryInevitable
@Gabrielle Mariotti Thanks! I used 1.0.0. Updated to 1.2.0-alpha01, but now I lost the whole material style. All I can see is the default AppCompat style on the layout design.Nobleminded
@Nobleminded I think that the 1.1.0-beta01 is better. It will become stable in a few weeks. In any case you have to use a Material Component Theme in your app.Inevitable
@Gabrielle Mariotti I tried 1.1.0-beta01 as well. Same result. I use the material component theme. Everything was fine (except the icon) with 1.0.0.Nobleminded
@GabrieleMariotti I wonder is there an option to provide startIconDrawable padding?Goatee
@AnoopM not currently (1.2.1-1.3.0-alpha02)Inevitable
E
1

step 1: copy required Text field

Material text field doc : https://material.io/components/text-fields/android#using-text-fields

enter image description hereenter image description here enter image description here

step 2: select text field -> search "draw" -> Click drawableLeft icon

enter image description here

step 3: Select or Add icon (I'll select vector icon)

enter image description here enter image description here enter image description here enter image description here

Select icon before Finish enter image description here

Select icon and click OK enter image description here

Done :) enter image description here

Encapsulate answered 23/3, 2022 at 15:22 Comment(1)
Show us the resulting xml code.Salient

© 2022 - 2024 — McMap. All rights reserved.