Search code examples
androidfragmentandroid-fragmentactivity

FragmentActivity inside a fragment


I am new to android and i have some questions. I created a simple layout with two fragments such as follow:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/fragment1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<FrameLayout
    android:id="@+id/fragment2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

I want to divide this layout as follow :

layout_height of fragment1 : 1/3 height

layout_height of fragment2 : 2/3 height

How can do it?

I used below code to show fragment1 :

 FragmentTransaction transaction =
            getSupportFragmentManager().beginTransaction();

    transaction.add(R.id.fragment1, firstfragment);

But I don't know how to show FragmentActivity in fragment2 ?


Solution

  • You can achive this layout like this
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="3"
         >
    
        <FrameLayout
            android:id="@+id/fragment1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
        <FrameLayout
            android:id="@+id/fragment2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2" />
    
    </LinearLayout>
    

    and in java you must call commit in transaction

    //to add Fragment1

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.fragment1, firstfragment).commit();

    // to add Fragment2

    FragmentTransaction transaction1 =
            getSupportFragmentManager().beginTransaction();
    transaction1.add(R.id.fragment2, secondfragment).commit;
    

    and you can not put any activity inside Fragments. but you can start another activity from your fragments