skip to Main Content

I have buttons what I made with "LinearLayout" but when I want to made them clickable and so that it goes to another activity. I understand, that I need to change from "LinearLayout" to "ImageButton" because I want to make a beautiful app [what I want it looks like][1], but when I launch it, it stops working. What can I do to repair that? Thank you in advance for your help and sorry for my bad english. 🙁

<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="@color/colorBackground"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layoutHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/header_background"
        android:padding="20dp"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textHello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="@string/hello"
            android:textColor="@color/colorWhite"
            android:textSize="25sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="TextContrastCheck" />

        <TextView
            android:id="@+id/textUsername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:text="@string/vassili"
            android:textColor="@color/colorWhite"
            android:textSize="25sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@id/textHello"
            tools:ignore="TextContrastCheck" />

        <ImageView
            android:id="@+id/imageMenu"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_menu"
            app:layout_constraintBottom_toBottomOf="@id/textHello"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@id/textHello"
            app:tint="@color/colorWhite"
            tools:ignore="ImageContrastCheck" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:overScrollMode="never"
        android:scrollbars="none"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/layoutHeader"
        app:layout_constraintVertical_bias="0.0"
        tools:layout_editor_absoluteX="-96dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/layoutGoal"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="22dp"
                android:background="@drawable/white_background_icons"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="15dp"
                app:layout_constraintDimensionRatio="H, 1:1"
                app:layout_constraintEnd_toStartOf="@id/layoutTraining"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="8dp"
                    android:contentDescription="@string/app_name"
                    android:padding="8dp"
                    android:src="@drawable/notepad" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/goals"
                    android:textColor="@color/colorAccent"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>


            <LinearLayout
                android:id="@+id/layoutTraining"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="22dp"
                android:background="@drawable/white_background_icons"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="15dp"
                app:layout_constraintDimensionRatio="H, 1:1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/layoutGoal"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="8dp"
                    android:contentDescription="@string/app_name"
                    android:padding="8dp"
                    android:src="@drawable/dumbbell" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/Trainings"
                    android:textColor="@color/colorAccent"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/layoutWater"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="22dp"
                android:background="@drawable/white_background_icons"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="15dp"
                app:layout_constraintDimensionRatio="H, 1:1"
                app:layout_constraintEnd_toStartOf="@id/layoutTraining"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/layoutGoal"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="8dp"
                    android:contentDescription="@string/app_name"
                    android:padding="8dp"
                    android:src="@drawable/bottle" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/Water"
                    android:textColor="@color/colorAccent"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/layoutStepCounter"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="22dp"
                android:background="@drawable/white_background_icons"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="15dp"
                app:layout_constraintDimensionRatio="H, 1:1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/layoutGoal"
                app:layout_constraintTop_toBottomOf="@id/layoutTraining"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="8dp"
                    android:contentDescription="@string/app_name"
                    android:padding="8dp"
                    android:src="@drawable/walk" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/step_counter"
                    android:textColor="@color/colorAccent"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>


  [1]: https://i.stack.imgur.com/WdW6v.png

2

Answers


  1. May you need to change constraintlayout to LinearLayout and if you want click be on image add id to imageview

    Login or Signup to reply.
  2. For this type of layout you need to make your LinearLayout clickable
    For example:

    LinearLayout linearLayoutGoals;
    

    then in you onCreate do findViewById and set its clickListener

    linearLayoutGoals = findViewById(R.id.layoutGoal);
    linearLayoutGoals.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //DO YOUR CODE
            }
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search