How to leave the fields with the same style?

2

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

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

How could I only enter these margins once for these fields and future fields that have been created, without having to enter the same margin settings in all fields?

<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" 
/>
    
asked by anonymous 23.06.2014 / 21:17

2 answers

1

Look into 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 on the subject, click here .

    
23.06.2014 / 21:36
2

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

You would have 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 typing the EditText

And in your 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 has been removed from here: link .

    
23.06.2014 / 21:41