diff options
author | Maurice Lam <yukl@google.com> | 2016-03-25 15:46:45 -0700 |
---|---|---|
committer | Maurice Lam <yukl@google.com> | 2016-03-29 11:11:37 -0700 |
commit | 00358e4d12e6c7ba0f1da1fa9ad57f87da9b3b1a (patch) | |
tree | a60e81ff92351e0e649b1c6461f3c49bc38f1b35 /library/main | |
parent | eb85a47ae17061a4ed20d5a5fba4c61e24f17013 (diff) | |
download | setupwizard-00358e4d12e6c7ba0f1da1fa9ad57f87da9b3b1a.tar.gz |
[SuwLib] Implement ButtonBarItem
Implement ButtonBarItem which can contain one or more buttons to be
displayed in a horizontal row.
Bug: 27600894
Change-Id: Id3879ee8fc0bd09c04bc8d98a0bbd301d1b410b8
Diffstat (limited to 'library/main')
7 files changed, 313 insertions, 3 deletions
diff --git a/library/main/res/layout/suw_items_button_bar.xml b/library/main/res/layout/suw_items_button_bar.xml new file mode 100644 index 0000000..48414a2 --- /dev/null +++ b/library/main/res/layout/suw_items_button_bar.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright (C) 2015 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + style="@style/SuwItemContainer" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:gravity="end" + android:orientation="horizontal" /> diff --git a/library/main/res/values-v21/styles.xml b/library/main/res/values-v21/styles.xml index fddfdc7..88c39d4 100644 --- a/library/main/res/values-v21/styles.xml +++ b/library/main/res/values-v21/styles.xml @@ -54,6 +54,16 @@ <item name="android:textAppearance">?android:attr/textAppearanceListItemSmall</item> </style> + <!-- Button styles --> + + <style name="SuwButtonItem" /> + + <style name="SuwButtonItem.Colored"> + <item name="android:buttonStyle">@android:style/Widget.Material.Button</item> + <item name="android:colorButtonNormal">?android:attr/colorAccent</item> + <item name="android:textColor">?android:attr/textColorPrimaryInverse</item> + </style> + <!-- Navigation bar styles --> <style name="SuwNavBarButtonStyle" parent="@android:style/Widget.Material.Button.Borderless"> diff --git a/library/main/res/values/attrs.xml b/library/main/res/values/attrs.xml index fb559da..8501e94 100644 --- a/library/main/res/values/attrs.xml +++ b/library/main/res/values/attrs.xml @@ -108,4 +108,11 @@ <attr name="suwDividerCondition" /> </declare-styleable> + <declare-styleable name="SuwButtonItem"> + <attr name="android:buttonStyle" /> + <attr name="android:enabled" /> + <attr name="android:text" /> + <attr name="android:theme" /> + </declare-styleable> + </resources> diff --git a/library/main/src/com/android/setupwizardlib/items/ButtonBarItem.java b/library/main/src/com/android/setupwizardlib/items/ButtonBarItem.java new file mode 100644 index 0000000..55bbe75 --- /dev/null +++ b/library/main/src/com/android/setupwizardlib/items/ButtonBarItem.java @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.setupwizardlib.items; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.View; +import android.widget.Button; +import android.widget.LinearLayout; + +import com.android.setupwizardlib.R; + +import java.util.ArrayList; + +/** + * A list item with one or more buttons, declared as + * {@link com.android.setupwizardlib.items.ButtonItem}. + * + * <p>Example usage: + * <pre>{@code + * <ButtonBarItem> + * + * <ButtonItem + * android:id="@+id/skip_button" + * android:text="@string/skip_button_label /> + * + * <ButtonItem + * android:id="@+id/next_button" + * android:text="@string/next_button_label + * android:theme="@style/SuwButtonItem.Colored" /> + * + * </ButtonBarItem> + * }</pre> + */ +public class ButtonBarItem extends AbstractItem implements ItemInflater.ItemParent { + + private final ArrayList<ButtonItem> mButtons = new ArrayList<>(); + private boolean mVisible = true; + + public ButtonBarItem() { + super(); + } + + public ButtonBarItem(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + public int getCount() { + return isVisible() ? 1 : 0; + } + + @Override + public boolean isEnabled() { + // The children buttons are enabled and clickable, but the item itself is not + return false; + } + + @Override + public int getLayoutResource() { + return R.layout.suw_items_button_bar; + } + + public void setVisible(boolean visible) { + mVisible = visible; + } + + public boolean isVisible() { + return mVisible; + } + + public int getViewId() { + return getId(); + } + + @Override + public void onBindView(View view) { + // Note: The efficiency could be improved by trying to recycle the buttons created by + // ButtonItem + final LinearLayout layout = (LinearLayout) view; + layout.removeAllViews(); + + for (ButtonItem buttonItem : mButtons) { + Button button = buttonItem.createButton(layout); + layout.addView(button); + } + + view.setId(getViewId()); + } + + @Override + public void addChild(ItemHierarchy child) { + if (child instanceof ButtonItem) { + mButtons.add((ButtonItem) child); + } else { + throw new UnsupportedOperationException("Cannot add non-button item to Button Bar"); + } + } + + @Override + public ItemHierarchy findItemById(int id) { + if (getId() == id) { + return this; + } + for (ButtonItem button : mButtons) { + final ItemHierarchy item = button.findItemById(id); + if (item != null) { + return item; + } + } + return null; + } +} diff --git a/library/main/src/com/android/setupwizardlib/items/ButtonItem.java b/library/main/src/com/android/setupwizardlib/items/ButtonItem.java new file mode 100644 index 0000000..4faeff4 --- /dev/null +++ b/library/main/src/com/android/setupwizardlib/items/ButtonItem.java @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.setupwizardlib.items; + +import android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; +import android.view.ContextThemeWrapper; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; + +import com.android.setupwizardlib.R; + +/** + * Description of a button inside {@link com.android.setupwizardlib.items.ButtonBarItem}. This item + * will not be bound by the adapter, and must be a child of {@code ButtonBarItem}. + */ +public class ButtonItem extends AbstractItem implements View.OnClickListener { + + public interface OnClickListener { + void onClick(ButtonItem item); + } + + private boolean mEnabled = true; + private CharSequence mText; + private int mTheme = R.style.SuwButtonItem; + private OnClickListener mListener; + + private Button mButton; + + public ButtonItem() { + super(); + } + + public ButtonItem(Context context, AttributeSet attrs) { + super(context, attrs); + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuwButtonItem); + mEnabled = a.getBoolean(R.styleable.SuwButtonItem_android_enabled, true); + mText = a.getText(R.styleable.SuwButtonItem_android_text); + mTheme = a.getResourceId(R.styleable.SuwButtonItem_android_theme, R.style.SuwButtonItem); + a.recycle(); + } + + public void setOnClickListener(OnClickListener listener) { + mListener = listener; + } + + public void setText(CharSequence text) { + mText = text; + } + + public CharSequence getText() { + return mText; + } + + /** + * The theme to use for this button. This can be used to create button of a particular style + * (e.g. a colored or borderless button). Typically {@code android:buttonStyle} will be set in + * the theme to change the style applied by the button. + * + * @param theme Resource ID of the theme + */ + public void setTheme(int theme) { + mTheme = theme; + mButton = null; + } + + /** + * @return Resource ID of the theme used by this button. + */ + public int getTheme() { + return mTheme; + } + + public void setEnabled(boolean enabled) { + mEnabled = enabled; + } + + @Override + public int getCount() { + return 0; + } + + @Override + public boolean isEnabled() { + return mEnabled; + } + + @Override + public int getLayoutResource() { + return 0; + } + + /** + * Do not use this since ButtonItem is not directly part of a list. + */ + @Override + public final void onBindView(View view) { + throw new UnsupportedOperationException("Cannot bind to ButtonItem's view"); + } + + protected Button createButton(ViewGroup parent) { + if (mButton == null) { + Context context = parent.getContext(); + if (mTheme != 0) { + context = new ContextThemeWrapper(context, mTheme); + } + mButton = new Button(context); + mButton.setOnClickListener(this); + } + mButton.setEnabled(mEnabled); + mButton.setText(mText); + return mButton; + } + + @Override + public void onClick(View v) { + if (mListener != null) { + mListener.onClick(this); + } + } +} diff --git a/library/main/src/com/android/setupwizardlib/items/ItemGroup.java b/library/main/src/com/android/setupwizardlib/items/ItemGroup.java index 3e500bb..e449a95 100644 --- a/library/main/src/com/android/setupwizardlib/items/ItemGroup.java +++ b/library/main/src/com/android/setupwizardlib/items/ItemGroup.java @@ -23,7 +23,8 @@ import android.util.SparseIntArray; import java.util.ArrayList; import java.util.List; -public class ItemGroup extends AbstractItemHierarchy implements ItemHierarchy.Observer { +public class ItemGroup extends AbstractItemHierarchy implements ItemInflater.ItemParent, + ItemHierarchy.Observer { /* static section */ @@ -101,6 +102,7 @@ public class ItemGroup extends AbstractItemHierarchy implements ItemHierarchy.Ob /** * Add a child hierarchy to this item group. */ + @Override public void addChild(ItemHierarchy child) { mChildren.add(child); child.registerObserver(this); diff --git a/library/main/src/com/android/setupwizardlib/items/ItemInflater.java b/library/main/src/com/android/setupwizardlib/items/ItemInflater.java index 6bd77ac..4789261 100644 --- a/library/main/src/com/android/setupwizardlib/items/ItemInflater.java +++ b/library/main/src/com/android/setupwizardlib/items/ItemInflater.java @@ -27,6 +27,10 @@ public class ItemInflater extends GenericInflater<ItemHierarchy> { private static final String TAG = "ItemInflater"; + public interface ItemParent { + void addChild(ItemHierarchy child); + } + private final Context mContext; public ItemInflater(Context context) { @@ -50,8 +54,8 @@ public class ItemInflater extends GenericInflater<ItemHierarchy> { @Override protected void onAddChildItem(ItemHierarchy parent, ItemHierarchy child) { - if (parent instanceof ItemGroup) { - ((ItemGroup) parent).addChild(child); + if (parent instanceof ItemParent) { + ((ItemParent) parent).addChild(child); } else { throw new IllegalArgumentException("Cannot add child item to " + parent); } |