1
\$\begingroup\$

I'm getting started with Android development. Take a look at this Android layout file:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/wallpaper">
    <Button
        android:id="@+id/gameStartButton"
        android:text="Start"
        android:fontFamily="serif"
        android:textSize="30sp"
        android:textColor="@color/white"
        android:background="@color/metal"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="@dimen/mainMenuItemWidth"
        android:onClick="chooseGameMode"/>
    <Button
        android:id="@+id/settingsButton"
        android:text="Settings"
        android:fontFamily="serif"
        android:textSize="30sp"
        android:textColor="@color/white"
        android:background="@color/metal"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="@dimen/mainMenuItemWidth"
        android:onClick="openSettings"/>
    <Button
        android:id="@+id/exitButton"
        android:text="Quit"
        android:fontFamily="serif"
        android:textSize="30sp"
        android:textColor="@color/white"
        android:background="@color/metal"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="@dimen/mainMenuItemWidth"
        android:onClick="closeApp"/>
</LinearLayout>

All three buttons have exactly the same dimensions, margins and color. As a result, this layout file appears needlessly verbose. Is is possible to extract this common information somewhere else so that it could be reused?

One way could be to subclass Button with specific features and use it instead of the stock button. Is there any other way? What I'm trying to achieve is simple attribute sharing and it shouldn't require dropping down to Java.

\$\endgroup\$
  • \$\begingroup\$ Have you looked into styles? \$\endgroup\$ – raldone01 Sep 8 '18 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.