mobile/android/base/widget/IconTabWidget.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial