Search code examples
javaandroidxmlbuttonclick

Declare a common onClick attribute for all buttons in styles.xml file


My layout involves a lot of Buttons with all attributes absolutely same except the android:text="button_name" attribute.

The theme for these buttons is defines as follows in styles.xml

<style name="Core.ButtonStyleSmall">
    <item name="android:layout_width">16sp</item>
    <item name="android:layout_height">16sp</item>
    <item name="android:layout_marginLeft">8sp</item>
    <item name="android:layout_marginBottom">6sp</item>
    <item name="android:textSize">10sp</item>
    <item name="android:onClick">takeToApplication</item>
</style>

What I want to achieve is a common onClick statement for all of these buttons and thus I have added it here but it doesn't seem to work.

In my java file it still prompts a warning saying Method'takeToApplication(android.view.View)' is never used.

What is the correct way to do this? Please help me regarding this.


Solution

  • Since no one had a definite answer, I will go ahead and answer it myself:

    Any xml attribute can be declared in the styles.xml file for all the views that need to share any common attributes. Thus, onClick works just fine when declared there.

    It gives you a warning beforehand saying Method 'name_of_method' is never used, but that is just because the call to the resources files happen at runtime. So the warning can be safely ignored, it renders no problems.