Thursday, January 24, 2013

Android Fragment at 2.3

Make a project with the name HelloFragment and creat main class as MainActivity having the code


package jitesh.fragmentexample;

import jitesh.fragmentexample.R;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

make the layout main.xml as


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
    <fragment
        android:id="@+id/frag_series"
        android:layout_width="200dip"
        android:layout_height="match_parent"
       
        class="jitesh.fragmentexample.ListFrag" />
    <fragment
        android:id="@+id/frag_capt"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="jitesh.fragmentexample.DetailFrag" />
</LinearLayout>

now create a claass ListFrag.java with the code


package jitesh.fragmentexample;

import jitesh.fragmentexample.R;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListFrag extends ListFragment{

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Amit", "Nitin", "Chintan", "Lakshami", "Jitesh"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
DetailFrag frag = (DetailFrag) getFragmentManager().findFragmentById(R.id.frag_capt);
if (frag != null && frag.isInLayout()) {
frag.setText(getCapt(item));
}
}

private String getCapt(String ship) {
if (ship.toLowerCase().contains("amit")) {
return "Amit ";
}
if (ship.toLowerCase().contains("nitin")) {
return "Nitin";
}
if (ship.toLowerCase().contains("chintan")) {
return "Chintan";
}
if (ship.toLowerCase().contains("lakshami")) {
return "Lakshami";
}
if (ship.toLowerCase().contains("jitesh")) {
return "Jitesh";
}
return "Jitesh Upadhyay";
}
}


create another class DetailFrag.java with code 

package jitesh.fragmentexample;


import jitesh.fragmentexample.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DetailFrag extends Fragment{

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detail_fragment, container, false);
return view;
}

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


the detail_fragment.xml has following elements

<?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/captain"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginTop="20dip"
        android:text="Jitesh Upadhyay"
        
        android:textSize="30dip" />
</LinearLayout>

the manifest should look like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jitesh.fragmentexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:name=".DetailActivity" >
        </activity>
    </application>

</manifest>

please follow the directory structure and dependencies for smooth running of the given code




No comments:

Post a Comment