In this tutorial I am going to discuss about fragments in android application.

Fragment is just a kind of sub-activity which is used to build a multi-pane UI and reuse a fragment in multiple activities. Fragments has their own life-cycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a “sub activity” that you can reuse in different activities).

The fragment lifecycle in android is just like the life-cycle of an activity.

Fragments are of two types :

  1. Static Fragment
  2. Dynamic Fragment

The major difference between static fragments and dynamic fragment in using fragments through XML or dynamically through java code.

In this tutorial I am just going to discuss you about static fragment.

1. Create a New Project

Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from templates to add Fragments to your app .

2. Add static Fragment to your activity_main.xml or main layout

Add static fragment to your main layout

<fragment
    android:id="@+id/static_fragment"
    android:name="com.technxtcodelabs.Staticfragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Now open res -> layout -> activity_main.xml  file and just implement the following xml code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <fragment
	android:name="com.technxtcodelabs.Staticfragment"
	android:id="@+id/static_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
 
</LinearLayout>

3. Create a Layout Resource file :

Now create a Layout Resource file and Add a fragment view named it as staticfragment.xml to implement static fragment in android .

<?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" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TestView" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test Button" />

</LinearLayout>

4. Now create a fragment class :

To create a fragment and extend the fragment class, then override onCreateView methods to insert your app logic or just add following code .

package com.technxtcodelabs;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;

public class Staticfragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.static_fragment, container, false);
    }
   @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // handle your fragment events here
        TextView txtView = (TextView) findViewById(R.id.tv);
        Button btn = (Button) findViewById(R.id.btn);
        b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(MainActivity.this,"YOUR MESSAGE",Toast.LENGTH_LONG).show();
         }
      });
     }
}

5. Now open your MainActivity.java and add following code :

package com.technxtcodelabs;

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


public class MainActivity extends AppCompatActivity {

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

    }
}

Now run the project/app in android device or any Emulator to add static fragment to your app.

%d bloggers like this: