TabHost with Fragments and FragmentActivity
I would suggest creating a separate fragment file for each tab. I recently did this as well, so I have outlined my code below:
Layout Files
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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
tab1_view.xml //add your respective tab layouts using this format (make sure to change string variables)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".DeviceFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tab1_fragment_string" />
</LinearLayout>
SRC Files
MainActivity.java //notice that in the .addTab
process I only used text. You can also add icons using drawables that you would need to add to your hdpi folder. I also only created three tabs in this example.
package com.example.applicationname;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
public class MainActivity extends FragmentActivity {
// Fragment TabHost as mTabHost
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(), R.id.realtabcontent);
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);
}
}
Tab1Fragment.java //once again replicate for desired number of tabs
package com.example.applicationname;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Tab1Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View V = inflater.inflate(R.layout.tab1_view, container, false);
return V;
}
}
Make sure that your R.java and strings.xml files are properly set up, and then your tabs should be up and running.
how to load tabhost in a fragment
you can do like this.
You should use TabLayout instead of TabHost.
Make Xml for main fragment which content Tabs.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Then Make Code like below:
public class TabsFragment extends Fragment {
View view;
PagerAdapter adapter;
private void setupViewPager(ViewPager viewPager) {
///Here we have to pass ChildFragmentManager instead of FragmentManager.
adapter = new PagerAdapter(getChildFragmentManager());
adapter.addFragment(new Fragment1(), "Fragment1");
adapter.addFragment(new Fragment2(), "Fragment2");
viewPager.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.f_tabslayout, container, false);
super.onCreate(savedInstanceState);
final ViewPager viewPager = (ViewPager)view.findViewById(R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout)view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
static class PagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
}
TabHost with FragmentActivity
The PhotosActivity used to extend the Activity class and now extends
the FragmentActivity class. I simply get a cast error when the
application opens.
The cast exception happens because the add() method of the FragmentTabHost expects the class of a Fragment and not something that extends Activity. You can't simply make the transition between simple activities and fragments by making your activities to extend the Fragment class, instead you need to refactor your current classes.
Replacement of TabActivity with FragmentActivity and Fragments
Since TabActivity is deprecated I need to find a way to do it with Fragments.
If you don't want to use TabActivity
- forget about putting FragmentActivities
into tab's content.
I remind that you can use TabWidget
without TabActivity
. So you can try this solution:
- Create just one
FragmentActivity
. - Put
TabWidget
intoFragmentActivity
's layout. MakeTabWidget
's content's height = 0. - Under
TabWidget
in XML declare container for youFragment
s (FrameLayout
for example). - In
FragmentActivity
just handle which tab is selected (TabHost.OnTabChangeListener
) and put neededFragment
into container. - Put programm logics (which was earlier in different activities) into different Fragments.
Or you can create FragmentActivity
with TabWidget
, and instead of switching Fragment
s you can directly put Fragment
s into each tab's content.
For example if you have 3 tabs and 3 fragments try what i do. Call showFragmentX
when you need to change one fragment to another.
public class Test extends FragmentActivity {
private Fragment1 fragment1=new Fragment1();
private Fragment2 fragment2=new Fragment2();
private Fragment3 fragment3=new Fragment3();
private void showFragment1(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragments_container, fragment1);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
private void showFragment2(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragments_container, fragment2);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
private void showFragment3(){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragments_container, fragment3);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.fragmentactivity_layout);
}
}
If you do so your fragmentX
variables will not be deleted each time you put them in fragment_container
. They will live while your FragmentActivity
live. Take look at fragments lifecycle.
Only OnCreateView
and onDestroyView
methods of fragments will call again and again while you replace
one fragment to another.
Also Fragments has their onSaveInstanceState
method where you can save the state of your fragment. For example: user typed his name in fragment1's editText. If you want to keep this data(name string) while user discover other fragments you should
1.save name string in fragment1's onSaveInstanceState
method
2. into fragment1's onCreateView
method check savedInstanceState bundle, if it's not null - fill edittext with string that you get from bundle.
public class Fragment1 extends Fragment {
EditText nameEditText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View fragment1View=inflater.inflate(R.layout.fragment1_layout, container, false);
nameEditText=(EditText) fragment1View.findViewById(R.id.edittext_name);
//check for saved data. if it is not null - fill your edittext with saved string
if(savedInstanceState!=null){
String nameString=(String) savedInstanceState.get("nameString");
nameEditText.setText(nameString);
}
return fragment1View;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//save data that user have entered
outState.putString("nameString", nameEditText.getText().toString());
}
}
You can check data before saving. I hope my point is clear now.
Also if you call setRetainInstance(true)
in onCreateView()
method of your fragment
- system will try to save the state of fragment (with all input data). link to description
Nested tabhost fragment android
After a while I came with a solution.
As I said each fragment has its own class and layout, so in Tab1 layout I added FragmentTabHost.
And in onCreateView method of Tab1 I initializing the FragmentTabHost and adding the inner tabs for it.
Related Topics
Validate Username and Email Crashed and Cannot Insert to Firebase Database
How to Create Custom UI for Android Mediacontroller
Trying to Get the Display Size of an Image in an Imageview
How to Use View.Ontouchlistener Instead of Onclick
How to Retrieve the Logged in Google Account on Android Phones
How to Make Android Apps Which Support Both 32-Bit and 64-Bit Architecture
How to Simulate Android Killing My Process
"Failure Delivering Result " - Onactivityforresult
How to Determine Which Dependency Causes Google Play Openssl Warning
Swiperefreshlayout Setrefreshing() Not Showing Indicator Initially
Compiling the Latest Openssl for Android
How to Use Radiogroup in Listview Custom Adapter
Get List of Installed Android Applications
Why Mediaplayer Throws Not Present Error When Creating Instance of It