Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight ImageView when focused or clicked?

A good example of this is either on the Twitter launch screen (the screen with the large icons that is seen when the application is first launch) or even just look at the application tray when you focus an application icon.

Basically I need to highlight an ImageView where the highlight contours to the image within the ImageView and looks like it's a border to that image. I would also like to customize the highlight to have it be a certain color and for it to fade out.

Thanks,

groomsy

like image 762
groomsy Avatar asked Nov 15 '10 15:11

groomsy


People also ask

How do you highlight in ImageView?

You need to assign the src attribute of the ImageView a state list drawable. In other words, that state list would have a different image for selected, pressed, not selected, etc. - that's how the Twitter App does it. The Google IO Schedule app has a good example of this.

Can ImageView be clickable?

For example, you can make an ImageView act like a simple Button by adding android:onClick to the ImageView . In this task you make the images in your layout clickable.


2 Answers

You need to assign the src attribute of the ImageView a state list drawable. In other words, that state list would have a different image for selected, pressed, not selected, etc. - that's how the Twitter App does it.

So if you had an ImageView:

<ImageView style="@style/TitleBarLogo"             android:contentDescription="@string/description_logo"             android:src="@drawable/title_logo" /> 

The src drawable (title_logo.xml) would look like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/title_logo_pressed"/>     <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/title_logo_pressed"/>     <item android:state_focused="true" android:drawable="@drawable/title_logo_selected"/>     <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/title_logo_default"/> </selector> 

The Google IO Schedule app has a good example of this.

like image 176
Josh Clemm Avatar answered Sep 20 '22 23:09

Josh Clemm


If you don't have another drawable for the pressed state you can use setColorFilterto achieve a simple tint effect.

It behaves just like pressed state selector so when the image is pressed it changes the background to light grey color.

final ImageView image = (ImageView) findViewById(R.id.my_image); image.setOnTouchListener(new View.OnTouchListener() {         private Rect rect;          @Override         public boolean onTouch(View v, MotionEvent event) {             if(event.getAction() == MotionEvent.ACTION_DOWN){                 image.setColorFilter(Color.argb(50, 0, 0, 0));                 rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());             }             if(event.getAction() == MotionEvent.ACTION_UP){                 image.setColorFilter(Color.argb(0, 0, 0, 0));             }             if(event.getAction() == MotionEvent.ACTION_MOVE){                 if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){                     image.setColorFilter(Color.argb(0, 0, 0, 0));                 }              }             return false;         }     }); 

It handles moving finger outside the view boundaries, thus if it occurs, it restores a default background.

It's important to return false from onTouch method when you want to support onClickListner too.

like image 35
klimat Avatar answered Sep 18 '22 23:09

klimat