Android - Simple Fragment Application

Submitted by: 
Operating System: 
Visitors have accessed this post 914 times.

In this tutorial we will try to create a Simple Fragment Application using Android. This simple app will open some new layout as a fragment to display it within the main layout. Android is an open source so that developer find it easy to develop and expand new features. Android is available to any devices such as TV, phones, watches etc. So let's now do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:orientation="vertical"
  11. android:weightSum="1"
  12. tools:context="com.razormist.simplefragmentapplication.MainActivity">
  13.  
  14. <TextView
  15. android:id="@+id/tv_title"
  16. android:layout_height="wrap_content"
  17. android:layout_width="wrap_content"
  18. android:layout_alignParentTop="true"
  19. android:text="Please Select A Color"
  20. android:layout_marginTop="20dp"
  21. android:layout_centerHorizontal="true"
  22. android:textSize="20sp"/>
  23.  
  24. <fragment
  25. android:id="@+id/f_display"
  26. android:layout_below="@+id/tv_title"
  27. android:layout_width="match_parent"
  28. android:layout_height="400dp"
  29. android:name="layout.blankfragment"
  30. tools:layout="@layout/fragment_blankfragment">
  31. </fragment>
  32.  
  33. android:id="@+id/btn_first_fragment"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:onClick="UpdateFragment"
  37. android:text="Green"
  38. android:layout_alignParentBottom="true"
  39. android:layout_marginLeft="22dp"/>
  40.  
  41. android:id="@+id/btn_second_fragment"
  42. android:layout_width="wrap_content"
  43. android:layout_toRightOf="@id/btn_first_fragment"
  44. android:layout_height="wrap_content"
  45. android:onClick="UpdateFragment"
  46. android:layout_alignParentBottom="true"
  47. android:text="Red" />
  48. android:id="@+id/btn_third_fragment"
  49. android:layout_width="wrap_content"
  50. android:layout_alignParentBottom="true"
  51. android:layout_toRightOf="@id/btn_second_fragment"
  52. android:layout_height="wrap_content"
  53. android:onClick="UpdateFragment"
  54. android:text="Blue" />
  55. android:id="@+id/btn_fourth_fragment"
  56. android:layout_width="wrap_content"
  57. android:layout_alignParentBottom="true"
  58. android:layout_toRightOf="@id/btn_third_fragment"
  59. android:layout_height="wrap_content"
  60. android:onClick="UpdateFragment"
  61. android:text="Pink" />
  62.  
  63. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simplefragmentapplication">
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>

Fragments

Fragment represented as a behavior to utilize the event on a layout You can think of a fragment as a modular section of an activity, which have receiver and sender of data information. To create a fragment, go to layout then right click select New then Fragment lastly Fragment(Blank). A new layout and java will be created at the same time.

Create a Five Fragments named them as follow: FirstFragment, SecondFragment, ThirdFragment, FourthFragment, and last blankfragment. After that open the java file of one of the fragment then delete some unnecessary block of codes, and leave only some codes as shown below.

  1. package layout;
  2.  
  3. import android.content.Context;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.support.v4.app.Fragment;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10.  
  11. import com.razormist.simplefragmentapplication.R;
  12.  
  13.  
  14. public class FirstFragment extends Fragment {
  15.  
  16. @Override
  17. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  18. Bundle savedInstanceState) {
  19. // Inflate the layout for this fragment
  20. return inflater.inflate(R.layout.fragment_first, container, false);
  21. }
  22. }

Do these to the rest of the fragment java file.

Now were done with the Fragment Java File let's now go to the layout of each individual fragment. Write these corresponding block of codes inside the xml file as shown below.

fragment_blankfragment.xml

  1. <FrameLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  2. " rel="nofollow">http://schemas.android.com/apk/res/android"
  3. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  4. " rel="nofollow">http://schemas.android.com/tools"
  5. </a> android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="layout.blankfragment">
  8.  
  9. <!-- TODO: Update blank fragment layout -->
  10.  
  11. </FrameLayout>

fragment_first.xml

  1. <FrameLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  2. " rel="nofollow">http://schemas.android.com/apk/res/android"
  3. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  4. " rel="nofollow">http://schemas.android.com/tools"
  5. </a> android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="layout.FirstFragment"
  8. android:background="#00ff00">
  9.  
  10. <!-- TODO: Update blank fragment layout -->
  11.  
  12. </FrameLayout>

fragment_second.xml

  1. <FrameLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  2. " rel="nofollow">http://schemas.android.com/apk/res/android"
  3. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  4. " rel="nofollow">http://schemas.android.com/tools"
  5. </a> android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="layout.SecondFragment"
  8. android:background="#ff0000">
  9.  
  10. <!-- TODO: Update blank fragment layout -->
  11.  
  12.  
  13. </FrameLayout>

fragment_third.xml

  1. <FrameLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  2. " rel="nofollow">http://schemas.android.com/apk/res/android"
  3. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  4. " rel="nofollow">http://schemas.android.com/tools"
  5. </a> android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="layout.ThirdFragment"
  8. android:background="#0000ff">
  9.  
  10. <!-- TODO: Update blank fragment layout -->
  11.  
  12. </FrameLayout>

fragment_fourth.xml

  1. <FrameLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  2. " rel="nofollow">http://schemas.android.com/apk/res/android"
  3. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  4. " rel="nofollow">http://schemas.android.com/tools"
  5. </a> android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="layout.FourthFragment"
  8. android:background="#ff00ff">
  9.  
  10. <!-- TODO: Update blank fragment layout -->
  11. <TextView
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="Fourth Fragment"
  15. android:layout_centerInParent="true"
  16. android:textSize="30sp"
  17. android:textColor="#ff00ff"/>
  18.  
  19. </FrameLayout>

The Main Function

This code contains the main function of the application. This code will render the Fragment data when a button is clicked. To start with first locate your MainActivity java file and open it, then write these block of codes inside the MainActivity class.

  1. package com.razormist.simplefragmentapplication;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.support.v4.app.Fragment;
  6. import android.support.v4.app.FragmentManager;
  7. import android.support.v4.app.FragmentTransaction;
  8. import android.view.View;
  9.  
  10. import layout.FirstFragment;
  11. import layout.FourthFragment;
  12. import layout.SecondFragment;
  13. import layout.ThirdFragment;
  14.  
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. }
  23.  
  24. public void UpdateFragment(View view){
  25. Fragment fragment;
  26.  
  27.  
  28. if(view == findViewById(R.id.btn_first_fragment)){
  29. fragment = new FirstFragment();
  30. FragmentManager fragmentManager = getSupportFragmentManager();
  31. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  32. fragmentTransaction.replace(R.id.f_display, fragment);
  33. fragmentTransaction.commit();
  34. }else if(view == findViewById(R.id.btn_second_fragment)){
  35. fragment = new SecondFragment();
  36. FragmentManager fragmentManager = getSupportFragmentManager();
  37. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  38. fragmentTransaction.replace(R.id.f_display, fragment);
  39. fragmentTransaction.commit();
  40. }else if(view == findViewById(R.id.btn_third_fragment)){
  41. fragment = new ThirdFragment();
  42. FragmentManager fragmentManager = getSupportFragmentManager();
  43. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  44. fragmentTransaction.replace(R.id.f_display, fragment);
  45. fragmentTransaction.commit();
  46. }else if(view == findViewById(R.id.btn_fourth_fragment)){
  47. fragment = new FourthFragment();
  48. FragmentManager fragmentManager = getSupportFragmentManager();
  49. FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  50. fragmentTransaction.replace(R.id.f_display, fragment);
  51. fragmentTransaction.commit();
  52. }
  53. }
  54. }

Try to run the app and see if it worked.

There you have it we create a Simple Fragment Application using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.