Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8404103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:30:49+00:00 2026-06-09T22:30:49+00:00

I have a TabHost with five tabs, we’ll call it tab 1-5. I would

  • 0

I have a TabHost with five tabs, we’ll call it tab 1-5.

I would like Tab 3 to stay portrait – I don’t want it to ever go landscape.

How would I achieve this?

Here is my current code (Main.class):

public class Main extends FragmentActivity {
    TabHost mTabHost;
    TabManager mTabManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();    

        mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);

        addTab(mTabManager, mTabHost, new String("Tab 1"), new String("tab1"),
            Tab1.class);
        addTab(mTabManager, mTabHost, new String("Tab 2"), new String("tab2"),
            Tab2.class);
        addTab(mTabManager, mTabHost, new String("Tab 3"), new String("tab3"),
            Tab3.class);
        addTab(mTabManager, mTabHost, new String("Tab 4"), new String("tab4"),
            Tab4.class);
        addTab(mTabManager, mTabHost, new String("Contact"), new String("contact"),
            Tab5.class);

        mTabHost.getTabWidget().getChildTabViewAt(0).setFocusable(false);
        mTabHost.getTabWidget().getChildTabViewAt(1).setFocusable(false);
        mTabHost.getTabWidget().getChildTabViewAt(2).setFocusable(false);
        mTabHost.getTabWidget().getChildTabViewAt(3).setFocusable(false);
        mTabHost.getTabWidget().getChildTabViewAt(4).setFocusable(false);
        mTabHost.setSelected(false);
        mTabHost.getTabWidget().setSelected(false);

        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("tab", mTabHost.getCurrentTabTag());
    }

    private static View prepareTabView(Context context, String text) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_bg, null);
        ((TextView) view.findViewById(R.id.tabsText)).setText(text);
        return view;
    }

    public static void addTab(TabManager manager, TabHost host, String title, String tag, Class<?> cl) {
        TabHost.TabSpec spec = host.newTabSpec(tag);
        View view = prepareTabView(host.getContext(), title);
        spec.setIndicator(view);

        manager.addTab(spec, cl, null);
    }

    public static class TabManager implements TabHost.OnTabChangeListener {
        private final FragmentActivity mActivity;
        private final TabHost mTabHost;
        private final int mContainerId;
        private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
        TabInfo mLastTab;

        static final class TabInfo {
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;
            private Fragment fragment;

            TabInfo(String _tag, Class<?> _class, Bundle _args) {
                tag = _tag;
                clss = _class;
                args = _args;
            }
        }

        static class DummyTabFactory implements TabHost.TabContentFactory {
            private final Context mContext;

            public DummyTabFactory(Context context) {
                mContext = context;
            }

            @Override
            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }

        public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) {
            mActivity = activity;
            mTabHost = tabHost;
            mContainerId = containerId;
            mTabHost.setOnTabChangedListener(this);
        }

        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
            tabSpec.setContent(new DummyTabFactory(mActivity));
            String tag = tabSpec.getTag();

            TabInfo info = new TabInfo(tag, clss, args);

            info.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);
            if (info.fragment != null && !info.fragment.isDetached()) {
                FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
                ft.detach(info.fragment);
                ft.commit();
            }

            mTabs.put(tag, info);
            mTabHost.addTab(tabSpec);
        }

        @Override
        public void onTabChanged(String tabId) {
            TabInfo newTab = mTabs.get(tabId);
            if (mLastTab != newTab) {
                FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
                if (mLastTab != null) {
                    if (mLastTab.fragment != null) {
                        ft.detach(mLastTab.fragment);
                    }
                }
                if (newTab != null) {
                    if (newTab.fragment == null) {
                        newTab.fragment = Fragment.instantiate(mActivity,
                            newTab.clss.getName(), newTab.args);
                        ft.add(mContainerId, newTab.fragment, newTab.tag);
                    } else {
                        ft.attach(newTab.fragment);
                    }
                }

                mLastTab = newTab;
                ft.commit();
                mActivity.getSupportFragmentManager().executePendingTransactions();
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:30:50+00:00Added an answer on June 9, 2026 at 10:30 pm

    I’m assuming from your AndroidManiest.xml file that each of your tabs are activities.
    If so, then take a look at Activity.setRequestedOrientation(int)

    In your Tab3 Activity class you can lock your activity in to portrait mode using the following code :

    public class Tab3 extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
    

    In all your other Tab activities you can turn ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tabhost with three tabs. Each is an activity. I would like
I have a tabhost with two tabs. Each tab has his own activity. My
I have an application with various tabs (on a tabhost), each tab is an
I have a tabhost with 4 tabs. Each tab has several editboxes. On real
I have tabhost with 4 tabs. If I (for example) open tab #3, start
I have a tabhost with some tabs, and each tab have implemented the method
I have TabHost with three tabs. One of these tabs contains ListActivity. I want
I need to have a TabHost consisting of two tabs where each tab represented
I have tabhost and in 1 tab I want to do the following: 1.
In my application I have a tabHost with 5 tabs. And I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.