Search code examples
androidandroid-layoutbuttonlabelandroid-constraintlayout

How I can put a label above the button in android studio?


I want to design a messages button that is attached to a label showing the number of messgaes. Like the image below

enter image description here

I designed it with android studio, but the label is not aligned properly. How I can put it at the top of the button?

enter image description here

Here is my code

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeActivity">


    <Button
        android:id="@+id/msg"
        android:layout_width="157dp"
        android:layout_height="42dp"
        android:background="@color/lightgrey"
        android:backgroundTint="#@color/lightgrey"
        android:text="Messages"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.206" />

    <TextView
        android:id="@+id/mark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_textview"
        android:gravity="center"
        android:text="2"
        android:textColor="@color/lightgrey"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.662"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.17" />



</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • You need to remove any constraints of the TextView attached to the parent, and make the TextView only constrained to the Button

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".HomeActivity">
    
        <Button
            android:id="@+id/msg"
            android:layout_width="157dp"
            android:layout_height="42dp"
            android:background="@color/lightgrey"
            android:backgroundTint="#@color/lightgrey"
            android:text="Messages"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintVertical_bias="0.206"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/mark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/rounded_textview"
            android:gravity="center"
            android:text="2"
            android:textColor="@color/lightgrey"
            android:textSize="20sp"
            android:elevation="10dp"
            app:layout_constraintBottom_toTopOf="@+id/msg"
            app:layout_constraintEnd_toEndOf="@id/msg"
            app:layout_constraintStart_toEndOf="@id/msg"
            app:layout_constraintTop_toTopOf="@id/msg" />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Preview

    enter image description here