Roshan
Roshan

Reputation: 1

Unable to Click anything (like button) in Card View used in Grid View using Kotlin

Here is the Code of MainActivity.kt

//Declarations
lateinit var recyclerView: RecyclerView
lateinit var gridView: GridView
lateinit var gridAdapter: ShopListGridAdapter
lateinit var recyclerAdapter: ShopListRecyclerViewAdapter

val items = ArrayList<ShopList>()

//Initialization in onCreate() method
recyclerView = findViewById(R.id.recycler_view)
gridView = findViewById(R.id.grid_view)
recyclerAdapter = ShopListRecyclerViewAdapter(applicationContext, items)
gridAdapter = ShopListGridAdapter(applicationContext, items)

//After adding some items in **items array list**
gridView.adapter = gridAdapter
recyclerView.adapter = recyclerAdapter
recyclerView.layoutManager = LinearLayoutManager(this@MainActivity)

Here is the Code of ShopListGridAdapter.kt

class ShopListGridAdapter(var context: Context, var itemList: ArrayList<ShopList>) : BaseAdapter() {

    override fun getCount(): Int = itemList.size
    override fun getItem(position: Int): Any = itemList[position]
    override fun getItemId(position: Int): Long = position.toLong()

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

        val view: View = View.inflate(context, R.layout.grid_content_item, null)

        ...
        val itemStatusBtn: Button = view.findViewById(R.id.grid_item_status_btn)

        val item: ShopList = itemList[position]
        ...

        itemStatusBtn.setOnClickListener {
            //do stuff
            ...
        }

        return view
    }
}

Here is the XML Code of Grid View (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    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:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/ll_wish_list_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:orientation="horizontal"
            android:paddingVertical="10dp"
            android:paddingHorizontal="20dp"
            android:gravity="center_vertical">

            <ImageView
                android:id="@+id/nav_drawer_opener_iv"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:src="@drawable/nav_bar_icon"
                android:contentDescription="@string/nav_bar_icon"/>

            <TextView
                android:id="@+id/top_bar_tv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/app_name"
                android:textStyle="bold"
                android:textSize="22sp"
                android:textAlignment="center"
                android:textColor="@color/text_color"/>

            <ImageView
                android:id="@+id/three_dots_menu_btn"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/more_dots"
                app:tint="@color/text_color"
                android:onClick="openThreeDotsMenu"
                android:contentDescription="@string/change_the_layout"/>

        </LinearLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@drawable/top_curve_rectangle"
            android:paddingTop="25dp">

            <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:id="@+id/grid_view_swipe_refresh"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <GridView
                    android:id="@+id/grid_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal"
                    android:paddingBottom="100dp"
                    android:horizontalSpacing="10dp"
                    android:verticalSpacing="10dp"
                    android:numColumns="2"
                    android:visibility="invisible"/>

            </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

            <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:id="@+id/recycler_view_swipe_refresh"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingBottom="100dp"
                    android:clipToPadding="false"
                    android:visibility="invisible"/>

            </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

            <ProgressBar
                android:id="@+id/progress_bar_loading"
                android:layout_width="50dp"
                android:layout_height="50dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toTopOf="@id/add_item_activity_btn"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:visibility="visible"/>

            <LinearLayout
                android:id="@+id/empty_indicator"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:gravity="center"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toTopOf="@+id/add_item_activity_btn"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:visibility="invisible"
                tools:ignore="UseCompoundDrawables">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/shopping_bag"
                    android:contentDescription="@string/empty_shopping_bag"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/your_list_is_empty"
                    android:textSize="30sp"
                    android:textColor="@color/green_color_1"
                    android:fontFamily="@font/roboto_medium"
                    android:textAlignment="center"/>

            </LinearLayout>

            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/add_item_activity_btn"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_marginVertical="20dp"
                android:contentDescription="@string/add_item"
                android:src="@drawable/add_icon"
                app:borderWidth="0dp"
                app:maxImageSize="40dp"
                app:tint="@color/white"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:onClick="openAddItemActivity"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:itemIconTint="@color/green_color_1"
        app:itemTextColor="#D93B4043"
        app:itemIconPadding="20dp"
        app:menu="@menu/nav_menu" />

</androidx.drawerlayout.widget.DrawerLayout>

Here is the code of grid item (grid_content_item.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:paddingHorizontal="25dp"
    android:paddingVertical="15dp"
    android:gravity="center_horizontal">

        <TextView
            android:id="@+id/item_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/item_name"
            android:textAlignment="center"
            android:textColor="#3FC6BA"
            android:textSize="20sp"
            android:fontFamily="@font/roboto_bold"/>

        <TextView
            android:id="@+id/quantity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginVertical="5dp"
            android:text="@string/quantity"
            android:textSize="12sp"
            android:fontFamily="@font/roboto_bold"
            android:textAlignment="center" />

        <TextView
            android:id="@+id/store_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/buy_from_anywhere"
            android:textSize="12sp"
            android:textAlignment="center"
            android:fontFamily="@font/roboto_medium"/>

        <Button
            android:id="@+id/grid_item_status_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:backgroundTint="@color/pending_color"
            android:fontFamily="@font/roboto_bold"
            android:text="@string/pending"
            android:textAllCaps="false"
            android:minHeight="0dip"
            android:textColor="@color/white" />

        <LinearLayout
            android:id="@+id/grid_speak_item_ll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:clickable="true"
            android:focusable="true"
            android:background="?android:attr/selectableItemBackground">

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/speak_out_icon"
                app:tint="#AB0C5FE8"
                android:layout_marginEnd="5dp"
                android:contentDescription="@string/speak_the_item"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/listen"
                android:fontFamily="@font/roboto_bold"
                android:textColor="#AB0C5FE8"/>

        </LinearLayout>
    </LinearLayout>

Grid View Item

Other methods tried

I have also tried this method in MainActivity.kt in onCreate() function

gridView.setOnItemClickListener { adapterView, view, pos, id ->
      when(view.id) {
           R.id.grid_item_status_btn -> Toast.makeText(...).show()
      }
}

but it also don't work for me. I'm unable to click anything on any item. Why Button is not Clickable? When I'm touching the button, it is not pressable. If the button gets fixed then I can proceed, but stuck right now. Thanks in Advance 😊

Changing View Layout in Code

In onCreate() method

sharedPref = getSharedPreferences("settings", MODE_PRIVATE)
if(sharedPref.getInt("view_mode", 0) == 1) gridView.visibility = ViewGroup.VISIBLE
else recyclerView.visibility = ViewGroup.VISIBLE

I had added a menu from where I change the view layouts:

when (it.itemId) {
      R.id.change_to_grid_view -> changeViewLayout(gridView)
      R.id.change_to_recycler_view -> changeViewLayout(recyclerView)
      ...
}

this is the function which handles visibility

    private fun changeViewLayout(view: View) {
        if(view == gridView) {
            gridView.visibility = ViewGroup.VISIBLE
            recyclerView.visibility = ViewGroup.INVISIBLE
            gridAdapter.notifyDataSetChanged()
        } else {
            recyclerView.visibility = ViewGroup.VISIBLE
            gridView.visibility = ViewGroup.INVISIBLE
            recyclerAdapter.notifyDataSetChanged()
        }
    }

Means, in my project only one view will be visible at a time. The button in Recycler View is clickable and working everything fine, but not in Grid View.

Upvotes: 0

Views: 443

Answers (1)

MehranB
MehranB

Reputation: 1525

In the following part of your mainActivity xml File, you have two views that are basically on top of each other covering (almost) the whole screen.

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/grid_view_swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:paddingBottom="100dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:numColumns="2"
        android:visibility="invisible"/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/recycler_view_swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="100dp"
        android:clipToPadding="false"
        android:visibility="invisible"/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

since they are both invisible in the xml file, it means you are turning them visible in your code somewhere.

Now my guess is that you are turning both views Visible at the same time in your code but you are not assigning an adapter to the recyclerView, which practically means you have an empty (hence invisible) recycler view on top of your GridView.

Check for that please.

Upvotes: 1

Related Questions