Mehmet Ethem SULAN :: Android :: How to open URL/Website on image click Android Sample

Android ile image click edildiğinde istenen websiteyi açmaya çalıştım.

android click image


Android Load Image From URL in Gallery Example örneğine bakarsanız daha iyi analrsınız.
public void onClick(View v){} metodu OnClickListener interfacesinde tanimlidir. imageView.setOnClickListener(this); ile listener ekliyorsunuz.
OpenUrlActivity.java

package com.open.website;

import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class OpenUrlActivity extends Activity implements OnClickListener{
    
	private ImageView imageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView=(ImageView) findViewById(R.id.imageView1_url);
        imageView.setOnClickListener(this);

    }
    
	@Override
	protected void onResume() {
		super.onResume();
		String url="http://www.ethemsulan.com/wp-content/themes/mahinhin/images/kelime.png";
		imageView.setBackgroundDrawable(loadImageFromURL(url));
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imageView1_url:
//Image tiklandiginda ethemsulan.com sitesini aciyor tarayicidan.
    		Intent intent_website=new Intent(Intent.ACTION_VIEW);
    		intent_website.setData(Uri.parse("http://www.ethemsulan.com/"));
    		startActivity(intent_website);
			break;
		}
		
	}

	 private Drawable loadImageFromURL(String url)
	    {
	    try
	    {
	    InputStream is = (InputStream) new URL(url).getContent();
	    Drawable d = Drawable.createFromStream(is, "src");
	    return d;
	    }catch (Exception e) {
	    System.out.println(e);
	    return null;
	    }
	 }

    
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="horizontal">
    <ImageView android:id="@+id/imageView1_url" android:layout_height="180dip" android:layout_gravity="center_vertical" android:layout_width="match_parent"></ImageView>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.open.website"
      android:versionCode="1"
      android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".OpenUrlActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Downlaod OpenUrl.tar.gz

Write a Comment

Let me know what you think?