Tuesday, 9 July 2013

Fragment In Android Example In seven easy step

Android introduced fragments in Android 3.0 (API level 11) before create such type of project first put this line in your manifest file 


<uses-sdk  android:minSdkVersion="11"    android:targetSdkVersion="16" /> or more API 

Note:- Nothing else configuration for that 

For that demo we need device configuration like 




In that demo application we use List Fragment Activity for display List view in one fragment and on click over the particular index of list display the detail left portion 

Step -1 Create Project in Android 
Step -2 put the following code in main.xml

<LinearLayout 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"
    tools:context=".MainActivity" >
<fragment 
    android:id="@+id/list"
    android:layout_width="200dp"  
    android:layout_height="wrap_content"
    android:layout_margin="?android:attr/actionBarSize"
    class="com.example.demofrag.ListActivityDemo"
    />
 <fragment 
     android:id="@+id/detail"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     class="com.example.demofrag.DetailFragment"
     />
</LinearLayout>


Step:3 put following code in MainActivity.java


package com.example.demofrag;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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



}


Step-4 create ListFragment.java class 



package com.example.demofrag;

import android.annotation.SuppressLint;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

@SuppressLint("NewApi")
public class ListActivityDemo extends ListFragment {

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String data =(String)getListView().getItemAtPosition(position);
   DetailFragment detailFragment = (DetailFragment)getFragmentManager().findFragmentById(R.id.detail);
   if(detailFragment!=null && detailFragment.isInLayout()){
    detailFragment.setText(data);
   }
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
 String[] arrray = new String[]{"JAVA","JEE","JSE","ANDROID"};
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1,arrray);
      setListAdapter(adapter);
}

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
  
}


Step 5 create DeatilFragment.java

package com.example.demofrag;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

@SuppressLint("NewApi")
public class DetailFragment extends Fragment{

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.frag_tree, container ,false);
return view;

}
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.captain);
view.setText(item);

  
}

Step:6 Create frag_one.xml layout



<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    

</fragment>

Step 7: Create frag_three.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="match_parent" >
    <TextView
        android:id="@+id/captain"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginTop="20dip"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="30dip" />

</LinearLayout>



for more Detail check below  
http://developer.android.com/training/basics/fragments/creating.html