Christofer Klassen
Christofer Klassen

Reputation: 157

Android custom XML view template

I have a custom-styled checkbox that I'd like to replicate a number of times in my Android app. The XML for this checkbox is:

<CheckBox
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:paddingLeft="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/checkbox_large"
    android:text="Temporary Text"/>

Obviously, I can simply copy the XML and paste it lower down in my layout file, but in the event that I want to change the design I would have to do it for every pasted checkbox.

Is there a clean way to create a custom template for this checkbox, or should I be implementing this in code?

Upvotes: 0

Views: 116

Answers (1)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

Is there a clean way to create a custom template for this checkbox, or should I be implementing this in code?

You can use styles. Create

<style name="MyCheckbox">
     <item name="android:layout_marginLeft">10dp</item>
    <item name="android:layout_marginTop">10dp</item>
    <item name="android:paddingLeft">10dp</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:button">@drawable/checkbox_large</item>
    <item name="android:text">Temporary Text</item>
</style>

and then just apply to all checkboxes you want:

<CheckBox 
    style="@style/MyCheckbox"/>

When in need of change, just alter your style.

Upvotes: 4

Related Questions