1

I am using a Tablayout to switch between Fragments on an Activity; however, the Fragment that I originally assign to the Activity seems to not be replaced when tabs are clicked - a new fragment just seems to be added. This new fragment has the implementation I want the TabLayout to have. That is, when someone clicks a tab, the Fragment is replaced with the fragment associated with that Tab. I genuinely can not see what is causing this as I feel like I am replacing the entire fragment by referencing its ID in the XML file, and I don't think I assign the fragments anywhere else. The key snippets of code are below. I have attached pictures to help you see what I am talking about. Thank You!

Main code pertaining to TabLayout:

 @Override
public void onCreate(Bundle savedInstanceState) throws IllegalArgumentException {
    super.onCreate(savedInstanceState);
    popup = new LoadingDialog(this);
    setContentView(R.layout.main_activity_layout);
    SharedPreferences sharedPref = getSharedPreferences("mysettings", Context.MODE_PRIVATE);
    Boolean islogged = sharedPref.getBoolean("loggedin", false);
    tab_layout = (TabLayout) findViewById(R.id.sliding_tabs);
    tab_layout.setTabMode(TabLayout.MODE_SCROLLABLE);
    tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int position = tab.getPosition();
            switch (position){
                case 0:
                    FragmentTransaction FTg = FM.beginTransaction();
                    Fragment gen = new FragmentGeneral();
                    FTg.replace(R.id.ma_layout, gen);
                    FTg.commit();
                    break;
                case 1:
                    FragmentTransaction FTs = FM.beginTransaction();
                    Fragment sum = new FragmentOne();
                    FTs.replace(R.id.ma_layout, sum);
                    FTs.commit();
                    break;
                case 2:
                    FragmentTransaction FTf = FM.beginTransaction();
                    Fragment forecast = new FragmentTwo();
                    FTf.replace(R.id.ma_layout, forecast);
                    FTf.commit();
                    break;
                case 3:
                    FragmentTransaction FTr = FM.beginTransaction();
                    Fragment rates = new FragmentThree();
                    FTr.replace(R.id.ma_layout, rates);
                    FTr.commit();
                    break;
            }
        }

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_activity"
>


<fragment
    android:name="com.example.schadtj.portalapp.FragmentGeneral"
    android:layout_gravity="center_horizontal"
    android:id="@+id/ma_layout"
    android:layout_width="match_parent"
    android:layout_height="558dp"
    >

</fragment>

<android.support.design.widget.TabLayout
    android:id="@+id/sliding_tabs"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <android.support.design.widget.TabItem
        android:text="User Info"
        android:icon="@drawable/information"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <android.support.design.widget.TabItem
        android:text="Summary"
        android:icon="@drawable/calculator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <android.support.design.widget.TabItem
        android:text="Forecast"
        android:icon="@drawable/calendar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <android.support.design.widget.TabItem
        android:text="Rates"
        android:icon="@drawable/money_bag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</android.support.design.widget.TabLayout>

enter image description here

enter image description here

2
  • What exactly you want to achieve? Aug 16, 2016 at 13:58
  • When a tab is clicked, I want to have the fragment showing replaced with the one associated with the tab that is clicked.
    – user6542934
    Aug 16, 2016 at 14:01

1 Answer 1

1

Change Like this in java:

    @Override
    public void onCreate(Bundle savedInstanceState) throws IllegalArgumentException {
        super.onCreate(savedInstanceState);
        popup = new LoadingDialog(this);
        setContentView(R.layout.main_activity_layout);
        SharedPreferences sharedPref = getSharedPreferences("mysettings", Context.MODE_PRIVATE);
        Boolean islogged = sharedPref.getBoolean("loggedin", false);
        tab_layout = (TabLayout) findViewById(R.id.sliding_tabs);
        tab_layout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                switch (position){
                    case 0:
                        FragmentTransaction FTg = FM.beginTransaction();
                        Fragment gen = new FragmentGeneral();
                        FTg.replace(R.id.ma_layout, gen);
                        FTg.commit();
                        break;
                    case 1:
                        FragmentTransaction FTs = FM.beginTransaction();
                        Fragment sum = new FragmentOne();
                        FTs.replace(R.id.ma_layout, sum);
                        FTs.commit();
                        break;
                    case 2:
                        FragmentTransaction FTf = FM.beginTransaction();
                        Fragment forecast = new FragmentTwo();
                        FTf.replace(R.id.ma_layout, forecast);
                        FTf.commit();
                        break;
                    case 3:
                        FragmentTransaction FTr = FM.beginTransaction();
                        Fragment rates = new FragmentThree();
                        FTr.replace(R.id.ma_layout, rates);
                        FTr.commit();
                        break;
                }
            }
        }

                   FragmentTransaction FTg1 = FM.beginTransaction();
                                Fragment gen = new FragmentGeneral();
                                FTg1.replace(R.id.ma_layout, gen);
                                FTg1.commit();
    }

Change fragmentto FrameLayout

<FrameLayout
    android:layout_gravity="center_horizontal"
    android:id="@+id/ma_layout"
    android:layout_width="match_parent"
    android:layout_height="558dp"/>
2
  • Thank You! It works! I changed Fragment to FrameLayout in XML and it worked, however, why did it work? Also, I did not change the Java code itself, because I did not know why you suggested the changes. What is your thinking for the Java changes? Once again, thank you for the help!
    – user6542934
    Aug 16, 2016 at 14:16
  • As standard we use FrameLayout to replace fragment and fragment tag is mostly use for static fragments.. Aug 16, 2016 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.