Tabs Activities inside Fragment Activity
Posted By : Chandan Wadhwa | 25-Dec-2014
Fragments are used for creating dynamic and multi-pane user interface in android. A fragment works as a modular section of an activity. A fragment inflates a layout to display contents. Android provides a Fragment Activity inside this we can inflate any number of independent fragment. So Fragment Activity is a base for fragment.
A tab activity can display fragment by visiting tabs. Each tab of tab activity inflate a different fragment.
But sometimes there is requirement to display tabs inside a fragment activity. I wrote the code for how to call many activities inside activity using fragment.
![](https://s3.amazonaws.com/oodles-blogs/blog-images/65e71800-6907-40c9-82bd-3882f27d1a4e.jpeg)
Main Fragment with Tabs
Fragment Activities Layout Code
Fragment Activities Source Code
public class HomeFragment extends Fragment {
FragmentTransaction fragmentTransaction;
FragmentManager fragmentManager;
BuyAdsFragment buyAdsFragment;
SellAdsFragment sellAdsFragment;
LocationFragment locationFragment;
Button btnbuyads,btnsellads,btnlocation;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home_fragment, container, false);
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
//Default fragment
buyAdsFragment= new BuyAdsFragment();
fragmentTransaction.replace(R.id.frag_layout, buyAdsFragment); //replacing fragment with the previous contents of framelayout (frag_layout)
fragmentTransaction.commit();
btnbuyads=(Button)rootView.findViewById(R.id.btnbuyads);
btnbuyads.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// inflate buyads fragment when tab1 is pressed
buyAds();
}
});
btnsellads=(Button)rootView.findViewById(R.id.btnsellads);
btnsellads.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//inflate sellAds fragment when tab2 is pressed
sellAds();
}
});
btnlocation=(Button)rootView.findViewById(R.id.btnlocation);
btnlocation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//inflate location fragment when tab3 is pressed
location();
}
});
return rootView;
}
//Methods to inflate layout when particular tab is pressed
public void buyAds()
{
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
buyAdsFragment= new BuyAdsFragment();
fragmentTransaction.replace(R.id.frag_layout, buyAdsFragment);
fragmentTransaction.commit();
}
public void location()
{
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
locationFragment= new LocationFragment();
fragmentTransaction.replace(R.id.frag_layout, locationFragment);
fragmentTransaction.commit();
}
public void sellAds()
{
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
sellAdsFragment= new SellAdsFragment();
fragmentTransaction.replace(R.id.frag_layout, sellAdsFragment);
fragmentTransaction.commit();
}
}
Note :- Import Fragment class from “android.app.Fragment” instead of V4.app.Fragment”
Location Fragment
public class LocationFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_location,
container, false);
return rootView;
}
}
BuyAds Fragment
public class BuyAdsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_buyads,
container, false);
return rootView;
}
}
Thanks
You might also like :-
1) Displaying chart inside Fragment Activity in Android
2) Move activity to foreground when screen is in off state
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Chandan Wadhwa
Chandan is an Android Apps developer with good experience in building native Android applications.