I've already implemented FragmentTabHost, but my Fragments are going outside the tabhost.
Here is my Activity:
public class MainActivity extends ActionBarActivity {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(),android.R.id.tabhost);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
PlaceholderFragment.class,null);
mTabHost.addTab(mTabHost.newTabSpec("contacts")
.setIndicator("Contacts"), PlaceholderFragment2.class, null);
}
}
And here is my fragment:
public class PlaceholderFragment2 extends Fragment {
public PlaceholderFragment2() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main2, container, false);
return rootView;
}
}
activity_main.xml
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
/>
</android.support.v4.app.FragmentTabHost>
And fragment_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="159dp" />
</FrameLayout>
The problem is that the fragment goes outside it's tab.
Here's the photo
Change your activity_main.xml to
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
and your activity's onCreate to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
PlaceholderFragment.class,null);
mTabHost.addTab(mTabHost.newTabSpec("contacts")
.setIndicator("Contacts"), PlaceholderFragment2.class, null);
}
Although I strongly suggestion not using a tabhost at all. I would instead go for something in the lines of this solution.
Related
I noticed that onCreateView() of one of my fragments is never called and i dont know why.I need to be able to make changes in onResume() but thats is also never called.
Here is how I add the fragments.
public class LiveGameStats extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_game_stats);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.tab1, new Tab1Fragment()).commit();
}
FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.liveGameTabhost);
mTabHost.setup(LiveGameStats.this, getSupportFragmentManager(), R.id.liverealtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("You"),
LiveGameTab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Your Team"),
LiveGameTab2Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Your Enemies"),
LiveGameTab3Fragment.class, null);
}
Tab1Fragment
public class Tab1Fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.tab1, container, false);
Log.v("FRAGMENT","FragmentActivityCreateView");
return V;
}
And finnaly the xml passed under R.layout.tab1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DeviceFragment" >
<ImageButton
android:id="#+id/searchBtn"
android:src="#drawable/waves"
android:background="#drawable/search_selector"
android:padding="40dp"
android:layout_height="200dp"
android:layout_width="200dp"
android:onClick="liveMatch"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I think I might have created the fragments the wrong way. As you see it now they are created as the xml says but when I changed tabs, the content is "reseted".Any changed I placed in onCreateView() or onResume() is not called.
EDIT
I have tried to implement the method described in the 1st comment. After the implementation the tabs have the same content. Is like all the tabs have the same content. This time I posted all my classes and their associated XML.
public class LiveGameStats extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_game_stats);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab1Fragment()).commit();
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab2Fragment()).commit();
getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab3Fragment()).commit();
}
FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.liveGameTabhost);
mTabHost.setup(LiveGameStats.this, getSupportFragmentManager(), R.id.liverealtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("You"),
LiveGameTab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Your Team"),
LiveGameTab2Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Your Enemies"),
LiveGameTab3Fragment.class, null);
}
activity_live_game_stats.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_live_game_stats"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light"
tools:context="lucian.leagueassistant.activity.LiveGameStats">
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/liveGameTabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="#+id/liverealtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
I wont post the code from LiveGameTab2 and LiveGameTab3 since they are almost the same. I have also another activity that
Edit2
Seems its solved when i changed from getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab1Fragment()).commit(); to getSupportFragmentManager().beginTransaction().add(android.R.id.tabcontent, new LiveGameTab1Fragment()).commit();
The problem is this:
getSupportFragmentManager().beginTransaction().add(R.id.tab1, new Tab1Fragment()).commit();
In the first argument you have to pass a id to a container in the Activity layout where your fragment will be attached.
You are passing a entire layout R.id.tab1.
Add a container view in activity_live_game_stats xml file,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DeviceFragment" >
<FrameLayout
android:id="#+id/myContainer"
...(Other attrs)>
</FrameLayout>
</RelativeLayout>
and then
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.myContainer, new Tab1Fragment()).commit();
}
I have constructed a TabHost inside my navigation drawer. That means I've created 3 Fragment with xml files each: ExhibitorFragment, TabInternationalFragment, and TabLocalFragment. Now the problem is, I can't see my tabwidget under the Toolbar even though it displays design xml in either two of the fragments.
below is my code for ExhibitorFragment.java:
public class ExhibitorFragment extends Fragment{
public ExhibitorFragment() {
// Required empty public constructor
}
private FragmentTabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment_exhibitor);
Bundle arg1 = new Bundle();
arg1.putInt("Arg for Frag1", 1);
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Frag Tab1"),
TabInternationalFragment.class, arg1);
Bundle arg2 = new Bundle();
arg2.putInt("Arg for Frag2", 2);
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Frag Tab2"),
TabLocalFragment.class, arg2);
return mTabHost;
}
#Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}
fragment_exhibitor.xml :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.guitarista.citem.Example">
<TabHost android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/International"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<FrameLayout
android:id="#+id/Local"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
</FrameLayout>
The TabInternationalFragment and TabLocalFragment are set to default when created same as their respected xml (fragment_tab_international, fragment_tab_local).
Your Java Code Is correct Try Changing the XML file to this format
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_marginTop="55dp"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="45dp"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Usr this code
<TabHost android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:layout_below="#android:id/tabs"
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/International"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<FrameLayout
android:id="#+id/Local"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
</FrameLayout>
FragmentTabHost mTabHost;
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("tab2"),
tab1.class,null);
mTabHost.addTab(mTabHost.newTabSpec("tab2")
.setIndicator("tab2"), tab2.class, null);
I've followed this tutorial Android Tabs in Fragments. Though this one is likely similar to Pager Sliding tab strip. Take Note that you still need to implement fragment codes for this to work.
I have a problem with my TabHost, inside a Fragment, the first tab to be selected will always be highlighted, so I have often two tabs selected as shown here
Here Notifications should not be highlighted !
But the problem is only for Notifications tab, as you can see here the tab friends behave just normally, because it is the second added..
Here is my code for the fragment holding my TabHost
public class ProfileContentFragment extends Fragment {
private FragmentTabHost mTabHost;
public ProfileContentFragment() {
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.profile_content_fragment,container, false);
mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("notifications").setIndicator("Notifications "),
NotificationsFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("friends").setIndicator("Friends"),
FriendsFragment.class, null);
return rootView;
}
#Override
public void onDestroyView(){
super.onDestroyView();;
mTabHost=null;
}
}
And here is the XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</RelativeLayout>
SOLVED :
It was solved by changing
mTabHost.setup(getActivity(), getFragmentManager(), android.R.id.tabcontent);
To :
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);
I am using a class that extends Fragment and not FragmentActivity and I cannot seem to make horizontal scroll to work. I first used a layout as simple as the first code below and it works perfectly (without the scroll of course). I decided to put a horizontal scroll because when my tab reaches 6, the text is so hard to read and when the text is too long, the complete text is not written. I tried to use the layout of other people that said "This works!". Please refer to the second code below; the third code is my fragment class. Thank you!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
The layout that other people said "It works"
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Lastly, my Fragment class. Maybe I'm missing something out?
public class AlarmTab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentTabHost tabHost;
tabHost = new FragmentTabHost(getActivity());
tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.alarm_tab);
for(int i=0; i<SmashDeviceData.get_instance().devices.size(); i++) {
Bundle bundle = new Bundle();
bundle.putInt("Arg for Frag" + i, 1);
tabHost.addTab(tabHost.newTabSpec("Tab"+i).setIndicator(SmashDeviceData.get_instance().devices.get(i).deviceName), AlarmActivity.class, bundle);
}
return tabHost;
}
public static Fragment newInstance() {
Fragment fragment = new AlarmTab();
return fragment;
}
}
try the below
for xml
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tabsColor"
android:scrollbars="none" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tabsColor"
android:gravity="center_horizontal" />
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
for your fragment
public class HomeFragment extends Fragment {
private FragmentTabHost mTabHost;
TabWidget widget;
HorizontalScrollView hs;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
initView(rootView);
return rootView;
}
private void initView(View rootView) {
mTabHost = (FragmentTabHost) rootView
.findViewById(android.R.id.tabhost);
widget = (TabWidget) rootView.findViewById(android.R.id.tabs);
hs = (HorizontalScrollView) rootView
.findViewById(R.id.horizontalScrollView);
mTabHost.setup(getActivity(), getChildFragmentManager(),
android.R.id.tabcontent);
}
}
I have 2 fragments FragmentA and FragmentB. FragmentA is on top of FragmentB. Now I want to add tabs on FragmentB. Please Help.
My code so far :-
layout/fragmentA.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Fragment" />
</LinearLayout>
layout/fragmentB.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F5F6CE"
android:orientation="vertical" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:id="#+id/tabdata1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout=“#layout/tabdata1_view"/>
<include
android:id="#+id/tabdata2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout=“#layout/tabdata2_view"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
FragmentB.java
public class Fragment2 extends Fragment{
TabHost myTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2_layout, container, false);
myTabHost = (TabHost) getView().findViewById(R.id.tabhost)
return rootView;
}
}
Error:-
The line “myTabHost = (TabHost) getView().findViewById(R.id.tabhost)”gives an error as it cannot find tabhost.
Please suggest a way to achieve this.
Try this
myTabHost = (TabHost) rootview.findViewById(android.R.id.tabhost);
This should remove the error which you are facing now
That's because until you return from onCreateView, the fragment does not actually have a view yet. You just need to call findViewById relative to rootView. Like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2_layout, container, false);
myTabHost = (TabHost) rootView.findViewById(R.id.tabhost)
return rootView;
}