1

I'm trying to add a black view with 0.5 opacity over the item in a listview when the item is pressed.

I've tried several things like using selectors to change the background but it doesn't work because each item on the list is an image that fills the entire item, and the image is over the background.

I've also tried:

mWebcamsListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            Log.i(TAG, "ITEM selected.");
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

            Log.i(TAG, "Nothing selected.");
        }
    });  

To change the opacity of the view but onItemSelected doesn't get triggered when I press the item, is there a way to get the view the user has pressed to change it's opacity?

List view:

<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="match_parent"
tools:context=".Webcams"
android:background="@android:color/black">

<ListView
    android:id="@+id/webcamsList"
    android:layout_width="match_parent"
    android:listSelector="@drawable/background_item_webcam"
    android:layout_height="wrap_content" />

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:visibility="visible" />

Background I tried just to change the color to red:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
    android:color="@android:color/holo_red_dark"
    />
</shape>

The selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"      android:drawable="@drawable/background_index_webcam"/>
</selector>

EDIT: I added the selector code, I think it doesn't work because the image is over the background I reckon I need to add a layer over the image rather than change the background, or get the view programmatically and change it's alpha.

EDIT2: I found the solution, as I thought the color was changing on the background all I needed to do is set android:drawSelectorOnTop="true" to draw the selector on top of the item's view

dan87
  • 333
  • 3
  • 16

3 Answers3

1

I think You will have to implement Listview's setOnItemClickListener listener not setOnItemSelectedListener as below:

  mWebcamsListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
             Log.i(TAG, "ITEM selected.");

        }
    });
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • That was the first thing I tried, but onitemclick is triggered when the user stops pressing the item – dan87 Oct 08 '14 at 09:13
1

there is 2 way for this issue first use a png transparent for click selector and another way is use set Alpha with gradient rectangle file

Meysam Valiolahi
  • 188
  • 1
  • 1
  • 12
1

try this in onItemSelected

view.setBackgroundColor(Color.parseColor("#80000000"));
Ron Davis
  • 346
  • 8
  • 28