|
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/. */ |
|
4 |
|
5 package org.mozilla.gecko.widget; |
|
6 |
|
7 import org.mozilla.gecko.R; |
|
8 |
|
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; |
|
17 |
|
18 public class IconTabWidget extends TabWidget { |
|
19 private OnTabChangedListener mListener; |
|
20 private final int mButtonLayoutId; |
|
21 private final boolean mIsIcon; |
|
22 |
|
23 public static interface OnTabChangedListener { |
|
24 public void onTabChanged(int tabIndex); |
|
25 } |
|
26 |
|
27 public IconTabWidget(Context context, AttributeSet attrs) { |
|
28 super(context, attrs); |
|
29 |
|
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(); |
|
34 |
|
35 if (mButtonLayoutId == 0) { |
|
36 throw new RuntimeException("You must supply layout attribute"); |
|
37 } |
|
38 } |
|
39 |
|
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 } |
|
48 |
|
49 addView(button); |
|
50 button.setOnClickListener(new TabClickListener(getTabCount() - 1)); |
|
51 button.setOnFocusChangeListener(this); |
|
52 } |
|
53 |
|
54 public void setTabSelectionListener(OnTabChangedListener listener) { |
|
55 mListener = listener; |
|
56 } |
|
57 |
|
58 @Override |
|
59 public void onFocusChange(View view, boolean hasFocus) { |
|
60 } |
|
61 |
|
62 private class TabClickListener implements OnClickListener { |
|
63 private final int mIndex; |
|
64 |
|
65 public TabClickListener(int index) { |
|
66 mIndex = index; |
|
67 } |
|
68 |
|
69 @Override |
|
70 public void onClick(View view) { |
|
71 if (mListener != null) |
|
72 mListener.onTabChanged(mIndex); |
|
73 } |
|
74 } |
|
75 } |