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 7443091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:16:39+00:00 2026-05-29T11:16:39+00:00

I’ve been trying to update my app and get going with fragments, the action

  • 0

I’ve been trying to update my app and get going with fragments, the
action bar, and all the other UI features that I’m missing out on. I
understand I can have multiple fragments in an activity, have
different layouts based upon the device and all that good stuff but
I’m struggling with getting some tab stuff the way I want. I
understand how to add tabs, switching between them but how do I have
more than one fragment in a tab? So for example I have essentially two
screens I want the user to be able to switch back and forth from
easily (why I want to use tabs). If I have two separate activities I
can specify this in xml files and use setContentView using the layouts
below

tab1_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <fragment
        android:name="com.example.tabrefactor.Fragment1"
        android:id="@+id/fragment_1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
    <fragment
        android:name="com.example.tabrefactor.Fragment2"
        android:id="@+id/fragment_2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
    <fragment
        android:name="com.example.tabrefactor.Fragment3"
        android:id="@+id/fragment_3"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
</LinearLayout>

tab2_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <fragment
        android:name="com.example.tabrefactor.Fragment4"
        android:id="@+id/fragment_4"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
</LinearLayout>

I can convert the second layout to using tabs since its only contains
one fragment but I’m not sure how get the first layout into a single
tab. Is that something that’s allowed? Thanks in advance,

Jason Prenger

  • 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-05-29T11:16:41+00:00Added an answer on May 29, 2026 at 11:16 am

    I’ll leave this open incase someone has a simplification or a better idea…

    Eventually went with a workaround of having a base layout with 3 frame layouts…

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <FrameLayout
            android:id="@+id/fragment_sb"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <FrameLayout
            android:id="@+id/fragment_local"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <FrameLayout
            android:id="@+id/fragment_rest"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    
    </LinearLayout>
    

    Then in my activity with the tabs I made a custom TabListener that handled the changes between. The code I used is below (I’m using actionbarsherlock so it’ll look slightly different than the normal stuff)

    public class TabActivity extends FragmentActivity {
    
        Fragment1 fragment1;
        Fragment2 fragment2;
        Fragment3 fragment3;
        Fragment4 fragment4;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_layout);
    
            final ActionBar bar = getSupportActionBar();
            bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            bar.setDisplayShowTitleEnabled(true);
            bar.setDisplayShowHomeEnabled(false);
            bar.setTitle("Title");
    
            bar.addTab(bar.newTab()
                    .setIcon(R.drawable.ic_list_tab_selected)
                    .setTabListener(new ListTabListener(this)));
            bar.addTab(bar.newTab()
                    .setIcon(R.drawable.ic_map_tab_selected)
                    .setTabListener(new MapTabListener(this)));
    
            if (savedInstanceState != null) {
                bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
            }
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.test_menu, menu);
    
            return super.onCreateOptionsMenu(menu);
        }
    
        public static class ListTabListener implements ActionBar.TabListener {
            private static final String fragment1Tag = "fragment1_tag";
            private static final String fragment2Tag = "fragment2_tag";
            private static final String fragment3Tag = "fragment3_tag";
    
            private FragmentActivity activity;
            private Fragment1 fragment1;
            private Fragment2 fragment2;
            private Fragment3 fragment3;
    
            public ListTabListener(FragmentActivity activity) {
                this.activity = activity;
    
                FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
                fragment1 = (Fragment1) activity.getSupportFragmentManager().findFragmentByTag(fragment1Tag);
                if (fragment1 != null && !fragment1.isDetached()) {
                    ft.detach(fragment1);
                }
    
                fragment2 = (Fragment2) activity.getSupportFragmentManager().findFragmentByTag(fragment1Tag);
                if (fragment2 != null && !fragment2.isDetached()) {
                    ft.detach(fragment2);
                }
    
                fragment3 = (Fragment3) activity.getSupportFragmentManager().findFragmentByTag(fragment1Tag);
                if (fragment3 != null && !fragment3.isDetached()) {
                    ft.detach(fragment3);
                }
    
                ft.commit();
            }
    
            @Override
            public void onTabReselected(Tab tab, FragmentTransaction nullFt) {
                //Reselected don't do anything          
            }
    
            @Override
            public void onTabSelected(Tab tab, FragmentTransaction nullFt) {
                FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
    
                if(fragment1 == null) {
                    fragment1 = new Fragment1();
                    ft.add(R.id.fragment_sb, fragment1, fragment1Tag);
                } else {
                    ft.attach(fragment1);
                }
    
                if(fragment2 == null) {
                    fragment2 = new Fragment2();
                    ft.add(R.id.fragment_local, fragment2, fragment2Tag);
                } else {
                    ft.attach(fragment2);
                }
    
                if(fragment3 == null) {
                    fragment3 = new Fragment3();
                    ft.add(R.id.fragment_rest, fragment3, fragment3Tag);
                } else {
                    ft.attach(fragment3);
                }
    
                ft.commit();
            }
    
            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction nullFt) {
                FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
    
                if(fragment1 != null)
                    ft.detach(fragment1);
                if(fragment2 != null)
                    ft.detach(fragment2);
                if(fragment3 != null)
                    ft.detach(fragment3);
    
                ft.commit();
            }
        }
    
        public static class MapTabListener implements ActionBar.TabListener {
            private static final String fragment4Tag = "fragment4_tag";
    
            private FragmentActivity activity;
            private Fragment4 fragment4;
    
            public MapTabListener(FragmentActivity activity) {
                this.activity = activity;
    
                FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
                fragment4 = (Fragment4) activity.getSupportFragmentManager().findFragmentByTag(fragment4Tag);
                if (fragment4 != null && !fragment4.isDetached()) {
                    ft.detach(fragment4);
                }
                ft.commit();
            }
    
            @Override
            public void onTabReselected(Tab tab, FragmentTransaction nullFt) {
                //Reselected don't do anything          
            }
    
            @Override
            public void onTabSelected(Tab tab, FragmentTransaction nullFt) {
                FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
    
                if(fragment4 == null) {
                    fragment4 = new Fragment4();
                    ft.add(R.id.fragment_rest, fragment4, fragment4Tag);
                } else {
                    ft.attach(fragment4);
                }
    
                ft.commit();
            }
    
            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction nullFt) {
                FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
    
                if(fragment4 != null)
                    ft.detach(fragment4);
    
                ft.commit();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a

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.