ButtonItem.java revision a8e0755e1b526174fe42b0d7215d3c6a6106aca2
1/*
2 * Copyright (C) 2016 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.setupwizardlib.items;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.util.AttributeSet;
22import android.view.ContextThemeWrapper;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.Button;
26
27import com.android.setupwizardlib.R;
28
29/**
30 * Description of a button inside {@link com.android.setupwizardlib.items.ButtonBarItem}. This item
31 * will not be bound by the adapter, and must be a child of {@code ButtonBarItem}.
32 */
33public class ButtonItem extends AbstractItem implements View.OnClickListener {
34
35    public interface OnClickListener {
36        void onClick(ButtonItem item);
37    }
38
39    private boolean mEnabled = true;
40    private CharSequence mText;
41    private int mTheme = R.style.SuwButtonItem;
42    private OnClickListener mListener;
43
44    private Button mButton;
45
46    public ButtonItem() {
47        super();
48    }
49
50    public ButtonItem(Context context, AttributeSet attrs) {
51        super(context, attrs);
52        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwButtonItem);
53        mEnabled = a.getBoolean(R.styleable.SuwButtonItem_android_enabled, true);
54        mText = a.getText(R.styleable.SuwButtonItem_android_text);
55        mTheme = a.getResourceId(R.styleable.SuwButtonItem_android_theme, R.style.SuwButtonItem);
56        a.recycle();
57    }
58
59    public void setOnClickListener(OnClickListener listener) {
60        mListener = listener;
61    }
62
63    public void setText(CharSequence text) {
64        mText = text;
65    }
66
67    public CharSequence getText() {
68        return mText;
69    }
70
71    /**
72     * The theme to use for this button. This can be used to create button of a particular style
73     * (e.g. a colored or borderless button). Typically {@code android:buttonStyle} will be set in
74     * the theme to change the style applied by the button.
75     *
76     * @param theme Resource ID of the theme
77     */
78    public void setTheme(int theme) {
79        mTheme = theme;
80        mButton = null;
81    }
82
83    /**
84     * @return Resource ID of the theme used by this button.
85     */
86    public int getTheme() {
87        return mTheme;
88    }
89
90    public void setEnabled(boolean enabled) {
91        mEnabled = enabled;
92    }
93
94    @Override
95    public int getCount() {
96        return 0;
97    }
98
99    @Override
100    public boolean isEnabled() {
101        return mEnabled;
102    }
103
104    @Override
105    public int getLayoutResource() {
106        return 0;
107    }
108
109    /**
110     * Do not use this since ButtonItem is not directly part of a list.
111     */
112    @Override
113    public final void onBindView(View view) {
114        throw new UnsupportedOperationException("Cannot bind to ButtonItem's view");
115    }
116
117    /**
118     * Create a button according to this button item.
119     *
120     * @param parent The parent of the button, used to retrieve the theme and context for this
121     *               button.
122     * @return A button that can be added to the parent.
123     */
124    protected Button createButton(ViewGroup parent) {
125        if (mButton == null) {
126            Context context = parent.getContext();
127            if (mTheme != 0) {
128                context = new ContextThemeWrapper(context, mTheme);
129            }
130            mButton = new Button(context);
131            mButton.setOnClickListener(this);
132        } else {
133            if (mButton.getParent() instanceof ViewGroup) {
134                // A view cannot be added to a different parent if one already exists. Remove this
135                // button from its parent before returning.
136                ((ViewGroup) mButton.getParent()).removeView(mButton);
137            }
138        }
139        mButton.setEnabled(mEnabled);
140        mButton.setText(mText);
141        return mButton;
142    }
143
144    @Override
145    public void onClick(View v) {
146        if (mListener != null) {
147            mListener.onClick(this);
148        }
149    }
150}
151