IconListPreference.java revision 271b3095b9f763421c0547109da9de774795072d
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.camera; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.content.res.TypedArray; 22import android.graphics.drawable.Drawable; 23import android.preference.ListPreference; 24import android.util.AttributeSet; 25 26/** A {@code ListPreference} where each entry has a corresponding icon. */ 27public class IconListPreference extends ListPreference { 28 private Drawable mIcons[]; 29 private Resources mResources; 30 31 public IconListPreference(Context context, AttributeSet attrs) { 32 super(context, attrs); 33 TypedArray a = context.obtainStyledAttributes( 34 attrs, R.styleable.IconListPreference, 0, 0); 35 mResources = context.getResources(); 36 setIcons(a.getResourceId(R.styleable.IconListPreference_icons, 0)); 37 a.recycle(); 38 } 39 40 public Drawable[] getIcons() { 41 return mIcons; 42 } 43 44 private void setIcons(int iconsRes) { 45 TypedArray array = mResources.obtainTypedArray(iconsRes); 46 int n = array.length(); 47 Drawable drawable[] = new Drawable[n]; 48 for (int i = 0; i < n; ++i) { 49 int id = array.getResourceId(i, 0); 50 drawable[i] = id == 0 ? null : mResources.getDrawable(id); 51 } 52 array.recycle(); 53 mIcons = drawable; 54 } 55 56 public void setIcons(Drawable[] icons) { 57 mIcons = icons; 58 } 59} 60