Skip to content

Instantly share code, notes, and snippets.

@udacityandroid
Created May 13, 2016 21:08
Show Gist options
  • Save udacityandroid/e9399aa9b3ffb7690d15b4b7fbe148db to your computer and use it in GitHub Desktop.
Save udacityandroid/e9399aa9b3ffb7690d15b4b7fbe148db to your computer and use it in GitHub Desktop.
Miwok App: Miwok app: List Item Views with pressed states
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a single list item -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/list_item_height"
android:background="@color/tan_background"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="@dimen/list_item_height"
android:layout_height="@dimen/list_item_height" />
<LinearLayout
android:id="@+id/text_container"
android:layout_width="match_parent"
android:layout_height="@dimen/list_item_height"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/image"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="@+id/miwok_text_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:textStyle="bold"
tools:text="lutti" />
<TextView
android:id="@+id/default_text_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
tools:text="one" />
</LinearLayout>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:src="@drawable/ic_play_arrow" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"/>
</RelativeLayout>
@tooptooptoop
Copy link

done

@KobusVorster
Copy link

Done done done

@kevalsanghani
Copy link

@sachinwork91
Using View with match_parent for width and height and selectableItemBackground works in this case because the ViewGroup is RelativeLayout.
In RelativeLayout, we can superimpose views on top of each other. So what we are doing in this case is creating a selectable view that
ripples on touch and superimposing it on the entire list item (because of match_parent width and height).
In activity_main, the ViewGroup is a LinearLayout which means that selectable view that you create cannot be superimposed on the category item. I hope this answer brings some clarity to your confusion.

Hey,
Shouldn't the superimposed view 'View' create a big ripple for entire parent that is the the RelativeLayout, not just the individual list items.

@kevalsanghani
Copy link

Can someone explain why android:drawSelectorOnTop="true" only works for word_list.XML and not on other XMLs?

@LuizPelegrini
Copy link

LuizPelegrini commented Sep 29, 2018

@sachinwork91
Using View with match_parent for width and height and selectableItemBackground works in this case because the ViewGroup is RelativeLayout.
In RelativeLayout, we can superimpose views on top of each other. So what we are doing in this case is creating a selectable view that
ripples on touch and superimposing it on the entire list item (because of match_parent width and height).
In activity_main, the ViewGroup is a LinearLayout which means that selectable view that you create cannot be superimposed on the category item. I hope this answer brings some clarity to your confusion.

Hey,
Shouldn't the superimposed view 'View' create a big ripple for entire parent that is the the RelativeLayout, not just the individual list items.

As you are handling with the item.layout only, be aware you are superimposed view 'View' for the item itself, not the whole screen

@LuizPelegrini
Copy link

option 1 didn't work for me either. Option 2 is the way to go

@kwonines
Copy link

Option 1 doesn't work for me also. It seems like that is a problem with lots of people. Switched to option 2 and it worked perfectly.

Copy link

ghost commented Jul 3, 2019

@sachinwork91
Using View with match_parent for width and height and selectableItemBackground works in this case because the ViewGroup is RelativeLayout.
In RelativeLayout, we can superimpose views on top of each other. So what we are doing in this case is creating a selectable view that
ripples on touch and superimposing it on the entire list item (because of match_parent width and height).
In activity_main, the ViewGroup is a LinearLayout which means that selectable view that you create cannot be superimposed on the category item. I hope this answer brings some clarity to your confusion.

thanks a lot i had the same problem

@AJM95
Copy link

AJM95 commented Aug 16, 2020

2020 I was here. pheeeww. This was a very long lesson guys. Phewww

@Abdelhaq-Bannar
Copy link

2021 ^^ i think i'm the last one hhh

@sarwarsateer
Copy link

anyone still here in 2021? lol
-Sarwar Sateer

Copy link

ghost commented Jun 25, 2021

Hey yo
Congrats on reaching this far everyone!!

@david-read-iii
Copy link

For those having issues with option 1, setting android:clickable="true" and android:focusable="true" helps in some cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment