TabsUtil.java :  » Geo » geonote » org » geonote » util » Android Open Source

Android Open Source » Geo » geonote 
geonote » org » geonote » util » TabsUtil.java
/**
 * Copyright 2010 Mark Wyszomierski
 */

package org.geonote.util;

import org.geonote.R;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

/**
 * Acts as an interface to the TabSpec class for setting the content view. The
 * level 3 SDK doesn't support setting a View for the content sections of the
 * tab, so we can only use the big native tab style. The level 4 SDK and up
 * support specifying a custom view for the tab.
 */
public abstract class TabsUtil {

    private static void setTabIndicator(TabSpec spec, String title, Drawable drawable, View view) {
        int sdk = new Integer(Build.VERSION.SDK).intValue();
        if (sdk < 4) {
            TabsUtil3.setTabIndicator(spec, title, drawable);
        } else {
            TabsUtil4.setTabIndicator(spec, view);
        }
    }

    public static void addTab(TabHost host, String title, int drawable, int index, int layout) {
        TabHost.TabSpec spec = host.newTabSpec("tab" + index);
        spec.setContent(layout);
        View view = prepareTabView(host.getContext(), title, drawable);
        TabsUtil.setTabIndicator(spec, title, host.getContext().getResources()
                .getDrawable(drawable), view);
        host.addTab(spec);
    }

    public static void addTab(TabHost host, String title, int drawable, int index, Intent intent) {
        TabHost.TabSpec spec = host.newTabSpec("tab" + index);
        spec.setContent(intent);
        View view = prepareTabView(host.getContext(), title, drawable);
        TabsUtil.setTabIndicator(spec, title, host.getContext().getResources()
                .getDrawable(drawable), view);
        host.addTab(spec);
    }

    private static View prepareTabView(Context context, String text, int drawable) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_main_nav, null);
        TextView tv = (TextView) view.findViewById(R.id.tvTitle);
        tv.setText(text);
        ImageView iv = (ImageView) view.findViewById(R.id.ivIcon);
        iv.setImageResource(drawable);
        return view;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.