how to create Fragment in android

What is fragment?

Fragment is a partial part of activity, that comprises of xml layout, and java code. Its main advantage, is the resuability of code, once the fragment is created, it can be called in different activity where needed. It works just similar to the include function, that is used in php, that mean if we want to include a code, from another webpage, we simply include that page, all the code is considered to be accessed, and present now in the currently working page.

How to create a fragments?

We can add fragment, using either xml layout, or using java , in this tutorial we will learn how can we make fragment using java code, and using xml representation.

How to create a fragment And add it to an activity in java?

1. Create a new project with name you like, with simply blank activity.
2. Secondly create a new class with name of myfragment and it should extent fragment class.
3. Create new xml layout with name of my_fragment_layout.xml .
4. Drag some elements in your xml file, may be a text view or button, what ever you like.
MainActivity.xml
Please remember to provide a seperate id to your relative layout , becuase this will be need later, in java file. As you can see below in MainActivity.java.

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

android:id="@+id/my_Layout" >
</RelativeLayout>

MainActivity.java:

package com.example.fargmenttutorial;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyFragment f = new MyFragment();
FragmentManager manager =getFragmentManager();
FragmentTransaction trasaction = manager.beginTransaction();
trasaction.add(R.id.my_Layout,f, "layoutTag");
trasaction.commit();

}

}

MyFragment.java

package com.example.fargmenttutorial;

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

public class MyFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.my_fragment_layout, container,false);
}

}

 

My_fragment_layout.xml

 


<?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="wrap_content"
android:orientation="vertical"
android:background="#ffbb66">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This trustingeeks.com"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

 

 

Conclusion:

So what this means is your can add different fragment to your activity , with working again and again, this is very simple , since we only add fragments, thing, what happens when you have multiple fragment in your activity and one of the item in the fragment, is click, how do we handle the inter communication between fragments and cativity, this can all be achived, lead us to more robust app development. One more thing to add fragments are very much use full, when it comes to tablet , since the space on tablet is more as compared to mobile application, there fore fragments play most important role their too.

how to create fragments in android

admin
 

student of computer Science , having interest and experience in C++,Java,Php,Html,Css,JavaScript, wordpress and android development