michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.widget; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import android.content.Context; michael@0: import android.content.res.TypedArray; michael@0: import android.util.AttributeSet; michael@0: import android.view.LayoutInflater; michael@0: import android.view.View; michael@0: import android.widget.ImageButton; michael@0: import android.widget.TextView; michael@0: import android.widget.TabWidget; michael@0: michael@0: public class IconTabWidget extends TabWidget { michael@0: private OnTabChangedListener mListener; michael@0: private final int mButtonLayoutId; michael@0: private final boolean mIsIcon; michael@0: michael@0: public static interface OnTabChangedListener { michael@0: public void onTabChanged(int tabIndex); michael@0: } michael@0: michael@0: public IconTabWidget(Context context, AttributeSet attrs) { michael@0: super(context, attrs); michael@0: michael@0: TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconTabWidget); michael@0: mButtonLayoutId = a.getResourceId(R.styleable.IconTabWidget_android_layout, 0); michael@0: mIsIcon = (a.getInt(R.styleable.IconTabWidget_display, 0x00) == 0x00); michael@0: a.recycle(); michael@0: michael@0: if (mButtonLayoutId == 0) { michael@0: throw new RuntimeException("You must supply layout attribute"); michael@0: } michael@0: } michael@0: michael@0: public void addTab(int imageResId, int stringResId) { michael@0: View button = LayoutInflater.from(getContext()).inflate(mButtonLayoutId, this, false); michael@0: if (mIsIcon) { michael@0: ((ImageButton) button).setImageResource(imageResId); michael@0: button.setContentDescription(getContext().getString(stringResId)); michael@0: } else { michael@0: ((TextView) button).setText(getContext().getString(stringResId)); michael@0: } michael@0: michael@0: addView(button); michael@0: button.setOnClickListener(new TabClickListener(getTabCount() - 1)); michael@0: button.setOnFocusChangeListener(this); michael@0: } michael@0: michael@0: public void setTabSelectionListener(OnTabChangedListener listener) { michael@0: mListener = listener; michael@0: } michael@0: michael@0: @Override michael@0: public void onFocusChange(View view, boolean hasFocus) { michael@0: } michael@0: michael@0: private class TabClickListener implements OnClickListener { michael@0: private final int mIndex; michael@0: michael@0: public TabClickListener(int index) { michael@0: mIndex = index; michael@0: } michael@0: michael@0: @Override michael@0: public void onClick(View view) { michael@0: if (mListener != null) michael@0: mListener.onTabChanged(mIndex); michael@0: } michael@0: } michael@0: }