Created
September 29, 2012 14:53
-
-
Save teppeihomma/3804250 to your computer and use it in GitHub Desktop.
指定したURLを開くImageView Widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<attr name="href" format="string" /> | |
<declare-styleable name="LinkImageView"> | |
<attr name="href" /> | |
</declare-styleable> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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