mobile/android/base/widget/IconTabWidget.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/widget/IconTabWidget.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,75 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.widget;
     1.9 +
    1.10 +import org.mozilla.gecko.R;
    1.11 +
    1.12 +import android.content.Context;
    1.13 +import android.content.res.TypedArray;
    1.14 +import android.util.AttributeSet;
    1.15 +import android.view.LayoutInflater;
    1.16 +import android.view.View;
    1.17 +import android.widget.ImageButton;
    1.18 +import android.widget.TextView;
    1.19 +import android.widget.TabWidget;
    1.20 +
    1.21 +public class IconTabWidget extends TabWidget {
    1.22 +    private OnTabChangedListener mListener;
    1.23 +    private final int mButtonLayoutId;
    1.24 +    private final boolean mIsIcon;
    1.25 +
    1.26 +    public static interface OnTabChangedListener {
    1.27 +        public void onTabChanged(int tabIndex);
    1.28 +    }
    1.29 +
    1.30 +    public IconTabWidget(Context context, AttributeSet attrs) {
    1.31 +        super(context, attrs);
    1.32 +
    1.33 +        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconTabWidget);
    1.34 +        mButtonLayoutId = a.getResourceId(R.styleable.IconTabWidget_android_layout, 0);
    1.35 +        mIsIcon = (a.getInt(R.styleable.IconTabWidget_display, 0x00) == 0x00);
    1.36 +        a.recycle();
    1.37 +
    1.38 +        if (mButtonLayoutId == 0) {
    1.39 +            throw new RuntimeException("You must supply layout attribute");
    1.40 +        }
    1.41 +    }
    1.42 +
    1.43 +    public void addTab(int imageResId, int stringResId) {
    1.44 +        View button = LayoutInflater.from(getContext()).inflate(mButtonLayoutId, this, false);
    1.45 +        if (mIsIcon) {
    1.46 +            ((ImageButton) button).setImageResource(imageResId);
    1.47 +            button.setContentDescription(getContext().getString(stringResId));
    1.48 +        } else {
    1.49 +            ((TextView) button).setText(getContext().getString(stringResId));
    1.50 +        }
    1.51 +
    1.52 +        addView(button);
    1.53 +        button.setOnClickListener(new TabClickListener(getTabCount() - 1));
    1.54 +        button.setOnFocusChangeListener(this);
    1.55 +    }
    1.56 +
    1.57 +    public void setTabSelectionListener(OnTabChangedListener listener) {
    1.58 +        mListener = listener;
    1.59 +    }
    1.60 +
    1.61 +    @Override
    1.62 +    public void onFocusChange(View view, boolean hasFocus) {
    1.63 +    }
    1.64 +
    1.65 +    private class TabClickListener implements OnClickListener {
    1.66 +        private final int mIndex;
    1.67 +
    1.68 +        public TabClickListener(int index) {
    1.69 +            mIndex = index;
    1.70 +        }
    1.71 +
    1.72 +        @Override
    1.73 +        public void onClick(View view) {
    1.74 +            if (mListener != null)
    1.75 +                mListener.onTabChanged(mIndex);
    1.76 +        }
    1.77 +    }
    1.78 +}

mercurial