4

I have 5 buttons on my activity that differ each other only with value height:

    <Button
    android:id="@+id/someName"
    android:layout_width="170dp"
    android:layout_height="70dp"
    android:layout_marginTop="10dp"
    android:text="@string/some_text" />

    ... other buttons...

Is it possible to extends 4 buttons from first one and change only one of their property?
I know it is possible for styles, but what with components?
Or maybe should i do this in java code, but then how to connect to specific class?

2 Answers 2

2

I've found a detailed answer:
http://docs.xamarin.com/recipes/android/resources/general/style_a_button/

My own example based on URL above:

1) res\drawable\category_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"><shape android:shape="rectangle">
            <solid android:color="#ef4444" />

            <corners android:radius="15dp" />
        </shape></item>
    <item><shape android:shape="rectangle">
            <solid android:color="#D8D8D8" />

            <corners android:radius="15dp" />
        </shape></item>

</selector>

2) res\layout\some_activity.xml

 ... some xml code ...
 <Button
        android:id="@+id/buttonTestCatA"
        style="@style/category_button"
        android:background="@drawable/category_button"
        android:text="@string/test_cat_A" />
... some xml code ...

3) res\values\styles.xml

<resources>

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>

    <style name="AppTheme" parent="AppBaseTheme"></style>

    <style name="category_button">
        <item name="android:layout_width">170dp</item>
        <item name="android:layout_height">70dp</item>
        <item name="android:layout_margin">5dp</item>
        <item name="android:textSize">25sp</item>
    </style>

</resources>
Sign up to request clarification or add additional context in comments.

1 Comment

Please extract the key notes from the link and add them to your answer so it is understandable even if the link is dead. You can also accept your own answer.
-1

You must create a new XML file in your drawable folder. For example, let's rename it myCustomButton.xml.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:width="100dp"
        android:height="20dp" />
</selector>

Then, for each button you want to get the same behaviour, add this line: android:background="@drawable/myCustomButton".

2 Comments

As far as i know all names in res folder shoul contains only small lether then my_custom_button.xml is allowed. Anyway this doesn't works. Compiler complains that attributes that should be extended are not visible.
Is this solution better than using styles.xml?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.