android fragment example


android fragment example


In this article i am going to show you the android fragment example with full source code. Fragment class in Android is used to build dynamic User Interfaces. Fragments should be used within the Activity. A greatest advantage of fragments is that it simplifies the task of creating UI for multiple screen sizes. A activity can contain any number of fragments. In this tutorial we are going to use fragments in our Android application.
Android working with Fragments

Creating Project

Create a new Android Application project in Android Studio with package as com.amal.fragments. Create the main layout asactivity_main and main Activity as MainActivity.

Download Complete Project

android fragment example

By the help of below link you can download the android fragment example

android fragment example
Android Fragments
AndroidFragments.zip
Version: 1.0
1.6 MiB
229138 Downloads
DETAILS

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:layout_height="wrap_content"
        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:layout_height="wrap_content"
        android:textSize="40px"
        android:textColor="#ffffff"
        android:layout_gravity="center"
        android:id="@+id/AndroidOs"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="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;
    @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.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"};
    @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, AndroidOS);
        setListAdapter(adapter);
        return view;
    }
    @Override
    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 {
    @Override
    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. android fragment example

android fragment example Screenshots

android fragment exampleandroid fragment example

android fragment example
android fragment example

Any questions comment here android fragment example

1 comments

  1. Borgata Hotel Casino & Spa Opening Night - JTHub
    The Borgata Hotel Casino & Spa is 광양 출장안마 set to reopen on Sunday, Dec. 동해 출장샵 15, 거제 출장마사지 and will 문경 출장안마 be 경주 출장샵 open 24 hours.

    ReplyDelete

My Instagram