Create a custom tab layout in android studio.

Total Post:24

Points:168
 399  View(s)
Ratings:
Rate this:

Create a Custom tab layout in android.

  1. Post:24

    Points:168
    Re: Create a custom tab layout in android studio.

    First, you need to create a layout file 

    Activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.view.ViewPager
            android:layout_above="@id/tab_layout"
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />


        <android.support.design.widget.TabLayout
            app:tabTextAppearance="@style/MineCustomTabText"
            android:id="@+id/tab_layout"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    </RelativeLayout>


    MainActivity.java

    package com.example.msclient010.mindstickqna.Fragment;
    import android.graphics.Color;
    import android.os.Build;
    import android.os.Bundle;
    import android.support.annotation.RequiresApi;
    import android.support.design.widget.TabLayout;
    import android.support.v4.app.Fragment;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    import com.example.msclient010.mindstickqna.Adapter.QuestionPagerAdapter;
    import com.example.msclient010.mindstickqna.R;
    import com.example.msclient010.mindstickqna.Utils.SaveSharedPreference;
    import static com.facebook.FacebookSdk.getApplicationContext;
    public class RecentActivityFragment extends Fragment {
        View mView;
        ViewPager mViewPager;
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        @Override
        public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
            if (savedInstanceState == null) {
                mView = inflater.inflate(R.layout.home_page_fragment, container, false);
                setupTabLayoutWithViewPager(mView);
            }
            return mView;
        }
        private void setupTabLayoutWithViewPager(View view) {
            mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
            QuestionPagerAdapter adapter = new QuestionPagerAdapter(getFragmentManager(), getContext());
            adapter.addFragment(new HomeFragment(), getString(R.string.home));
            adapter.addFragment(new UnansweredFragment(), getString(R.string.unanswered));
                  //********* My code **************
            if (SaveSharedPreference.getPrefUserId(getContext()) != null) {
                adapter.addFragment(new NotificationFragment(), getString(R.string.notification));
                adapter.addFragment(new UserProfileFragment(), getString(R.string.profile));
            } else {
                adapter.addFragment(new LoginRegisterFragment(), getString(R.string.login));
            }
           /// ********************
            mViewPager.setAdapter(adapter);
            TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
            tabLayout.setupWithViewPager(mViewPager);
            tabLayout.getTabAt(0).setIcon(R.drawable.ic_home_white_18dp);
            tabLayout.getTabAt(1).setIcon(R.drawable.ic_question_answer_white_18dp);
       if (SaveSharedPreference.getPrefUserId(getContext()) != null) {
                tabLayout.getTabAt(2).setIcon(R.drawable.ic_notifications_white_18dp);
                tabLayout.getTabAt(3).setIcon(R.drawable.ic_account_circle_white_18dp);
            } else {
                tabLayout.getTabAt(2).setIcon(R.drawable.login_white_icon);
            }
        }
    }


    QuestionPagerAdapter.java

    package com.example.msclient010.mindstickqna.Adapter;
    import android.content.Context;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentStatePagerAdapter;
    import java.util.ArrayList;
    import java.util.List;
    public class QuestionPagerAdapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();
        public QuestionPagerAdapter(FragmentManager fragmentManager, Context context){
            super(fragmentManager);
        }
        @Override
        public Fragment getItem(int position){
            return mFragmentList.get(position);
        }
        @Override
        public int getCount(){
            return mFragmentList.size();
        }
        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
               }
    }


    Output :

    Create a custom tab layout in android  studio.


    "Thanks!!! for Reading"

Answer