-1

How can I make a list item have such a blue background when clicked?

enter image description here

Here is code from my fragment when i click on item:

override fun onListItemClick(itemIndex: Int, itemCode: String, itemViewId: Int) {
        val connectionGuid = (adapter.getItem(itemIndex) as ConnectionViewModel).guid
        proceedView.isEnabled = true
        proceedView.setOnClickListener { proceedConnection(connectionGuid) }
    }

And here is code of my adapter:

class ConnectionItemHolder(parent: ViewGroup, private val listener: ListItemClickListener?) :
    RecyclerView.ViewHolder(parent.inflateListItemView(R.layout.view_item_connection)) {

    private val logoImageView = itemView.findViewById<ImageView>(R.id.logoImageView)
    private val titleView = itemView.findViewById<TextView>(R.id.titleView)
    private val subTitleView = itemView.findViewById<TextView>(R.id.subTitleView)

    init {
        itemView.setOnClickListener {
            if (adapterPosition > RecyclerView.NO_POSITION)
                listener?.onListItemClick(itemIndex = adapterPosition)
        }
    }

    fun bind(item: ConnectionViewModel) {
        logoImageView.loadRoundedImage(
            imageUrl = item.logoUrl,
            placeholderId = R.drawable.ic_logo_bank_placeholder,
            cornerRadius = itemView.resources.getDimension(R.dimen.dp_12)
        )
        titleView.text = item.name
        subTitleView.text = item.statusDescription
        subTitleView.setTextColorResId(item.statusColorResId)
    }
}

And here is my layout view_item_connection:

<androidx.cardview.widget.CardView 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="wrap_content"
    android:layout_height="@dimen/dp_80"
    android:layout_marginStart="@dimen/dp_16"
    android:layout_marginEnd="@dimen/dp_16"
    android:clipToPadding="false"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardCornerRadius="@dimen/dp_6"
    app:cardElevation="@dimen/dp_20">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/app_logo_background">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginEnd="@dimen/dp_15"
            android:layout_toEndOf="@+id/logoImageView"
            android:orientation="vertical">

            <TextView
                android:id="@+id/titleView"
                style="@style/Text18Primary"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="bottom"
                android:maxLines="1"
                tools:text="Demobank" />

            <TextView
                android:id="@+id/subTitleView"
                style="@style/Text14Secondary"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_3"
                android:maxLines="1"
                tools:text="Connected on 12 sept. 2019, 18:02" />
        </LinearLayout>

        <ImageView
            android:id="@+id/logoImageView"
            style="@style/ConnectionLogoImage"
            android:layout_centerVertical="true"
            android:contentDescription="@null" />
    </RelativeLayout>
</androidx.cardview.widget.CardView>
Morozov
  • 4,968
  • 6
  • 39
  • 70

3 Answers3

1

create a xml in drawable folder like this

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/white" />
    <stroke
        android:width="1dp"
        android:color="@color/your_line_color" />
    <corners
        android:radius="10dp"
        />
</shape>

USAGE

in bindview() use like this

viewHolder.rootLayout.setBackgroundResource(R.drawable.stroke_background)
Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

First collect current selected item position:

 OnItemClickListener listViewOnItemClick = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
                mSelectedItem = position;
                mAdapter.notifyDataSetChanged();
        }
    };

Second override the getView method of your adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final View view = View.inflate(context, R.layout.item_list, null);

    if (position == mSelectedItem) {
        // set your color here
    }

    return view;
}
Shalu T D
  • 3,921
  • 2
  • 26
  • 37
0

If you use MaterialCardView from Google Material Components, you can do this:

<com.google.android.material.card.MaterialCardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:strokeColor="@color/card_stroke_color"
    app:strokeWidth="2dp"
    />

And color/card_stroke_color.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="?colorPrimary" android:alpha="0.5"/>
    <item android:color="@android:color/transparent" />
</selector>

And when binding your ViewHolder:

cardView.isChecked = item.checked
Nicolas
  • 6,611
  • 3
  • 29
  • 73
  • i used androidx.cardview.widget.CardView but com.google.android.material.card.MaterialCardView is a child as i see in official documentation, anyway i get next error: `AAPT: error: 'color/card_stroke_color' is incompatible with attribute strokeColor (attr) color.`, also i have next dependencies: implementation 'com.google.android.material:material:1.1.0' – Morozov Jun 04 '20 at 12:59
  • Sorry I made a typo. You need the `app:` namespace instead of `android:` for those two. – Nicolas Jun 04 '20 at 13:02
  • @color/card_stroke_color" mb @drawable/card_stroke_color" ? – Morozov Jun 04 '20 at 13:04
  • For `app:strokeColor` and `app:strokeWidth`. – Nicolas Jun 04 '20 at 13:06
  • get next error: `Error inflating class com.google.android.material.card.MaterialCardView`. when i tried to findViewById my `cardView` – Morozov Jun 04 '20 at 13:13
  • You need the `com.google.android.material:material` dependency. If you're not using Google Material Components already I don't recommend you use my solution. It doesn't make sense to use such a library for a single component, either use it in your whole app or don't. – Nicolas Jun 04 '20 at 13:16
  • i said that i use in my project this library: implementation 'com.google.android.material:material:1.1.0' – Morozov Jun 04 '20 at 13:21
  • @Morozov Are you using a Material Components Theme in your app? – Gabriele Mariotti Jun 05 '20 at 08:18