Search code examples
javaandroidbutton

Apply general settings on multiple image buttons Android Studio


enter image description here I wanna make multiple buttons organized in a horizontal linear layout, but every buttons have common fields such as android:layout_marginStart="10dp", android:background="@null", android:scaleType="centerInside". Is there an better way to do that in order to assign this fields to every single button and not copy and paste them? Thank you

enter image description here As I explain, im looking for a pattern for all the buttons with common field. Thank you!


Solution

  • Try adding those common features into res\values\styles (If you don't have one then create) and then apply the styles to all the required imageButton.

     <style name="ImageButtonStyle">
     ="10dp", android:background="@null", android:scaleType="centerInside"
            <item name="android:layout_marginStart">10dp</item>
            <item name="android:background">@null</item>
            <item name="android:textColor">@color/black</item>
            <item name="android:scaleType">centerInside</item>
            //and many more attributes as u like.
            <item name="android:textAlignment">viewStart</item>
            <item name="android:fontFamily">@font/urbanist_medium</item>
        </style>
    

    Then use it in xml like this.

    <ImageButton
        android:id="@+id/txtDate"
        style="@style/ImageButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>