Muhammed Refaat
Muhammed Refaat

Reputation: 9103

making the displayed image only clickable in an imagebutton

I have an ImageButton in my Activity as follows:

 <ImageButton 
   android:contentDescription="@string/controller"
   android:id="@+id/control"
   android:layout_width="match_parent"
   android:layout_weight="3"
   android:layout_height="0dp"
   android:scaleType="fitCenter"
   android:background="@android:color/transparent"
   android:src="@drawable/displayed_image" />

regarding to some UI standardization required I can't assign the image width to any thing but match_parent, So, the resulted image is like:

All I want to do make only the displayed image is the clickable part of the Image and whole of the transparent-area is not clickable,so that when assigning an onClickListener for the ImageButton the action executed only if the clicking was in the displayed-image, is there's any static or programmatic way to afford that?

enter image description here

Upvotes: 0

Views: 78

Answers (3)

Plinio.Santos
Plinio.Santos

Reputation: 1759

You can set the image button height and width to wrap_content. Then add the property android:layout_gravity="center". Just it.

Upvotes: 1

laminatefish
laminatefish

Reputation: 5246

You could try using something like this: http://blahti.wordpress.com/2012/06/26/images-with-clickable-areas/ - But to be honest, there are other ways of doing it.

A couple of other ideas:

  1. Have 3 separate images within the container. A left and right piece and your clickable image piece. Combined they'd look exactly the same as the original single ImageView. The benefit is, that you'd be able to control the centre piece and ignore any other touches/clicks. This would probably require a RelativeLayout. I don't know if that goes against your UI concerns.

  2. Overlay a different view (ImageButton possibly?), to handle the click for you. So in effect you'd end up with something like: [ [] ] (Where the brackets represent each view - the larger of the two being your original).

Hope this helps.

Upvotes: 1

erik
erik

Reputation: 4958

why not wrap the imageButton in a RelativeLayout, that has a match_parent width and then have the imagebutton set to wrap_content and centerInParent

Upvotes: 0

Related Questions