Android: Fragment tab host showing text for tab but not icon - android

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

Related

TabLayout disappears when screen goes to sleep

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

How to add badge counter inside tabhost android

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

Fragment within FragmentTabHost view lost onBackPressed Android?

I have created a application. Following is scenario before I explain problem.
I have Activity A which has multiple fragments such as F1,F2,F3 etc.
Now for F1 fragment I have implemented FragmentTabHost with three Fragments F11,F12,F13 fragment views. On tab is working fine for this.
But today i noticed one problem.
Say I am inside F1 I show three fragment tabs F11,F12,F13. User can switch between tabs and it works fine.
Problem is say i goto Fragment F13 from F11 by pressing tab. It shows F13 fragment successfully.
However when I click Back Button on menu it goes back F11 fragment but empty screen is shown means F11 view is not shown..
This is my F1 fragment code implementing FragmentTabHost:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_fragment, container,
false);
setHasOptionsMenu(true);
// realtabcontent = (FrameLayout) rootView
// .findViewById(R.id.realtabcontent);
mTabHost = (FragmentTabHost) rootView
.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(),
R.layout.home_fragment);
mTabHost.setOnTabChangedListener(this);
View tabView = createTabView(getActivity(), "Featured");
spec = mTabHost.newTabSpec("featured").setIndicator(tabView);
mTabHost.addTab(spec, FeaturedHomeTab.class, null);
tabView = createTabView(getActivity(), "Top");
spec = mTabHost.newTabSpec("top").setIndicator(tabView);
mTabHost.addTab(spec, TopHomeTab.class, null);
tabView = createTabView(getActivity(), "New");
spec = mTabHost.newTabSpec("new").setIndicator(tabView);
mTabHost.addTab(spec, NewHomeTab.class, null);
onTabChanged("featured");
return rootView;
}
So this is main code with three fragments and when i back from one fragment to previous fragment in tabhost view disappears.
What can be problem. Please help.
Tabs aren't meant to participate in temporal navigation (which is what back nav is for) because they represent content at same level of hierarchy.
In case of fragments, the back nav usually pops the back stack. There is a caveat that back nav doesn't pops sub (child) fragments first. So, the fragments added to Activity are removed on back, this includes the entire tabs fragment (along with its child fragments).

Android FindFragmentByTag returns null in FragmentTabHost

I am trying to retrieve a fragment from my fragmentTabHost:
private void initView() {
FragmentTabHost mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("summary").setIndicator("Summary"),
SummaryActivity.class, bundle);
SummaryActivity summaryActivity = (SummaryActivity) getSupportFragmentManager().findFragmentByTag("summary");
System.out.println(summaryActivity);
}
When I try to get my fragment immediately after it was created, it returns null. It could be a thread issue. How, or even when, should I call getFragmentByTag to retrieve my fragment in this case?
The Fragment you are looking for is not in the stack. It is not even added/present at the time you are finding it back.
findFragmentByTag method will look and match last fragment that added in the transaction, if that is not matching with the required one, then it will search with the available list from history. Finally if your tag is not there with any fragments it will return null.
Make sure that , you are adding the Fragment to the view first.
the view should receive the fragment/tabhost to make it available in the lifecycle.
you have to return your tabHost to the view to add the fragment before you do findFragmentByTag lookup.
For ex:
You are in your Fragment's onCreateView then you should do return the tabhost to the view after you inflated it.
onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1);
mTabHost.addTab(mTabHost.newTabSpec("fragmenttag").setIndicator("Fragmenttag"),
FragmentStackSupport.CountingFragment.class, null);
return mTabHost;
}
Then probably your Fragment will be available in back stack , you can get it back by Tag.
Fragment Tags can only be set during fragment transactions. The tab host tag refers to something else. Try using findFragmentById() if you set an id on the fragments root view.

In Tab bar to go an other fragment in the same tab?

I am using FragmentAcitvity in my Tab Bar .
I am able to load a new fragment on tab changed.
But i have a problem Because i have many activties in one tab.
for example i have 2 tabs :
Tab1 , Tab2. and i have in
Tab1 : activityA-->activityB---ActivityC
and
Tab2: activityF
how i can achieve this.
my code is here.
Main Class
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
Context context=this;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(context, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("Records").setIndicator("")), ActiovityA.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("tab2"),acitvityf.class, null);
mTabHost.setCurrentTab(0);
}
}
my acitvityA is here
public class acitvityA extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.acitvitya_xml, container, false);
return v;
}
}
myactivityB is
public class acitvityb extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.acitvityb_xml, container, false);
return v;
}
}
how i can solve this ?what is missing here.
You might have to do a little bit of homework yourself, but i could give you a hint that could help you.
You have the tabs set well. What you could do is, When the transition from Activity1 to 2 takes place, hide the last fragment from the stack, and then show the Activity 2 fragment in tab one (here you will push the fragment into the stack and show it). Maintain a stack of the fragments you are using, and compare an instance of which fragment has been pushed or popped from the stack to bring back the correct fragment.
Its a combination of fragment transactions add, hide, and stack push and pop.
Stack is needed for back press events.
Hope that helps.

Categories

Resources