Sunday, 7 September 2014

List Fragment Example in Android

activity_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"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragment"
        android:layout_width="360dp"
        android:layout_height="match_parent"
        class="org.rajeshvijayakumar.fragments.MenuFragment" />

    <View android:id="@+id/dummy_view"
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray" />
    <RelativeLayout
        android:id="@+id/fragment2"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />

</LinearLayout>


list_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pref_frag_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/list" />
</RelativeLayout>


text_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:background="@android:color/white"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="21sp"
        android:textColor="@android:color/holo_purple"
        android:layout_gravity="center"
        android:id="@+id/name_text_view"/>
</LinearLayout>


MainActivity.java

package org.rajeshvijayakumar.fragments;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

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

MenuFragment.java

package org.rajeshvijayakumar.fragments;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MenuFragment extends ListFragment {
    String[] names = new String[] { "Rajesh", "Mahesh", "Mrithula", "Sonika",
            "Ramachander", "Sriram", "Omji", "Raji" };
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list_fragment, container, false);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, names);
        setListAdapter(adapter);
        return view;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment = new TextFragment();
       
        Bundle bundle = new Bundle();
        bundle.putString("names", names[position]);
        fragment.setArguments(bundle);
        fragmentTransaction.add(R.id.fragment2, fragment, "Fragment");
        fragmentTransaction.commit();
        getListView().setSelector(android.R.color.holo_blue_dark);
    }
}


TextFragment.java


package org.rajeshvijayakumar.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TextFragment extends Fragment {
    TextView text,vers;
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.text_fragment, container, false);
        text= (TextView) view.findViewById(R.id.name_text_view);
        Bundle bundle = getArguments();
        String name = bundle.getString("names");
        text.setText(name);
               
        return view;
    }
}


AndroidMainfest.xml

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Output :





Source in Github Repo.......