Android – How to make clickable image open a web page

It’s very easily to allow a user to touch an image and cause the touch event to open a web page:

First, at the image to your main.xml file:

<ImageView

android:id=”@+id/my_button”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_gravity=”center_horizontal”

android:src=”@drawable/logo”/>

Then set up a listener for the “on click” in your code:

ImageView img = (ImageView) findViewById(R.id.my_button);

img.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// open the desired page

Intent browserIntent = new Intent(“android.intent.action.VIEW”,

Uri.parse(“http://www.gonzobrains.com”));

startActivity(browserIntent);

}

});

One thought on “Android – How to make clickable image open a web page

  1. I don’t know who you are. Thanks a lot! The tut works now! Also, do mention the Import files needed. I searched a lot to find those.

    Cheers :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>