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();
Related
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();
I'm trying to apply a custom style to the actionbar tabs, but for now without any success. I know there are many tutorials out there, but can someone explain how it should be done or provide a link to a good tutorial or documentation on how can I implement custom styling.
Here is the code I have:
ActionBar actionBar = getActionBar();
Tab1 = actionBar.newTab().setText("TAB1");
Tab2 = actionBar.newTab().setText("Tab2");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
What Im trying to achieve:
Here is a snippet of code I wrote for one of my projects, you should be able to easily adjust it for your own needs!
//#param string - the R.string.name_of_string, I use it for convenience
private ActionBar.Tab createCustomTab(ActionBar actionBar, int string){
View tabView = getLayoutInflater().inflate(R.layout.tab, null);
((TextView) tabView.findViewById(R.id.txt_tab)).setText(string);
//in my case I wanted to center the layout - for some reason certain layout params do not work in the XML,
// (such as centering), so I do that here
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
tabView.setLayoutParams(lp);
return actionBar.newTab().setCustomView(tabView);
}
Let me know if you have any questions!
I have a app that uses ActionBar and Fragment to show several different views in the app. So far I am only showing either a list of items, a photo, a web view... but I want to go further and show in the Fragment a Master/Detail Flow, so that I can have a ListView and DetailView. This is so far a sample code
// ActionBar
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// create new tabs and and set up the titles of the tabs
ActionBar.Tab mFindTab = actionbar.newTab().setText(
getString(R.string.ui_tabname_find));
ActionBar.Tab mChatTab = actionbar.newTab().setText(
getString(R.string.ui_tabname_chat));
ActionBar.Tab mMeetTab = actionbar.newTab().setText(
getString(R.string.ui_tabname_meet));
ActionBar.Tab mPartyTab = actionbar.newTab().setText(
getString(R.string.ui_tabname_party));
// create the fragments
Fragment mFindFragment = new FindFragment();
Fragment mChatFragment = new ChatFragment();
Fragment mMeetFragment = new MeetFragment();
Fragment mPartyFragment = new PartyFragment();
// bind the fragments to the tabs - set up tabListeners for each tab
mFindTab.setTabListener(new MyTabsListener(mFindFragment,
getApplicationContext()));
mChatTab.setTabListener(new MyTabsListener(mChatFragment,
getApplicationContext()));
mMeetTab.setTabListener(new MyTabsListener(mMeetFragment,
getApplicationContext()));
mPartyTab.setTabListener(new MyTabsListener(mPartyFragment,
getApplicationContext()));
// add the tabs to the action bar
actionbar.addTab(mFindTab);
actionbar.addTab(mChatTab);
actionbar.addTab(mMeetTab);
actionbar.addTab(mPartyTab);
How can I create the Master/Detail Flow so that when touch on the corresponding action bar.tab it is shown?
Did you know that you can create stubs for this in the 'New' Menu?
The menu selections: File>New>Android Activity>Master / Detail Flow
I suggest you create an example using this Wizard, and then use it to understand how everything works (this will give you code that uses the proper patterns, and has implemented all the correct code standards).
Tab footballTab = actionBar.newTab();
footballTab.setText("Football");
footballTab.setIcon(android.R.drawable.btn_radio); //whatever
footballTab.setTabListener(this);
Tab basketBallTab = actionBar.newTab();
basketBallTab.setText("Basketball");
basketBallTab.setIcon(android.R.drawable.btn_star_big_off); //whatever
basketBallTab.setTabListener(this);
Tab tennisTab = actionBar.newTab();
tennisTab.setText("Tennis");
tennisTab.setIcon(android.R.drawable.btn_minus); //whatever
tennisTab.setTabListener(this);
actionBar.addTab(footballTab, false);
actionBar.addTab(basketBallTab, true);
actionBar.addTab(tennisTab, false);
The result shows the icon and the text next to each other in a horizontal manner.
Is there a way I can show the icon and set the text below it ?
Moreover, is it possible to pass in custom views for the tab icon ?
I have implemented ViewPageIndicator and it is running fine. I have 3 tabs with names "Tab1", "Tab2", and "Tab3". What I want is the tab name when I scroll the tab.
Could anyone let me know how to fetch the tab title of the current tab that is being displayed?
You can get the title for the current page from your PagerAdapter:
int currentPage = viewPager.getCurrentItem();
CharSequence pageTitle = pagerAdapter.getPageTitle(currentPage);