1

I have got a cool image (fancy with rounded corners). I want to implement that image in my android app. When clicked on it, I want it to darken a bit (like a button when pressed) and launch a new activity.

How can I do that and what should I use (ImageView, ImageButton or just Button) ?

user1420042
  • 1,689
  • 9
  • 35
  • 53
  • Use ImageButton and change the image background to transparent (#00000000). Then create New drawable, with your cool image and the dark version of your cool image :) – Anis BEN NSIR Sep 28 '12 at 16:27
  • Wait, wait... how do i do that? set the background to #000000, ok. and then? – user1420042 Sep 28 '12 at 16:31

3 Answers3

5

You'll require two version of your cool image call it as active.png (clicked state i.e darken a bit) and inactive.png (normal state). in the drawable folder create a Selector file for them as follows Like : /drawable/cool_button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@drawable/active"/> 
    <item android:drawable="@drawable/inactive"/>

</selector>

and then user the ImageButton like this:

<ImageButton
    android:id="@+id/coolImageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/cool_button_selector"
    android:background="@null" />

Added: android:background="@null" to <ImageButton>

Hope this helps :)

Vishal Vyas
  • 2,571
  • 1
  • 22
  • 39
1

You can use any of the above depending on what your requirements are. For button like behavior, you can use an ImageButton, Here's the instructions to change the image onTouch

change button image in android

You can similarly change the image of ImageView.

ImageView img = (ImageView) getViewById(id-here);
img.setImageResource(R.drawable.my_image);
Community
  • 1
  • 1
rizalp1
  • 6,346
  • 2
  • 17
  • 19
  • And what happens with that button when I return to my activity ? Then it still would be the old "when-clicked" image. – user1420042 Sep 28 '12 at 16:29
0

you can use a normal Imageview with an onClickListener, the imageview's drawable will be a state list drawable xml file that comes with its own built in event listeners which can be linked to separate image assets.

CQM
  • 42,592
  • 75
  • 224
  • 366