Search code examples
androidbuttonlayout

How to make a button with more information in android studio


I'm trying to develop a button that has more than just an text information + an icon, but I don't know how can I make this, I want to make as this layout image right here:

Exepected Layout

I've already started to make it but I don't know how to finish, should I use a button? or try to make a container to store all information?

<?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"
    android:background="#00FFFFFF"
    tools:context=".Home">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/txtUsuario"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/roboto_medium"
            android:text="Olá usuário!"
            android:textColor="@color/black"
            android:textSize="25sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView20" />

        <ImageView
            android:id="@+id/imageView20"
            android:layout_width="200dp"
            android:layout_height="80dp"
            android:src="@drawable/logotechtablecolorida"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView29"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/roboto_light"
            android:text="Escolha seu restaurante e faça seu pedido"
            android:textAlignment="center"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txtUsuario" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • You can use any layout (ConstraintLayout, LinearLayout) and set a click listener to them.

    <LinearLayout
        android:id="@+id/twoLinesButton"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">
            
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is the first line"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is the second line"
            />
    </LinearLayout>
    

    And in your code, attach a click listener to the view.

    val twoLinesButton = findViewById<LinearLayout>(R.id.twoLinesButton)
    twoLinesButton.setOnClickListener {
        //Handle the click action
    }