How to use public method removeTab(TabLayout.Tab tab)? - android

Checking [https://developer.android.com/reference/android/support/design/widget/TabLayout.html][1] I created my tabs
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Tab 1 Item").setIcon(android.R.drawable.ic_dialog_email).setTag("tt"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2 Item").setIcon(android.R.drawable.ic_btn_speak_now));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3 Item").setIcon(android.R.drawable.ic_lock_idle_low_battery));
tabLayout.addTab(tabLayout.newTab().setText("Tab 4 Item").setIcon(android.R.drawable.ic_dialog_alert));
but then when I try to remove the tab I am getting error. My simple question is how can I use the method removeTab(TabLayout.Tab tab) ?

for example:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
TabLayout.Tab tab1 = tabLayout.newTab().setText("Tab 1 Item").setIcon(android.R.drawable.ic_dialog_email).setTag("tt");
tabLayout.addTab(tab1);
TabLayout.Tab tab2 = tabLayout.newTab().setText("Tab 2 Item").setIcon(android.R.drawable.ic_btn_speak_now);
tabLayout.addTab(tab2);
tabLayout.removeTab(tab1); // remove first tab

Create your tab outside the function and then add and remove them as you wish.
Something like :
TabLayout t = new TabLayout();
t.setText("test");
...
add(t);
remove(t);

Related

How to call an activity instead of a fragment in Tablayout?

I have an android app in which I have 3 tabs in a tablayout. I simply want the first tab to open a new activity and not a fragment, because I'll have new tabs in this new activity. What's the best solution for that?
I have tried to desing with a linear layout a "fake tab" but the problem is it's never gonna have the same size as the other tabs.
Is there a way to do that like this?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.pager);
tabLayout = findViewById(R.id.tabLayout);
adapter = new TabAdapter(getSupportFragmentManager());
adapter.addFragment(new ProfileActivity(), "Profile");
adapter.addFragment(new SwipeFragment(), "Meet");
adapter.addFragment(new FriendsFragment(), "Friends");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
this addFragment method does not accept an Activity so that line is in error. Someone has any idea on how can I achieve this?
Anyway in which I can make a tab call an activity instead of a fragment

Viewpager TabLayout Tab change when swipe

I want to change tab when user 20% of view swipe but right now if i swipe around 60 present then tab change. other wise it back previous tab. how can i do that.
viewPager = (ViewPagerCustomDuration) findViewById(R.id.tabs_viewpager);
viewPager.setScrollDurationFactor(1);
viewPager.setOffscreenPageLimit(3);
viewPager.animate().translationX(0f).setDuration(500);
final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
viewPager.setPageMargin(pageMargin);
// viewPager.setPagingEnabled(false);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabFragmentPagerAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this, viewPager);
tabFragmentPagerAdapter.addFrag(new HomeFragment(), "Home");
tabFragmentPagerAdapter.addFrag(new DiscoverFragment(), "Discover");
tabFragmentPagerAdapter.addFrag(new TopFragment(), "Top");
tabFragmentPagerAdapter.addFrag(new CategoryFragment(), "Category");
// setupViewPager(viewPager);
viewPager.setAdapter(tabFragmentPagerAdapter);

Tab layout underline width according to tab layout text size

I have Tab layout in which having two TabItems .So when one tabitem is selected the underline is coming from starting point and the width is too big .
So i want to set that indicator width according to my tab text and to show underline only under the text in both tab items.
final TabLayout tabLayout = (TabLayout) view.findViewById(R.id.varietytaba);
tabLayout.addTab(tabLayout.newTab().setText("VEG"));
tabLayout.addTab(tabLayout.newTab().setText("NON VEG"));
tabLayout.setBackgroundColor(Color.parseColor("#000000"));
tabLayout.setTabTextColors(Color.parseColor("#FFFFFF"), Color.parseColor("#FFFFFF"));
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager_drinka);
viewPager.setAdapter(new Fragvariety.Pageradapter(getChildFragmentManager(),
tabLayout.getTabCount()));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FFFFFF"));
}

Android tablayout selecting the last tab instead of first

I need your help. I have a tablayout with tintable imageview and textview. I want to highlight the default selected tab which is the first one.
This is my sample code:
TabLayout.Tab tab;
mTabs = (TabLayout) findViewById(R.id.tabs);
mViewpager = (ViewPager) findViewById(R.id.viewpager);
MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
mViewpager.setAdapter(pagerAdapter);
mViewpager.addOnPageChangeListener(new MyPageScrollListener(mTabs));
mTabs.setupWithViewPager(mViewpager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < mTabs.getTabCount(); i++) {
tab = mTabs.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
mTabs.setOnTabSelectedListener(new MyOnTabSelectedListener());
tab.select();
But it's selecting the last tab. Anyone here knows how to fix this? I'd appreciate any kind of help. Thanks!
Finally, I found a solution here: link
I added:
mTabs.getTabAt(1).select();
mTabs.getTabAt(0).select();
And it works!
I found a better solution. Instead if selecting the tab from the TabLayout you can do this with the ViewPager like so:
ViewPager viewPager = (ViewPager) findViewById(R.id.restaurant_menu_viewPager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
// ViewPager mit TabLayout verknüpfen
TabLayout tabLayout = (TabLayout) findViewById(R.id.restaurant_menu_tabLayout);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(1);
See the post here:
https://stackoverflow.com/a/38762670/7318519
You selected the last tab.
Take a look at your source code :)
TabLayout.Tab tab;
...
for (int i = 0; i < mTabs.getTabCount(); i++) {
tab = mTabs.getTabAt(i);
}
...
tab.select();
tab is the last tab you will get with getTabAt(int position).
So when you want to select the first tab this will help
maTabs.getTabAt(0).select();
Another (maybe better) solution is to turn the for loop into this
for (int i = mTabs.getTabCount() - 1; i >= 0; i--) {
tab = mTabs.getTabAt(i);
}
Then your "last" tab you are getting in this loop is the first tab in your TabLayout.
An working full example code looks like this gist.

set android.support.v7.widget.Toolbar images

I am using android.support.v7.widget.Toolbar with ViewPager like this :
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabanim_tabs);
tabLayout.setupWithViewPager(viewPager);
the tab is displaying with text of the ViewPager when I add the dummy Fragment to it
adapter.addFrag(new Fragment(getResources().getColor(R.color.material_light)), "XYZ");
i want the image to display instead of a text.
i did it by using this
tabLayout.getTabAt(0).setIcon(R.drawable.brands);
tabLayout.getTabAt(0).setText("");
tabLayout.getTabAt(1).setIcon(R.drawable.food);
tabLayout.getTabAt(1).setText("");
tabLayout.getTabAt(2).setIcon(R.drawable.cinema);
tabLayout.getTabAt(2).setText("");

Categories

Resources