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

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

Related

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

ViewPager blocks Fragment's View's OnClickListeners

I currently have a view pager in a tab layout
//Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SettingsFragmentPagerAdapter(getSupportFragmentManager()));
//Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
And then the fragments in the view pager have TextViews with OnClickListeners attached
final TextView zSemTxt = (TextView) getActivity().findViewById(R.id.zSemTxt);
zSemTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mSelectedTextView = zSemTxt;
showDialogToSelectClass();
}
});
My issue is the OnClickListeners are never called, it appears that all touches go to the ViewPager and never goes through to the TextViews. I've tried things like viewPager.requestDisallowInterceptTouchEvent(true); but none of it change anything.
Ideas?
Show more code.
Probably onClick dosen't work because your textview = null.
final TextView zSemTxt = (TextView) getActivity().findViewById(R.id.zSemTxt);
Are You sure about this?
I dont see your code but try
final TextView zSemTxt = (TextView) view.findViewById(R.id.zSemTxt);
where view is your fragment inflated view.
Turns out neither the onActivityCreated or onCreateView were being called on the fragment.
The solution was to go to the PageFragment class I was using to manage which fragment was displayed by the ViewPager and add the onClickListeners to there. The fragments were being inflated there which is why the other classes were never called.

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

Add badge for tab widget android

I want to add badge for tab widget at android. i use android-viewbadger lib, but i not set badge show to image
here code:
tabs = (TabWidget) findViewById(android.R.id.tabs);
badge = new BadgeView(this, tabs, 2);
// badge.setBackgroundResource(R.drawable.badge_28);
badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
badge.setWidth(14);
badge.setHeight(14);
badge.show();

Categories

Resources