Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.gecko.widget;
7 import org.mozilla.gecko.R;
9 import android.content.Context;
10 import android.content.res.TypedArray;
11 import android.util.AttributeSet;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.widget.ImageButton;
15 import android.widget.TextView;
16 import android.widget.TabWidget;
18 public class IconTabWidget extends TabWidget {
19 private OnTabChangedListener mListener;
20 private final int mButtonLayoutId;
21 private final boolean mIsIcon;
23 public static interface OnTabChangedListener {
24 public void onTabChanged(int tabIndex);
25 }
27 public IconTabWidget(Context context, AttributeSet attrs) {
28 super(context, attrs);
30 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconTabWidget);
31 mButtonLayoutId = a.getResourceId(R.styleable.IconTabWidget_android_layout, 0);
32 mIsIcon = (a.getInt(R.styleable.IconTabWidget_display, 0x00) == 0x00);
33 a.recycle();
35 if (mButtonLayoutId == 0) {
36 throw new RuntimeException("You must supply layout attribute");
37 }
38 }
40 public void addTab(int imageResId, int stringResId) {
41 View button = LayoutInflater.from(getContext()).inflate(mButtonLayoutId, this, false);
42 if (mIsIcon) {
43 ((ImageButton) button).setImageResource(imageResId);
44 button.setContentDescription(getContext().getString(stringResId));
45 } else {
46 ((TextView) button).setText(getContext().getString(stringResId));
47 }
49 addView(button);
50 button.setOnClickListener(new TabClickListener(getTabCount() - 1));
51 button.setOnFocusChangeListener(this);
52 }
54 public void setTabSelectionListener(OnTabChangedListener listener) {
55 mListener = listener;
56 }
58 @Override
59 public void onFocusChange(View view, boolean hasFocus) {
60 }
62 private class TabClickListener implements OnClickListener {
63 private final int mIndex;
65 public TabClickListener(int index) {
66 mIndex = index;
67 }
69 @Override
70 public void onClick(View view) {
71 if (mListener != null)
72 mListener.onTabChanged(mIndex);
73 }
74 }
75 }