Search code examples
androidgrid-layout

GridLayout of Button on ImageView


What I'm trying to display to the screen is Image and buttons that cover the image and enable the user clicks on the buttons and slowly reveals the image behind the buttons. The problem that the button does not appear at all,can not be seen at all. In addition to that, I need x * x buttons that will fit to cover the whole image evenly.

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/cat">

<GridLayout
    android:id="@+id/GridLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="4"
    android:rowCount="4">
    <Button
        android:id="@+id/first"
        android:textSize="20sp"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:text="W"
        android:textAlignment="center"
        android:background="@drawable/signalsquare"
        />


</GridLayout>
This image is for demonstration purposes(sorry for the lack of symmetry in the buttons)

At first the user sees the image covered with buttons,then each click of each button reveals another small part of the image

enter image description here


Solution

  • enter image description here

    <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/questions" />
    
        <LinearLayout
            android:id="@+id/GridLayout1"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="@+id/imageView3"
            app:layout_constraintEnd_toEndOf="@+id/imageView3"
            app:layout_constraintStart_toStartOf="@+id/imageView3"
            app:layout_constraintTop_toTopOf="@+id/imageView3">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/q"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
                <Button
                    android:id="@+id/w"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
                <Button
                    android:id="@+id/e"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
                <Button
                    android:id="@+id/r"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:layout_weight="1">
                <Button
                    android:id="@+id/q"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
                <Button
                    android:id="@+id/w"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
                <Button
                    android:id="@+id/e"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
                <Button
                    android:id="@+id/r"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:layout_weight="1">
                <Button
                    android:id="@+id/q"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
                <Button
                    android:id="@+id/w"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
                <Button
                    android:id="@+id/e"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
                <Button
                    android:id="@+id/r"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:layout_weight="1">
                <Button
                    android:id="@+id/q"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
                <Button
                    android:id="@+id/w"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
                <Button
                    android:id="@+id/e"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
                <Button
                    android:id="@+id/r"
                    android:textSize="20sp"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="W"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:background="@drawable/button_design"
                    />
            </LinearLayout>
        </LinearLayout>