Fragment doesnt show up - android

I am trying to make an activity containing two fragments.The first one has 3 nested fragments which I will use as a tabs and the second one only contains buttons. I don't want to add the buttons fragment into other fragments because I don't want it to reload. Right now i don't have any errors but my buttons fragment doesn't show up. Here are my fragments and my activity.
my main activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
Fragment statistics = new StatisticsFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_statistics, statistics).commit();
Fragment buttons = new MainPageButtonsFragment();
FragmentTransaction buttonsTransaction = getSupportFragmentManager().beginTransaction();
buttonsTransaction.add(R.id.main_page_buttons_fragment,buttons).commit();
his layout
<RelativeLayout android:orientation="vertical" >
<FrameLayout
android:id="#+id/fragment_statistics"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/main_page_buttons_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/fragment_statistics"/>
I wont write my buttons fragment because it is not necessary I will only say its root tag is FrameLayout.
my statistics fragment (the one with tabs)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Fragment currentDayFragment = new CurrentDayFragment();
FragmentTransaction transaction1 = getChildFragmentManager().beginTransaction();
transaction1.add(R.id.current_day_fragment, currentDayFragment).commit();
Fragment configurationInfoFragment = new ConfigurationInfoFragment();
FragmentTransaction transaction2 = getChildFragmentManager().beginTransaction();
transaction2.add(R.id.configuration_info_fragment, configurationInfoFragment).commit();
Fragment expensesInfoFragment = new ExpensesInfoFragment();
FragmentTransaction transaction3 = getChildFragmentManager().beginTransaction();
transaction3.add(R.id.expenses_info_fragment, expensesInfoFragment).commit();
LayoutInflater lf = getActivity().getLayoutInflater();
View statisticsView = lf.inflate(R.layout.fragment_statistics, container, false);
return statisticsView;
}
his layout:
<FrameLayout >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#id/current_day_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#id/configuration_info_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#id/expenses_info_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</android.support.v4.view.ViewPager>
the nested fragments just have root tag FrameLayout and couple of TextViews in it. The result is just an activity with 3 tabs and no buttons fragment.
I am new to android programming and I am completely lost in this. Help Please!

Try using giving custom height like this....
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/fragment_statistics"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<FrameLayout
android:id="#+id/main_page_buttons_fragment"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>

Relative layout does not have android:orientation param
I thing first fragment cover second one
Change relative layout with LinearLayout
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fragment_statistics"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/main_page_buttons_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/fragment_statistics"/>
</LinearLayout>

Related

Fragment is added to activity and fragments oncreateview is called but view is not being inflated?

I have an activity which extends appcompatactivity and i'm adding a fragment in framelayout inside this activity.The fragments oncreateview is being called but fragments view is not being inflated or shown.
AppCompat activity's layout:
<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"
android:background="#fff" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/contain">
</FrameLayout>
</FrameLayout>
I'm adding fragment to the child framelayout.The code to attach fragment is below:
OneFragment m = new OneFragment();
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.contain,m);
ft.addToBackStack(null);
ft.commit();
This is the code inside oncreateview of OneFragment:
View view = inflater.inflate(R.layout.fragment_one, container, false);
//Find the +1 button
mPlusOneButton = (PlusOneButton) view.findViewById(R.id.plus_one_button);
return view;
Fragment_one.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=".OneFragment">
<com.google.android.gms.plus.PlusOneButton
xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus"
android:id="#+id/plus_one_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
plus:size="standard"
plus:annotation="inline" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>

Fragment doesn't replace and hide another fragment

I'm working on an android project and my fragment are not working...
I'm trying to have this kind of UI ->
http://developer.android.com/training/basics/fragments/fragment-ui.html
However my second fragment doesn't replace or hide the first one, it's simply going under it!
This is how I declare my first fragment in the main activity:
setContentView(R.layout.finallayout);
Bundle bundle = new Bundle();
bundle.putStringArrayList("urllist", URLlist);
// set Fragmentclass Arguments
MainFragment main = new MainFragment();
main.setArguments(bundle);
getFragmentManager().beginTransaction().add(R.id.scrollcontentcontainer, main,"list").commit();
In the onCreateView of this one, I'm adding some content:
mainview = inflater.inflate(R.layout.finallayout, container, false);
scollviewgroup= (ViewGroup) container.findViewById(R.id.scrollcontentcontainer);
//mainview.setOnClickListener(this);
for (int i = 0; i < json.size(); i++) {
lineartab.add(inflater.inflate(R.layout.relativeaqi, scollviewgroup, false));
createMainLayout(this.json.get(i), i, lineartab.get(i), scollviewgroup, inflater);
}
and that's the way I declare the second one:
Fragment contentFragment = new CoreFragment();
FragmentManager fm = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
// fragmentTransaction.add(contentFragment, "core");
fragmentTransaction.replace(R.id.scrollcontentcontainer, contentFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
On the onCreateView, I'm inflating with a layout contained in a other layout file:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//container.removeAllViews();
//IF I USE THIS ONE I HAVE THE EXPECTED RESULT, HOWEVER I M NOT SURE THATS THE BEST WAY TO DO IT SINCE ITS REMOVING THE CONTENT INCLUDED IN THE OTHER FRAGMENT
View view = inflater.inflate(R.layout.placecore, container, false);
return view;
}
Trying to use add instead of replace, or using hide doesn't work.
I want my two fragments to use the same layout template, but with an unique instance of it for each fragment, I don't know if it's possible or not. I tried to use a different layout file for the second one but then I had always an error saying No view found for id...
Anyone know how to fix that? thanks
xml files:
finallayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/bottomcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:focusable="false">
</RelativeLayout>
<LinearLayout
android:id="#+id/scrollcontentcontainer"
android:focusable="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
and placecore.xml, which contains the content of the 2nd fragment.
It contains many textviews and buttons I hide to make it more clear(i put "some content" instead):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/corecontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="20dp"
android:clickable="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/alayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
some content
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#1d010101"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp">
some content
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/playout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
some content
</LinearLayout>
</LinearLayout>
Following Michael's comment, using the support library ('android.support.v4.app.Fragment') fixed my problem.

Activity layout is still visible after adding fragment to the activity

This is a really lame question, but I really cannot figure out why it happens, so please help me out.
I have a simple activity and I add a fragment to it dynamically. The problem is that once the fragment is added to the activity, the activity layout is also visible. Why does this happen?
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLoad = (Button) findViewById(R.id.btn1);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
};
btnLoad.setOnClickListener(listener);
}
}
activity_main.xml
<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" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
</LinearLayout>
HelloFragment.java
public class HelloFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflating the layout for this fragment **/
View v = inflater.inflate(R.layout.hello_fragment_layout, null);
return v;
}
}
hello_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="fragment 1"/>
</LinearLayout>
Convention is to have a FrameLayout that you use fragmentTransaction.replace()/add() on.
You shouldn't have views like TextView and Button in your MainActivity layout file. Instead, have those in another fragment and just have a FrameLayout in your MainActivity xml. Load the fragment with TextView/Button first, and call replace on button click with your new fragment.
Set a background color to your fragment linearLayout and make it clickable
The fragment is just another element on the layout. Adding it to the layout does not automatically remove other views. If you want the fragment to replace the content of rootView you need to remove rootview's children views first. Something like:
((LinearLayout)findViewById(R.id.rootView)).removeAllViews();
So your onClick method should look like this:
#Override
public void onClick(View v) {
((LinearLayout)findViewById(R.id.rootView)).removeAllViews();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
your rootview work as container for both fragment and your views on activity. check it and make separate view for fragment.
I know it's very late to answer this question but for those who are still looking do the following in your activity layout:
<RelativeLayout
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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
</RelativeLayout>
And fragment layout to following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#color/white" <!--Add this-->
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
activity_main.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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootView"
android:orientation="vertical">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</FrameLayout>
MainActivity.java
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.fragmentContainer, hello, "HELLO");
fragmentTransaction.commit();
}
};
I have a solution for this to achieve what you can do is make two fragment one for textview and button your showing as default when activity is opened and another which you have already created as HelloFragment.
From Activity onCreate() method call the first fragment with default textview and button and from the click listener of that button call HelloFramgent.
Step1: Crate another fragment with any name MainFragment having a xml layout as follows:-
main_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="OK"/>
</LinearLayout>
MainFragment.java - Class
public class MainFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflating the layout for this fragment **/
View v = inflater.inflate(R.layout.main_fragment_layout, container, false);
Button btnLoad = (Button) v.findViewById(R.id.btn1);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
HelloFragment hello = new HelloFragment();
fragmentTransaction.add(R.id.rootView, hello, "HELLO");
fragmentTransaction.commit();
}
};
btnLoad.setOnClickListener(listener);
return v;
}
}
Step2: Call the MainFragment from activity onCreate() method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MainFragment mainFragment = new MainFragment();
fragmentTransaction.add(R.id.rootView, mainFragment, "MAIN_FRAGMENT");
fragmentTransaction.commit();
}
Build your user interface in Fragments (In most cases one Fragment equals one screen) and then use Activities to arrange and display those Fragments.
check this: click here to view detailed answer
You have two linear layouts, one inside another, so you need to do the following steps:
Set id to 1 linear layout rootView and next to the childview.
Find your child view:
View view = findViewById(R.id.child_view);
view.setVisibility(View.INVISIBLE);
Replace your rootView with your fragment:
getSupportFragmentManager().beginTransaction().add(R.id.rootview, new blankFragment())
.addToBackStack(null).commit();
Then override:
#override
public void onBackPressed() {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
view.setVisibility(View.VISIBLE);
}
super.onBackPressed();
}
The Activity views are not being masked against the Fragment's views, so both appear. One solution is to load the fragment into a frame in the main activity's layout which has a slight elevation (2 dp). When the fragment is loaded its base elevation will cause it to always draw over the activity. Set the fragments parent container with a color to hide the activity views.
MasterActivity layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Activity Title"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:textSize="24sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:textAllCaps="false"
android:text="Button1" />
<Button
android:id="#+id/button2"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:text="Button2" />
<!-- Fragment holder, with elevation -->
<FrameLayout
android:translationZ="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragHolder"
/>
The Fragment layout has a background color:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:layout_margin="10dp">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Test Background Task"
android:textSize="24sp" />
</RelativeLayout>
Make your First Layout invisible while calling fragment and again while returning from fragment make your main view visible and remove fragment at that time so it will not conflict with each other it worked for really well just check it once

Android 3 - Adding a Fragment to a LinearLayout: fill_parent does not work

I am trying to add a fragment programmatically to a LinearLayout, but the fragment does not stretch its content across the whole layouts height. It acts like wrap_content instead of fill_parent.
On the other side the fill_parent works on the fragment's width.
How can I change this behaviour?
DashboardActivity:
<?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="horizontal" >
<fragment
android:id="#+id/fragment_dashboard"
android:layout_width="5dp"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:name="de.upb.cs.ginkgo.tablet.ui.fragments.DashboardFragment" >
<!-- Preview: layout=#layout/fragment_dashboard -->
</fragment>
<LinearLayout
android:id="#+id/rightfrag"
android:layout_width="450dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp" >
</LinearLayout>
</LinearLayout>
Its onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
//-- Instantiate Fragmentstuff
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ActivitystreamFragment asf = new ActivitystreamFragment();
fragmentTransaction.add(R.id.rightfrag, asf);
fragmentTransaction.commit();
}
The ActivitystreamFragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ListView
android:id="#+id/android:list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="true"
android:background="#drawable/back_rightfrag"
>
</ListView>
</LinearLayout>
And its createView:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_activitystream, null);
return root;
}
Ok - this one was pretty dumb. I changed my container from a LinearLayout to a FrameLayout and voilĂ : it's working.

Android how to interact with Fragment tabs

Hello I am having difficulty changing the visual view of the fragment activity. I created all the tabs but onClick I do not know how to pass the activity into that white space.
so the structure of this is a simple activity named Main which just sets the content view to a maintabholder.xml document that holds the fragment view which connects to a class named MainNav whose content view contains all the tabs at the bottom.
maintabholder.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="#layout/header" android:layout_alignParentTop="true" android:id="#+id/headerArea"></include>
<fragment
class="my.package.name.MainNav"
android:id="#+id/fragment_tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
MainNav.java code snippet:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
mRoot = inflater.inflate(R.layout.fragment_tabs, null);
mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
setupTabs();
return mRoot;
}
and R.layout.fragment_tabs:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EFEFEF">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:background="#drawable/tab_background"
android:gravity="center_horizontal"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/tab_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/tab_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/tab_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/tab_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/tab_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</RelativeLayout>
and Finally the contents of an Activity that I want to load into the Fragment (upon pressing the tab1 button)
public class HomeFragment extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
}
That is to help you get the gist of things, but the problem occurs here in MainNav.java:
#Override
public void onTabChanged(String tabId) {
Log.d(TAG, "onTabChanged(): tabId=" + tabId);
if (TAB_HOME.equals(tabId)) {
updateTab(tabId, R.id.tab_1);
mCurrentTab = 0;
return;
}
}
when a tab is clicked, it is registered in the onTabChanged activity, and the updateTab function is called:
private void updateTab(String tabId, int placeholder) {
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(tabId) == null) {
//fm.beginTransaction()
// .replace(placeholder, /*need fragment object here*/ , tabId)
// .commit();
//
}
}
the commented out replace method is looking for an (int, fragment, string) and I guess it should then change the white space for me.
but I don't understand where I get the fragment object from! This phantom object needs to somehow contain my HomeFragment's xml layout and allow me to interact with everything else I've coded for the HomeFragment activity.
My first hunch is that my HomeFragment activity needs to extend FragmentActivity instead
just activity. But this is where I am hitting a wall.
There is very little documentation on the compatibility pack methods, but the documentation for TabActivity says not to use TabActivity anymore, so here I am. Developing for Android 2.2+
Insight appreciated.
You can't put Activities into Fragments, it's jut that simple. You have to convert your Activities that serve the tab-contents into Fragments. It shouldn't be a problem, given that you don't use fragments in those Activities.
So here are 2 rules to follow:
You can't put Activities into Fragments.
You can't put Fragments into Fragments.
I recommend that you keep that bottom fragment on your layout around just to handle the tab-switches in the tab-widget. If a tab switch happens, you report this to the parent Activity, which will load the corresponding Fragment into the are above the tab-fragment. For this to work, you'd have to modify you layout so it has a ViewGroup above the tab-fragment and below the header. You'd dynamically load the fragments with the tab-contents into that ViewGroup.
Here's the layout (maintabholder.xml):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="#layout/header" android:layout_alignParentTop="true" android:id="#+id/headerArea"></include>
<FrameLayout android:id="#+id/TabContent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"/>
<fragment
class="my.package.name.MainNav"
android:id="#+id/fragment_tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
When a tab-switch happens in your bottom fragment, you tell this to the parent Activity:
...
(YourActivity)getActivity().switchToTab(tabId);
...
And in the YourActivity:
...
public void switchToTab(int tabId){
switch(tabId){
...
case 2:
getSupportFragmentManager().beginTransaction().replace(R.id.TabContent, TabContentFragment2.newInstance(), "someTag").commit().
break;
...
}
}
Something like that.

Categories

Resources