Thursday 4 August 2016

Fragment | Example of Fragment

Fragment:- 

  • A fragment is a piece of  an activity which enable more modular activity design.
  • A fragment is a kind of sub-activity.
  • A fragment has its own life cycle and behaviour.
  • We can combine multiple fragment into a single activity.
  • A fragment can used by multiple activity.
We can create fragment by extending Fragment class.

Example:-
Step1:- Open your Android Studio.
Step2:-Click on File >New >New Project> FragmentExample
Step3:-Click on res>activity_main.xml and take 3 Button
Step 4:- activity_main.xml code will be like following.

<?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="vertical"
    android:layout_marginTop="5dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="HOME"
            android:onClick="onHome"
            android:gravity="center"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="CONTACT"
            android:onClick="onContact"
            android:gravity="center"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="SERVICE"
            android:onClick="onService"
            android:gravity="center"/>
    </LinearLayout>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fram1">
</FrameLayout>
</LinearLayout>


Step 5:- MainActivity.java code will be like following.
package androidhubb.fragtest;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends FragmentActivity {
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager=getSupportFragmentManager();
    }
    public void onHome(View v){
        fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fram1,new Home());
        fragmentTransaction.commit();

    }
    public void onContact(View v){
        fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fram1,new Contact());
        fragmentTransaction.commit();
    }
    public void onService(View v){
        fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fram1,new Services());
        fragmentTransaction.commit();
    }
}

Step 6:- Make 3 xml file and their class file with the name of following 

           1. Home.java -> home.xml
        2. Contact.java -> contact.xml
        3. Service.java -> service.xml
Step 7:-  .java file of Home | Contact | Service will be like following

               Home.java | Contact.java | Service.java


public class Home extends Fragment {

    @Nullable    
@Override    
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.home,container,false);
        return v;
    }
}

Output:-




No comments:

Post a Comment