Skip to content

Instantly share code, notes, and snippets.

@teppeihomma
Created September 29, 2012 14:53
Show Gist options
  • Save teppeihomma/3804250 to your computer and use it in GitHub Desktop.
Save teppeihomma/3804250 to your computer and use it in GitHub Desktop.
指定したURLを開くImageView Widget
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.app"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.app.widget.LinkImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:contentDescription="@null"
android:src="@drawable/logo"
app:href="@string/url" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="href" format="string" />
<declare-styleable name="LinkImageView">
<attr name="href" />
</declare-styleable>
</resources>
package com.example.app.widget;
import com.example.app.R;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
public class LinkImageView extends ImageView implements View.OnClickListener {
private String url;
public LinkImageView(Context context) {
this(context, null);
}
public LinkImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LinkImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.LinkImageView);
url = a.getString(R.styleable.LinkImageView_href);
a.recycle();
this.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Context context = getContext();
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment