set android.support.v7.widget.Toolbar images - android

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("");

Related

How to implement viewpager2 with tab layout [duplicate]

This question already has answers here:
How to use TabLayout with ViewPager2 in Android
(12 answers)
Closed 2 years ago.
I know view pager and tab layout, but i want to display a tab on my screen, and each tab make up of a fragment. How can i do? I have try few approach but none of them work for me.
You actually want to use TabLayout and ViewPager2.
private TabLayout tabLayout;
private AppBarLayout appBarLayout;
private ViewPager viewPager;
tabLayout=(TabLayout)findViewById(R.id.tablayout);
viewPager=(ViewPager)findViewById(R.id.viewpager);
//create viewpageradaper class object
ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager());
//adding fragments using adapter object
adapter.AddFragment(new FirstFragment(), "Home");
adapter.AddFragment(new SecondFragment(), "Friends");
adapter.AddFragment(new ThirdFragment(), "Group");
//set adapter into viewpager
viewPager.setAdapter(adapter);
//set viewpager into tablayout
tabLayout.setupWithViewPager(viewPager);
Here's the git repo
Sometimes you might get an error with ViewPager2 for auto import import androidx.viewpager.widget.ViewPager;
You have changed it to ViewPager2 also.

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

add badge on icon of a tab in a tablayout using badgeView

I have looked all round stackoverflow how i can add a badge in an icon of a tab in a tab layout, yet have no answer.
This is my code
//Get reference to your Tablayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setIcon(ICONS[0]);
tabLayout.getTabAt(1).setIcon(ICONS[1]);
tabLayout.getTabAt(2).setIcon(ICONS[2]);
BadgeView badge = new BadgeView(this, tabLayout.getTabAt(0).getCustomView());
badge.setText("1"); //Whatever value you should add
badge.show();
BadgeView mMotification = new BadgeView(this, tabLayout.getChildAt(1));
mMotification.setText("10");
mMotification.show();
I have also tried many other alternatives but it seem like BadgeView takes only views
You can create custom layout for the tabs and add them in TabLayout
This is what i did for it to work
TabLayout.Tab tab = tabLayout.getTabAt(0);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_notifications);
tab.setCustomView(imageView);
BadgeView badge = new BadgeView(this, imageView);
badge.setText("23");
badge.setBadgeMargin(25,0);
badge.show();

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

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);

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.

Categories

Resources