Android App Development |Tab Layout | Lecture#43 | Hive Learners

Words
359
Reading
2 min
Listen
Play
4y

饾摉饾摶饾摦饾摦饾摻饾摬饾摲饾摪饾摷

Hello beautiful members of Hive learners, In lecture 42 we learn how to save data in firebase and how to update the existing data. We also implement the onComplete listeners for it. Today we will learn a very useful tool in the app which is Tab Layout. It is simple a layout that can contain more than one screen. The fun part is that each screen can have a separate XML and java file. These are called fragments. The fragment is a lightweight activity. We can use it to show data or pass data to other activities or fragments.

alt

GitHub Link

Use this GitHub project to clone into your directory. The following lecture will constantly update it so you will never miss the latest code. Happy Coding!

What Should I Learn

  • What is Fragment
  • How to implement Tab layout

Assignment

  • Create a Tab Layout

Procedure

For the Tab Layout, we will use the Welcome Activity. We will show the Tab Layout at the bottom of the buttons. We need t to add the Tab Layout in the XML file and a ViewPager that will help us to show activities on Tab Change.

alt

Tab Layout added in the XML now we need to declare and initialize the Viewpager and Tab Layout in the JAVA file.

alt

Now we need to create two fragments. Blogs_Fragment and Transfers_Fragment. Here is how you can create fragments.

alt

alt
With the same method, we create the Transfers_Fragment

Now we need to create an adapter class to set it in the ViewPager. It will help us to change the Fragment on Tab Layout scroll/change. I add the Adapter in the same Welcome_Activity class. Here is the code.

public class SectionsPagerAdapter_welcome extends FragmentPagerAdapter {
        @SuppressWarnings("deprecation")
        SectionsPagerAdapter_welcome(FragmentManager supportFragmentManager) {
            super(supportFragmentManager);
        }

        @Override
        public long getItemId(int position) {
            return super.getItemId(position);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {

            Fragment fragment = new Fragment();
            try {
                switch (position) {
                    case 0:
                        fragment = new Blogs_Fragment();
                        break;
                    case 1:
                        fragment = new Transfers_Fragment();
                        break;

                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return fragment;


        }

        @Override
        public int getCount() {
            // Show 8 total pages.
            return welcome_tabs.getTabCount();
        }
    }

alt

declare and initialize this class in our activity and set the ViewPager adapter to this.

alt

Now we need to add addOnPageChangeListener to viewpager and set it to the Tab Layout. Then we need to add a addOnTabSelectedListener of Tab Layout and set viewpager in it. It will create a connection between ViewPager and Tab Layout.

alt

We have successfully created the Tab Layout. In the next lecture, we will learn how to add content in the Fragments.

hl_divider.png

Thank You

hl_footer_banner.png

Android App Development |Tab Layout | Lecture#43 | Hive Learners | Ecency