Skip to content

Instantly share code, notes, and snippets.

@solar
Created June 1, 2011 09:41
Show Gist options
  • Select an option

  • Save solar/1002049 to your computer and use it in GitHub Desktop.

Select an option

Save solar/1002049 to your computer and use it in GitHub Desktop.

Revisions

  1. solar revised this gist Jun 1, 2011. 3 changed files with 49 additions and 14 deletions.
    47 changes: 33 additions & 14 deletions IconPreference.java
    Original file line number Diff line number Diff line change
    @@ -2,60 +2,79 @@

    import android.content.Context;

    import android.content.res.TypedArray;

    import android.graphics.drawable.Drawable;

    import android.preference.PreferenceScreen;
    import android.preference.Preference;

    import android.util.AttributeSet;

    import android.view.View;

    import android.widget.ImageView;

    public class IconPreference extends PreferenceScreen {
    /**
    * 左側にアイコンを表示するPreference.
    *
    * @author sora (shinpei.okamura@insprout.com)
    */
    public class IconPreference extends Preference {

    private Drawable icon = null;

    /**
    * @see Preference#Preference(Context,AttributeSet,int)
    * iconプロパティからリソースを読み込む.
    *
    * {@inheritDoc}
    * @see Preference#IconPreference(Context,AttributeSet,int)
    */
    public IconPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setLayoutResource(R.layout.icon_preference);

    TypedArray ta = context.obtainStyledAttributes(
    attrs, R.styleable.IconPreference, defStyle, 0);
    icon = ta.getDrawable(R.styleable.IconPreference_icon);
    }

    /**
    * @see Preference#Preference(Context,AttributeSet)
    * {@inheritDoc}
    * @see Preference#IconPreference(Context,AttributeSet)
    */
    public IconPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setLayoutResource(R.layout.icon_preference);
    this(context, attrs, 0);
    }

    /**
    * アイコンのImageViewを設定する.
    *
    * {@inheritDoc}
    * @see Preference#onBindView(View)
    */
    protected void onBindView(View view) {
    super.onBindView(view);

    ImageView image = (ImageView) view.findViewById(R.id.icon);
    if (image != null) {
    ImageView imageView = (ImageView) view.findViewById(R.id.icon);
    if (imageView != null) {
    if (icon != null) {
    image.setImageDrawable(icon);
    imageView.setImageDrawable(icon);
    } else {
    image.setVisibility(View.GONE);
    imageView.setVisibility(View.GONE);
    }
    }
    }

    /**
    * Set icon.
    *
    * アイコンを設定.
    *
    * @param icon
    */
    public void setIcon(Drawable icon) {
    this.icon = icon;
    notifyChanged();
    if (this.icon == null && icon != null
    || icon != null && !icon.equals(this.icon)) {
    this.icon = icon;
    notifyChanged();
    }
    }
    }
    6 changes: 6 additions & 0 deletions attrs.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <declare-styleable name="IconPreference">
    <attr name="icon" format="reference" />
    </declare-styleable>
    </resources>
    10 changes: 10 additions & 0 deletions settings.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:sazabi="http://schemas.android.com/apk/res/org.sazabi.lib.preference">

    <org.sazabi.lib.preference.IconPreference
    android:key="help"
    android:title="@string/pref_help"
    sazabi:icon="@drawable/icon" />

    </PreferenceScreen>
  2. solar revised this gist Jun 1, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions IconPreference.java
    Original file line number Diff line number Diff line change
    @@ -4,15 +4,15 @@

    import android.graphics.drawable.Drawable;

    import android.preference.Preference;
    import android.preference.PreferenceScreen;

    import android.util.AttributeSet;

    import android.view.View;

    import android.widget.ImageView;

    public class IconPreference extends Preference {
    public class IconPreference extends PreferenceScreen {

    private Drawable icon = null;

  3. solar created this gist Jun 1, 2011.
    61 changes: 61 additions & 0 deletions IconPreference.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    package org.sazabi.lib.preference;

    import android.content.Context;

    import android.graphics.drawable.Drawable;

    import android.preference.Preference;

    import android.util.AttributeSet;

    import android.view.View;

    import android.widget.ImageView;

    public class IconPreference extends Preference {

    private Drawable icon = null;

    /**
    * @see Preference#Preference(Context,AttributeSet,int)
    */
    public IconPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setLayoutResource(R.layout.icon_preference);
    }

    /**
    * @see Preference#Preference(Context,AttributeSet)
    */
    public IconPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setLayoutResource(R.layout.icon_preference);
    }

    /**
    * {@inheritDoc}
    * @see Preference#onBindView(View)
    */
    protected void onBindView(View view) {
    super.onBindView(view);

    ImageView image = (ImageView) view.findViewById(R.id.icon);
    if (image != null) {
    if (icon != null) {
    image.setImageDrawable(icon);
    } else {
    image.setVisibility(View.GONE);
    }
    }
    }

    /**
    * Set icon.
    *
    * @param icon
    */
    public void setIcon(Drawable icon) {
    this.icon = icon;
    notifyChanged();
    }
    }
    49 changes: 49 additions & 0 deletions icon_preference.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    <?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="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">

    <ImageView android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="6dp"
    android:layout_marginRight="6dp"
    android:layout_gravity="center" />

    <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="6dp"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="6dp"
    android:layout_weight="1">

    <TextView android:id="@+android:id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal" />

    <TextView android:id="@+android:id/summary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@android:id/title"
    android:layout_alignLeft="@android:id/title"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:maxLines="2"
    android:textColor="?android:attr/textColorSecondary" />

    </RelativeLayout>

    <LinearLayout android:id="@+android:id/widget_frame"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" />
    </LinearLayout>