Fragment Example Create the following files and test the application on emulator or device. File: AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.atijaffna.fragmentex" > <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme" > <activity android:name=".fragmentactivity" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest> File: FragmentActivity.java package com.atijaffna.fragmentex; import android.app.fragment; import android.app.fragmentmanager; import android.app.fragmenttransaction; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; public class FragmentActivity extends AppCompatActivity { Button b1,b2; protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate); setcontentview(r.layout.activity_fragment); b1=(button)findviewbyid(r.id.button); b2=(button)findviewbyid(r.id.button2); View.OnClickListener listener = new View.OnClickListener() { public void onclick(view v) { Fragment fr; if (v==b1) fr=new FragmentA(); else fr=new FragmentB(); FragmentManager fm=getfragmentmanager(); FragmentTransaction ft=fm.begintransaction(); ft.replace(r.id.fragmentplace,fr); ft.commit(); ; b1.setonclicklistener(listener); b2.setonclicklistener(listener); File: FragmentA.java package com.atijaffna.fragmentex; import android.os.bundle; import android.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class FragmentA extends Fragment {
public FragmentA() { // Required empty public constructor public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { // Inflate the layout for this fragment return inflater.inflate(r.layout.fragment_a, container, false); File: FragmentB.java package com.atijaffna.fragmentex; import android.os.bundle; import android.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class FragmentB extends Fragment { public FragmentB() { // Required empty public constructor public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { // Inflate the layout for this fragment return inflater.inflate(r.layout.fragment_b, container, false);
File: activity_fragment.xml <?xml version="1.0" encoding="utf-8"?> <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:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".fragmentactivity"> <TextView android:text="hello World!" android:layout_width="wrap_content" android:id="@+id/textview" /> <Button android:layout_width="wrap_content" android:text="new Button" android:id="@+id/button" android:layout_aligntop="@+id/textview" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparentright="true" android:layout_alignparentend="true" /> <Button android:layout_width="wrap_content" android:text="new Button" android:id="@+id/button2" android:layout_below="@+id/button" android:layout_alignparentleft="true" android:layout_alignparentstart="true"
android:layout_alignright="@+id/button" android:layout_alignend="@+id/button" /> <fragment android:layout_width="wrap_content" android:name="android.preference.preferencefragment" android:id="@+id/fragmentplace" android:layout_below="@+id/button2" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparentbottom="true" android:layout_alignright="@+id/button2" android:layout_alignend="@+id/button2" /> </RelativeLayout> File: fragment_a.xml <FrameLayout 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="com.atijaffna.fragmentex.fragmenta"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" android:background="#fbef6e" /> </FrameLayout>
File: fragment_b.xml <FrameLayout 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="com.atijaffna.fragmentex.fragmentb"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> </FrameLayout> Try it yourself: Creating Project Create a new Android Application project in Android Studio with package as com.example.fragments. Create the main layout as activity_main and main Activity as MainActivity. Creating Fragment Layouts We are going to use two fragments with our main layout. The first fragment consists of a ListView. It is defined in list_fragment.xml. list_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="match_parent" android:layout_height="match_parent"> <ListView android:layout_width="match_parent" android:id="@android:id/list" /> </LinearLayout>
The second fragment consists of two TextView to display Android OS name and Android version number. 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="match_parent" android:gravity="center" android:background="#5ba4e5" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:textsize="40px" android:textcolor="#ffffff" android:layout_gravity="center" android:id="@+id/androidos"/> <TextView android:layout_width="wrap_content" android:layout_gravity="center" android:textcolor="#ffffff" android:textsize="30px" android:id="@+id/version"/> </LinearLayout>
Creating Fragment Classes Our first class is TextFragment which extends to Fragment. We use LayoutInflator to display the layout for our Fragment. It consists of two Textviews. A function change is used to change the text in the TextView. TextFragment.java package com.amal.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; { public View oncreateview(layoutinflater inflater,viewgroup container, Bundle savedinstanc View view = inflater.inflate(r.layout.text_fragment, container, false); text= (TextView) view.findviewbyid(r.id.androidos); vers= (TextView)view.findViewById(R.id.Version); return view; public void change(string txt, String txt1){ text.settext(txt); vers.settext(txt1);
Our second class is MenuFragment which extends to ListFragment. A array of strings AndroidOS and Version is defined which contains Android version names and number. When a list item is clicked the TextView on the TextFragment is updated with the Android version name and number. MenuFragment.java package com.amal.fragments; 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[] AndroidOS = new String[] { "Cupcake","Donut","Eclair","Froyo", "Gingerbread","Honeycomb","Ice Cream SandWich","Jelly Bean","KitKat" ; String[] Version = new String[]{"1.5","1.6","2.0-2.1","2.2","2.3","3.0-3.2", "4.0","4.1-4.3","4.4"; 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, AndroidOS); setlistadapter(adapter); return view; public void onlistitemclick(listview l, View v, int position, long id) {
TextFragment txt = (TextFragment)getFragmentManager().findFragmentById(R.id.fragment2); txt.change(androidos[position],"version : "+Version[position]); getlistview().setselector(android.r.color.holo_blue_dark); Creating Main layout Our main layout consists of two fragments which are displayed horizontally side by side. Each fragment should link to the fragment class which is defined. 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" tools:context="com.amal.fragments.mainactivity"> <fragment android:layout_height="match_parent" android:layout_width="240px" class="com.amal.fragments.menufragment" android:id="@+id/fragment"/> <fragment android:layout_width="240px" android:layout_height="match_parent" class="com.amal.fragments.textfragment" android:id="@+id/fragment2"/> </LinearLayout>
Creating Activity Our MainActivity just extends to the FragmentActivity. MainActivity.java package com.amal.fragments; import android.os.bundle; import android.support.v4.app.fragmentactivity; public class MainActivity extends FragmentActivity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); Creating Manifest We do not need any special permissions for our Project.
Screenshots