Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make an Android button template?

Tags:

android

I have several buttons in my current app. They are all identical except for their text and a tag. The main.xml would be a lot nicer if I didn't have to repeat all the button config information for each button.

Is there a way to define a button as template, and then make more using it as a template?

In this example, I have about 10 of the following:

 <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="Y"
            android:typeface="monospace"
            android:textSize="12pt"
            android:tag="Y"
            android:textColor="@color/button_text"
            android:background="@drawable/grey_blank_48x48"
            android:onClick="onButtonClicked"/>

It would be nice if I had one, and then 9 of these:

 <Button2
            android:text="N"
            android:tag="N"/>

Resolution

It was pretty much as @Luksprog said:

<style name="ASR33_button" >
    <item name="android:id">@+id/button</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:padding">10dp</item>
    <item name="android:textColor">@color/button_text</item>
    <item name="android:background">@drawable/grey_blank_48x48</item>
    <item name="android:onClick">onButtonClicked</item>
    <item name="android:typeface">monospace</item>
    <item name="android:textSize">12pt</item>
</style>

with the main.xml having things like this:

   <Button
            style="@style/ASR33_button"
            android:tag="Y"
            android:text="Y"
            />

   <Button
            style="@style/ASR33_button"
            android:tag="N"
            android:text="N"
            />

This carries the day.

like image 801
Tony Ennis Avatar asked Apr 07 '12 17:04

Tony Ennis


Video Answer


2 Answers

Us a style:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="btnlook" >
        <item name="android:id">@+id/button</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:padding">10dp</item>
        <item name="android:textColor">@color/button_text</item>
        <item name="android:background">@drawable/grey_blank_48x48</item>
        <item name="android:onClick">onButtonClicked</item>
        <item name="android:typeface">monospace</item>
        <item name="android:textSize">12pt</item>
    </style>
</resources>

and then in your xml layout:

<Button
            style="@style/btnlook"
            android:text="N"
            android:tag="N"/>
like image 164
user Avatar answered Sep 20 '22 17:09

user


You can make a custom view that extends button and sets all the things that is repeating. Then you can use it as you described, but with a fully qualified name, not just shortname.

class MyButton extends Button {
    public MyButton() {
        // Set the values you want
    }
}

And in XML:

<com.me.myapp.MyButton
    android:text="N"
    android:tag="N" />
like image 21
Andreas Hagen Avatar answered Sep 20 '22 17:09

Andreas Hagen