batuman
batuman

Reputation: 7304

Implementing Tabs in Android

I like to check I am doing the right thing or is there a better way than this. I like to implement tabs in Android application. I found out that I should use Fragment.

Then I implemented my application with TabbedActivity.Inside I added in Tabs. I have added Tab in the onCreate() of the MainActivity as

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("Accelerometer"));
        tabLayout.addTab(tabLayout.newTab().setText("Gyroscope"));
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

Since I want to use two xmls for two different fragments (frangment1.xml and frangment2.xml), I created two blank fragments and inside PlaceholderFragment class's, I added frangment1.xml and frangment2.xml using cnt variable

 public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";
        private static int cnt = 0;
        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = null;
            if(cnt== 0) {
                rootView = inflater.inflate(R.layout.tab_fragment1, container, false);
                TextView textView = (TextView) rootView.findViewById(R.id.frag1);
                cnt++;
            }else if(cnt== 1){
                rootView = inflater.inflate(R.layout.tab_fragment2, container, false);
                TextView textView = (TextView) rootView.findViewById(R.id.frag2);
            }
            //textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

I like to check is that the correct way or any other better way for that.

Upvotes: 0

Views: 65

Answers (2)

Ashwani Kumar
Ashwani Kumar

Reputation: 1472

Hey there is not much to say about this code. I have not tried to run your code. But if it run for you its fine. Even I use to do this type of Tabs implementation in android. But since there are many changes from then as per the new android design library. There are tons of new features, not just in designs but also in functionalities. You might wanna check this link for some of them being implemented here.

http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

Also for guidelines from google on tabs, their implementations, specifications and all that stuff check this out.

https://www.google.com/design/spec/components/tabs.html#tabs-usage

Upvotes: 1

Ahmed Hegazy
Ahmed Hegazy

Reputation: 12605

No this is not right.

You shouldn't make the two fragments inside one fragment. You should separate them to step out of the if condition in every method (checking which view is inflated). Suppose now you have two separate fragments, you just need to check the position in your adapter to choose which fragment you need in which position.

@Override
public Fragment getItem(int position) {
    switch(position) {
        case 0:
            return new MyFragment1();
            break;
        case 1:
            return new MyFragment2();
            break;
    }
    return null;
}

Upvotes: 0

Related Questions