GridView setOnItemClickListener change item background colour, remove background colour of all other item

0

I currently have my app working so that when an item inside the GridView is clicked, the background will turn blue, how can I make it so that if another item is clicked, the previous blue is removed?

I would imagine it would need to be something like prevItem.setBackgroundColor(0x00000000); or applying that background colour to every item currently in the GridView

Answer here seemed to work (https://stackoverflow.com/a/61738985/8997460) but if any icon at the position 9 or higher then the app would crash and i get the error java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setBackgroundColor(int)' on a null object reference

iconGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            groupImg = (int) parent.getItemAtPosition(position);
            LinearLayout item = (LinearLayout) view;
            item.setBackgroundColor(Color.BLUE);

        }
    });

the GridView element code:

        <GridView
        android:id="@+id/iconGrid"
        android:layout_width="350dp"
        android:layout_height="300dp"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:isScrollContainer="false"
        android:numColumns="3"
        android:scrollbars="vertical"
        android:stretchMode="columnWidth"
        android:verticalSpacing="20dp"
        app:layout_constraintBottom_toTopOf="@+id/colPickerBtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.491"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/groupName" />

GridView Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="false"
    android:focusable="false"
    android:descendantFocusability="blocksDescendants">

    <ImageView
        android:id="@+id/groupIcon"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:scaleType="fitCenter"
        android:focusableInTouchMode="false"
        android:focusable="false"
        />

</LinearLayout>
java
android
android-studio
gridview
asked on Stack Overflow May 11, 2020 by Oli Knight • edited May 11, 2020 by Oli Knight

2 Answers

0

You could either set a custom color as default in the Gridview element xml, like

<GridView
        android:id="@+id/iconGrid"
        android:background="@color/myColor"
        ...
        />

and then reference that from code whenever you want to reapply it,

item.setBackgroundColor(getResources().getColor(R.color.myColor));

or you could cache the default color and then use it later,

private int defaultColor = Color.TRANSPARENT;

Drawable background = item.getBackground();
if (background instanceof ColorDrawable)
    defaultColor = ((ColorDrawable) background).getColor();
answered on Stack Overflow May 11, 2020 by Themelis
0

Here's an example doing exactly what you want:

https://android--code.blogspot.com/2015/08/android-gridview-selected-item-color.html

in a nutshell, it looks like they keep a reference to the previous view and change it when a new selection is made.

answered on Stack Overflow May 11, 2020 by user2199860

User contributions licensed under CC BY-SA 3.0