I need to show the number of notification inside a tab. How do i do that in android. My tab code looks something like this below.
mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("HOME").setIndicator("HOME"),
homepage.class, null);
mTabHost.addTab(mTabHost.newTabSpec("NOTIFICATIONS").setIndicator("NOTIFICATIONS"),
notificationfragment.class, null);
return rootView;
You can customize the tabhosts Spec by setting the view on it as-
spec = tabHost.newTabSpec("groups");
View view = LayoutInflater.from(this).inflate(R.layout.tabwidget_tabs, tabHost.getTabWidget(), false);
spec.setIndicator(view);
Related
I am using a tabhost to hold and navigate multiple fragments. I am not sure what I am doing wrong but the tab is only showing the text but not the icon (I really only need the icon and no text). Fragment 1 and 2 tabs are setup to only show text but on the third fragment tab I try to add in the icon with getDrawable. There is no error everything compiles fine but it just doesn't show the icon.
Thanks for your help.
This is the start of my oncreateView method where I set the tabs up:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my_discovered_songs, container, false);
mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("fragment1").setIndicator("Fragment 1"),
OneFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragment2").setIndicator("Fragment 2"),
TwoFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragment3").setIndicator("Fragment 3", getResources().getDrawable(R.drawable.com_facebook_button_icon)),
ThreeFragment.class, null);
I'm using TabLayout from Android Design Support Library in my app.
I setup tabs using viewPager in activity's onCreate and they work well.
viewPager.setAdapter(
new TabsAdapter(getSupportFragmentManager(),
new TabInfo("Test1", Fragment1.newInstance()),
new TabInfo("Test2", Fragment2.newInstance()),
new TabInfo("Test3", Fragment3.newInstance())
));
tabs.setupWithViewPager(viewPager);
But sometimes, randomly when screen goes to sleep and I unlock the phone (tested on Moto G 2014, Android Lollipop) tabs just disappear.
It's bad, because I can't reproduce it on purpose.
try using tabhost. It's a lot more stable. The code shown below is an example of a tabhost in a fragment with three child fragments. I use this kind of code myself and works perfectly
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.container);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("tab1"),
Tab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("tab2"),
Tab2Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("tab3"),
Tab3Fragment.class, null);
return mTabHost;
}
For example, I have 5 tab inside a fragment tabhost.
For my past experience, if I have to implement a "go to detail" page inside a tab, I would just simply use activity to do (e.g. intent and create new activity).
Since I would like to keep the tabhost bar at the page so I can not use activity anymore , I need to change the fragment which is inside the tabhost. I tried using fragment transaction but when I switch tab the text is overlap, so how to change the fragment inside the tab? Thanks a lot.
This is how I create tabhost
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
tabHost.addTab(
tabHost.newTabSpec("home").setIndicator("",
getResources().getDrawable(R.drawable.btn_home)),
HomeFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("exhibit").setIndicator("",
getResources().getDrawable(R.drawable.btn_exhibit)),
ExhibitFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("plan").setIndicator("",
getResources().getDrawable(R.drawable.btn_floor_plan)),
PlanFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("collection").setIndicator("",
getResources().getDrawable(R.drawable.btn_collection)),
CollectionFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("setting").setIndicator("",
getResources().getDrawable(R.drawable.btn_setting)),
SettingFragment.class, null);
This is how I go to detail before, I would like to use fragment now
private OnClickListener set_listener(final int pos) {
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ctx, CollectionDetail.class);
intent.putExtra("pos", pos);
startActivity(intent);
}
};
return listener;
}
I have this piece of code for instantiating my Android app:
onCreate:
tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//Adding all the tabs!
tabHost.addTab(tabHost.newTabSpec("Servers").setIndicator("Servers"),ServerTab.class, null);
tabHost.addTab(tabHost.newTabSpec("Shoutbox").setIndicator("Shoutbox"), ShoutBoxTab.class, null);
tabHost.addTab(tabHost.newTabSpec("Leader\nboards").setIndicator("Leader\nboards"), LeaderBoardsTab.class, null);
tabHost.addTab(tabHost.newTabSpec("About").setIndicator("About"), AboutTab.class, null);
tabHost.setCurrentTab(0);
When this is called, the onCreateView of the second tab (ShoutBox) is called four times. Resulting in a lot more loading time then needed. During the loading I didn't touch my phone or changed its state(Rotation etc). Why is this happening? Thanks
How to use tabs in android after depreciation of tab activity? by using sherlock and fragments i am able to do tabing but i want to set tab image as whole not tab icon...
i don't need black bg with my image i want my image on whole tab.
here is my code
private FragmentTabHost mTabHost;
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
// mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("",
getResources().getDrawable(R.drawable.tab8)),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab 2",
getResources().getDrawable(R.drawable.tab9)),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator("Tab 3",
getResources().getDrawable(android.R.drawable.star_on)),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab4").setIndicator("Tab 4",
getResources().getDrawable(android.R.drawable.star_on)),
FragmentTab.class, null);
Help Required :)
I did it by using tabhost, you can use tabhost without tabactivity following is the code.
final TabHost tabHost=(TabHost)findViewById(R.id.tabhost);
tabHost.setup();
final TabSpec spec1 = tabHost.newTabSpec("Tab1");
View view = LayoutInflater.from(this).inflate(R.layout.tabbar8, tabHost.getTabWidget(), false);
spec1.setIndicator(view);
spec1.setContent(R.id.tab1);
where tabbar8 is layout which i want to set on my first tab
You can completely customize the ActionBar tabs by using setCustomView() on the ActionBar.Tab. You'll define an XML layout for the tab, then set similar to:
// Home tab
ActionBar.Tab tabHome = mActionBar.newTab();
tabHome.setText("Home");
LinearLayout layoutLinear = (LinearLayout)inflater.inflate(R.layout.layout_tab_standard, null);
TextView title = (TextView)layoutLinear.findViewById(R.id.title);
title.setText(strTabTitle);
tabHome.setCustomView(layoutLinear);
mActionBar.addTab(tabHome);
so you can put your ImageView, or set a background drawable, on the linear layout you define (R.layout.layout_tab_standard in this example).
You should also review the Action Bar Style Generator (http://jgilfelt.github.io/android-actionbarstylegenerator/), you might find this useful.