Edittext customization on Android

Asked

Viewed 1,932 times

1

I saw that there are several ways to customize a EditText, one of them is in background and the other is XML.

What would be the best option? Does it have disadvantage in using background or gives in the same?

  • In my view the two have the same purposes but the background is more complex.

1 answer

2


The best way to customize or style a EditText, just like any other view, is creating a dedicated xml file for such.

A file dedicated to customization ensures:

  • Reuse;
  • Organizing;
  • Easy access;

For example, I want to create a editText with curved/rounded edges.

First I will create an xml file where I will put all style attributes: rounded_edittext.xml

 <shape xmlns:android="http://schemas.android.com/apk/res/android" >

        <solid android:color="#FFFFFF" />

        <stroke
            android:width="1dp"
            android:color="#862f99" />
        <corners
            android:topLeftRadius="10dp"
            android:topRightRadius="10dp"
            android:bottomLeftRadius="10dp"
            android:bottomRightRadius="10dp"/>
    </shape>

Now I can add style to my editText defined the background as follows:

   <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_edittext"
        android:ems="10"
        android:inputType="textPersonName"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="135dp" />

And that’s it'

inserir a descrição da imagem aqui

More info: https://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromXml

Browser other questions tagged

You are not signed in. Login or sign up in order to post.