Created
June 1, 2011 09:41
-
-
Save solar/1002049 to your computer and use it in GitHub Desktop.
Android PreferenceScreen with icon image.
This file contains hidden or 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" | |
| 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> |
This file contains hidden or 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 org.sazabi.lib.preference; | |
| import android.content.Context; | |
| import android.graphics.drawable.Drawable; | |
| import android.preference.PreferenceScreen; | |
| import android.util.AttributeSet; | |
| import android.view.View; | |
| import android.widget.ImageView; | |
| public class IconPreference extends PreferenceScreen { | |
| 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(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not sure what API level this code is, but PreferenceScreen is now a final class :(
public final class PreferenceScreen