IconListPreference.java revision 73e782de608cbe2ddffd75c055009ff2e208f78b
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.util.AttributeSet;
24
25/** A {@code ListPreference} where each entry has a corresponding icon. */
26public class IconListPreference extends ListPreference {
27    private Drawable mIcons[];
28    private Resources mResources;
29
30    public IconListPreference(Context context, AttributeSet attrs) {
31        super(context, attrs);
32        TypedArray a = context.obtainStyledAttributes(
33                attrs, R.styleable.IconListPreference, 0, 0);
34        mResources = context.getResources();
35        setIcons(a.getResourceId(R.styleable.IconListPreference_icons, 0));
36        a.recycle();
37    }
38
39    public Drawable[] getIcons() {
40        return mIcons;
41    }
42
43    private void setIcons(int iconsRes) {
44        TypedArray array = mResources.obtainTypedArray(iconsRes);
45        int n = array.length();
46        Drawable drawable[] = new Drawable[n];
47        for (int i = 0; i < n; ++i) {
48            int id = array.getResourceId(i, 0);
49            drawable[i] = id == 0 ? null : mResources.getDrawable(id);
50        }
51        array.recycle();
52        mIcons = drawable;
53    }
54
55    public void setIcons(Drawable[] icons) {
56        mIcons = icons;
57    }
58}
59