Search code examples
androidandroid-layoutandroid-fragmentsandroid-nested-fragmentandroid-adapterview

Fragment doesnt show up


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!


Solution

  • 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>