Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the toolbar title using design Library Tab Layout?

I am using Design Library support TabLayout, I am trying to change the title of the toolbar when Tab is Pressed or ViewPager is slided.But not able to get any success so far. This is the following code:-

public class ActivityHomePage extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    Toolbar toolbar;
    DesignDemoPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homepage);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);




        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);
      //  viewPager.getAdapter().getPageTitle();
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();


    }
    private int[] tabIcons = {

            R.drawable.ic_action_star,
            R.drawable.ic_action_heart,
            R.drawable.ic_action_square
    };
    private void setupViewPager(ViewPager viewPager) {
        adapter = new DesignDemoPagerAdapter(getSupportFragmentManager());

        adapter.addFrag(new FragmentStar(), "star");
        adapter.addFrag(new FragmentHeart(), "heart");
        adapter.addFrag(new FragmentSquare(), "square");
        viewPager.setAdapter(adapter);



    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private void setupTabIcons() {
        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        switch (id) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                return true;
            case R.id.action_settings:
                return true;
        }

        return super.onOptionsItemSelected(item);
    }




     class DesignDemoPagerAdapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public DesignDemoPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);

        }

        @Override
        public CharSequence getPageTitle(int position) {


            return null;
        }
    }

}
like image 340
Rajat kumar Avatar asked Mar 15 '23 10:03

Rajat kumar


1 Answers

Finally I am able to make it, Might be helpful for someone , so here is the answer for the above:-

  /** Tab & PageViewer Controller**/
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_action_star));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_action_heart));
        tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_action_square));

        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
       // setupTabIcons();

      viewPager = (ViewPager) findViewById(R.id.viewpager);
        pAdapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(pAdapter);
        toolbar.setTitle("Same Page");
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch(tab.getPosition()) {
                    case 0:
                        viewPager.setCurrentItem(0);
                        toolbar.setTitle("Fragment Star");
                        break;
                    case 1:
                        viewPager.setCurrentItem(1);
                        toolbar.setTitle("Fragment Heart");
                        break;
                    case 2:
                        viewPager.setCurrentItem(2);
                        toolbar.setTitle("Fragment Squae");
                        break;

                    default:

                        viewPager.setCurrentItem(tab.getPosition());
                        toolbar.setTitle("Fragment Star");
                        break;
                }



            }

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

            }

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

            }
        });


    }
/** **/
 public class PagerAdapter extends FragmentStatePagerAdapter {
        int mNumOfTabs;

        public PagerAdapter(FragmentManager fm, int NumOfTabs) {
            super(fm);
            this.mNumOfTabs = NumOfTabs;
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    FragmentStar tab0 = new FragmentStar();
                    return tab0;
                case 1:
                    FragmentHeart tab1 = new FragmentHeart();
                    return tab1;
                case 2:
                    FragmentSquare tab2 = new FragmentSquare();
                    return tab2;

                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return mNumOfTabs;
        }
    }
like image 77
Rajat kumar Avatar answered Apr 24 '23 21:04

Rajat kumar