Search code examples
androidandroid-linearlayoutandroid-imageviewandroid-tablelayoutandroid-relativelayout

Button with ImageView is not clickable in a Relative Layout inside a Table Layout wrapped in Linear Layout


I am unable to click. I tried making Relative Layout clickable and focusable and Button & ImageView not clickable and not focusable but it is not working. I tried setting click event on both button and Layout. Nothing is working. Following is the main_activity.xml file

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.knit.cdc_knit.MainActivity"
    android:orientation="vertical">
            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:weightSum="1"
                    android:layout_weight="0.5">
                    <RelativeLayout
                        android:id="@+id/knit_layout"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5"
                        >
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/aboutKnit"
                            android:background="@drawable/line_right_bottom_trans"
                            android:text="@string/aboutKnit"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"
                            />
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/college"
                            android:layout_centerInParent="true"
                            />
                    </RelativeLayout>
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/aboutCdc"
                            android:background="@drawable/line_bottom_trans"
                            android:text="@string/aboutCdc"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/depart"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                </TableRow>
                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:weightSum="1"
                    android:layout_weight="0.5">
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/record"
                            android:background="@drawable/line_right_bottom_trans"
                            android:text="@string/record"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/record"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/company"
                            android:background="@drawable/line_bottom_trans"
                            android:text="@string/company"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/company"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                </TableRow>
                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:weightSum="1"
                    android:layout_weight="0.5" >
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/training"
                            android:background="@drawable/line_right_bottom_trans"
                            android:text="@string/training"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/training"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/internship"
                            android:background="@drawable/line_bottom_trans"
                            android:text="@string/internship"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/intern"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                </TableRow>
                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:weightSum="1"
                    android:layout_weight="0.5">
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/personality"
                            android:background="@drawable/line_right_trans"
                            android:text="@string/personality"
                            android:textSize="14sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/gallery"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.5">
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/team"
                            android:background="@drawable/transparent"
                            android:text="@string/team"
                            android:textSize="15sp"
                            android:textColor="@color/fontColor"
                            android:gravity="center|bottom"
                            android:singleLine="false"
                            android:paddingBottom="10dp"/>
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/team"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                </TableRow>
            </TableLayout>

</android.support.v7.widget.LinearLayoutCompat>

Here is java code:

    package com.knit.cdc_knit;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {
    Button btnKnit, btnCdc, btnRecord, btnCompany, btnTraining, btnintern, btnGallery, btnTeam;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onClickButtonListener(){
        btnKnit = findViewById(R.id.aboutKnit);
        RelativeLayout knitlayout = findViewById(R.id.knit_layout);
        knitlayout.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent iknit = new Intent(MainActivity.this, AboutKnit.class);
                        startActivity(iknit);
                    }
                }
        );
    }
}

Here is AndroidManifest.xml:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.knit.cdc_knit">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AboutKnit">
            <intent-filter>
                <action android:name="android.intent.action.AboutKnit" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity android:name=".AboutCdc">
            <intent-filter>
                <action android:name="com.knit.cdc_knit.AboutCdc" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

My Layout looks like: This is main_activity.xml


Solution

  • Better Approach to this is :

    package com.knit.cdc_knit;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.RelativeLayout;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        Button btnKnit, btnCdc, btnRecord, btnCompany, btnTraining, btnintern, btnGallery, btnTeam;
    RelativeLayout knitlayout;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    onClickButtonListener();
        }
        public void onClickButtonListener(){
            btnKnit = findViewById(R.id.aboutKnit);
            knitlayout = findViewById(R.id.knit_layout);
            knitlayout.setOnClickListener(this);
    btnKnit .setOnClickListener(this);
    
        }
    
     @Override
        public void onClick(View view) {
            if(view.getId() == R.id.knit_layout)
            {
     Intent iknit = new Intent(MainActivity.this, AboutKnit.class);
                            startActivity(iknit);
    } else if(view.getId() == R.id.btnKnit )
            {
    //Your Logic
    }
    }