Post Top Ad

Post Top Ad

Saturday 8 July 2017

Dynamically Add Fragment in Android

Dynamic layouts. A fragment is usually used as part of an activity's user interface and contributes its own layout to the activity. A fragment is implemented as independent object -- independent of the activity that contains it. The benefit is that it can be used by multiple activities associated with the application

1activity.main.xml:

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2"
    tools:context="com.example.kiran.lesson10_dynamicfragments.MainActivity">

    <fragment
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:name="com.example.kiran.lesson10_dynamicfragments.FirstFragment"
        android:id="@+id/fragment"
        android:layout_weight="1" />

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/frameLayout">

    </FrameLayout>

</LinearLayout>



2.MainActivity.java

package com.example.kiran.lesson10_dynamicfragments;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    private FragmentManager mFragmentManager;
    private FragmentTransaction mFragmentTransaction;
    private SecondFragment mSecFrag;
    private ThirdFragment mThirdFrag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFragmentManager = getFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();

        mSecFrag = new SecondFragment();
        mThirdFrag = new ThirdFragment();

        mFragmentTransaction.add(R.id.frameLayout,mSecFrag);
        mFragmentTransaction.commit();
    }

    public void change() {
        mFragmentTransaction = mFragmentManager.beginTransaction();
        if(mSecFrag.isResumed()){
            mFragmentTransaction.replace(R.id.frameLayout,mThirdFrag);
        }else{
            mFragmentTransaction.replace(R.id.frameLayout,mSecFrag);
        }
        mFragmentTransaction.commit();
    }
}


3.FragmentFirst.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"
    android:background="#fcd0d0">

    <Button
        android:text="Fragment Blue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/change_blue" />

    <Button
        android:text="Fragment Green"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/change_green" />
</LinearLayout>


4.FirstFragment.java
package com.example.kiran.lesson10_dynamicfragments;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Created by kiran on 11/1/2016.
 */

public class FirstFragment extends Fragment {
    private Button mChangeGreen;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first,container,false);
        mChangeGreen = (Button) view.findViewById(R.id.change_green);
        mChangeGreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity activity = (MainActivity) getActivity();
                activity.change();
            }
        });

        return view;
    }
}

6.fragmentsecond.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"
    android:background="#bfd4ec">

</LinearLayout>


7.SecondFragment.java:

package com.example.kiran.lesson10_dynamicfragments;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by kiran on 11/1/2016.
 */

public class SecondFragment  extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_second,container,false);
        return view;
    }
}

 

No comments:

Post a Comment