Search code examples
javaandroidandroid-studioandroid-layoutbutton

Android Studio Java - How do I add more than one text on a button


Is there a way to add text in this specific layout instead of constantly pressing the space button and trying to align the number with the previous buttons. 1

 <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btn1"
        android:layout_width="352dp"
        android:layout_height="55dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/btn1"
        **android:text="1                  Item                                  number"**
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.44"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btn"
        android:layout_width="352dp"
        android:layout_height="55dp"
        android:layout_marginBottom="160dp"
        android:background="@drawable/btn1"
        **android:text="2                    Item                         number"**
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.491"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.177" />


</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • This way is not a good practice to fulfill your requirement as android xml have some rules for managing the views and it's content over the screen.

    You can use ConstraintLayout functionality for managing views in chains like a horizontal management in your case or vertical management. You can visit this link to understand the basic and try to work around on it to be the master. You can use the weights with chain showing inside the image to meet your requirement.

    enter image description here

    There is another option available when using LinearLayout which provides the weight to show the views over the screen you can create horizontal oriented linear layout and provide the three views either button or text as you wanted and then provide the weight for them as per your requirement.