How to leave the fields with the same style?

Asked

Viewed 77 times

2

I don’t like to keep repeating code and I believe there is a way to leave, for example, two fields or more with the same style of formatting:

Down below I got two EditText they have something in common, like the left and right margins and the top.

As I could type, only once these margins, for those fields and future fields that are created, without having to type in all fields this same margin settings?

<EditText  
    android:id = "@+id/edfirstnumber" 
    android:layout_width = "match_parent" 
    android:layout_height = "wrap_content" 
    android:layout_marginTop = "20dp" 
    android:layout_marginLeft = "10dp" 
    android:layout_marginRight = "10dp" 
    android:hint = "@string/campoNumerico" 
    android:inputType = "number" 
/>

<EditText  
    android:id = "@+id/edsegundnumber" 
    android:layout_width = "match_parent" 
    android:layout_height = "wrap_content" 
    android:layout_marginTop = "20dp" 
    android:layout_marginLeft = "10dp" 
    android:layout_marginRight = "10dp" 
    android:hint = "@string/campoNumerico" 
    android:inputType = "number" 
/>

2 answers

2

You can create a style with all the attributes in common and use it in your EditText.

You’d have it in your file styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomText" parent="@style/Text">
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">#008</item>
    </style>
</resources>

I call attention to parent="@style/Text" because if this is not used you end up misrepresenting the EditText

And in its layout:

<?xml version="1.0" encoding="utf-8"?>
<EditText
    style="@style/CustomText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello, World!" />

The Code was taken from here: http://developer.android.com/guide/topics/resources/style-resource.html.

1


Search about styles and themes (Styles and themes). You can create a specific style for what you want and apply it to all the elements you want. More about the subject, click here.

Browser other questions tagged

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