android-studiocode-duplication

How to reuse the same guidelines in differente constraint layouts in Android Studio


I am doing the user interface of an Android Studio Java app. I use guidelines to constraint the elements with the layout constraint, e.g: app:layout_constraintEnd_toStartOf="@+id/vButtonGuidelineEnd"

I would like to use the same guidelines in different layouts (to treat them like margins). Is there a way to not duplicate code. In addition to creating many guidelines (exactly the same), they must have different names. It has no sense. I have searched a lot and didn't found a single way of doing this. People explain guidelines to be used in a single layout.

I tried creating a layout with all the common guidelines called guidelines.xml, which is another constraint layout. This made sense. I tried to include somehow this layout to the others but it is included like a block and I can't access the elements of the guidelines.xml layout. Therefore, I can't constraint the elements to the given guidelines. This is my guidelines.xml:

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/guidelinesLayout" android:layout_width="match_parent" android:layout_height="match_parent">

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/vButtonGuidelineStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.2" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/vButtonGuidelineEnd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.8" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/hIconGuidelineStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.05" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/hIconGuidelineEnd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.3" /></androidx.constraintlayout.widget.ConstraintLayout>`

I would like to use this different guidelines in several layouts. For example doing something similar to this in another layout but using the guidelines of guidelines.xml:

<TextView
        android:id="@+id/textViewAppName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textAppearance="@style/LargeTextAppearance"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.494"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/hIconGuidelineEnd" ***
        app:layout_constraintVertical_bias="0.0" />

Solution

  • You will want to use a Style in order to replicate common features across multiple objects. This will avoid duplication of code as well as allow you to make changes more quickly if you desire. You'll create a styles.xml file and name the style you want to use, and then set the specific attributes that are common across all instances of it. The documentation can be found here:

    https://developer.android.com/develop/ui/views/theming/themes#Styles