Godsplan23
Humor Engineer
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
Operating System
Android
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.
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.
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.
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
fragment_first.xml
fragment_second.xml
fragment_third.xml
fragment_fourth.xml
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.
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!!!
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
Android
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.
- <?
xml version=
"1.0"
encoding=
"utf-8"
?>
- <
RelativeLayout xmlns:
android=
"http://schemas.android.com/apk/res/android"
- xmlns:
app=
"http://schemas.android.com/apk/res-auto"
- xmlns:
tools=
"http://schemas.android.com/tools"
- android:
layout_width=
"match_parent"
- android:
layout_height=
"match_parent"
- android:
orientation=
"vertical"
- android:
weightSum=
"1"
- tools:
context=
"com.razormist.simplefragmentapplication.MainActivity"
>
- <
TextView
- android:
id=
"@+id/tv_title"
- android:
layout_height=
"wrap_content"
- android:
layout_width=
"wrap_content"
- android:
layout_alignParentTop=
"true"
- android:
text=
"Please Select A Color"
- android:
layout_marginTop=
"20dp"
- android:
layout_centerHorizontal=
"true"
- android:
textSize=
"20sp"
/>
- <
fragment
- android:
id=
"@+id/f_display"
- android:
layout_below=
"@+id/tv_title"
- android:
layout_width=
"match_parent"
- android:
layout_height=
"400dp"
- android:
name=
"layout.blankfragment"
- tools:
layout=
"@layout/fragment_blankfragment"
>
- </
fragment>
- <
Button
- android:
id=
"@+id/btn_first_fragment"
- android:
layout_width=
"wrap_content"
- android:
layout_height=
"wrap_content"
- android:
onClick=
"UpdateFragment"
- android:
text=
"Green"
- android:
layout_alignParentBottom=
"true"
- android:
layout_marginLeft=
"22dp"
/>
- <
Button
- android:
id=
"@+id/btn_second_fragment"
- android:
layout_width=
"wrap_content"
- android:
layout_toRightOf=
"@id/btn_first_fragment"
- android:
layout_height=
"wrap_content"
- android:
onClick=
"UpdateFragment"
- android:
layout_alignParentBottom=
"true"
- android:
text=
"Red"
/>
- <
Button
- android:
id=
"@+id/btn_third_fragment"
- android:
layout_width=
"wrap_content"
- android:
layout_alignParentBottom=
"true"
- android:
layout_toRightOf=
"@id/btn_second_fragment"
- android:
layout_height=
"wrap_content"
- android:
onClick=
"UpdateFragment"
- android:
text=
"Blue"
/>
- <
Button
- android:
id=
"@+id/btn_fourth_fragment"
- android:
layout_width=
"wrap_content"
- android:
layout_alignParentBottom=
"true"
- android:
layout_toRightOf=
"@id/btn_third_fragment"
- android:
layout_height=
"wrap_content"
- android:
onClick=
"UpdateFragment"
- android:
text=
"Pink"
/>
- </
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.
- <?
xml version=
"1.0"
encoding=
"utf-8"
?>
- <
manifest xmlns:
android=
"http://schemas.android.com/apk/res/android"
- package
=
"com.razormist.simplefragmentapplication"
>
- <
application
- android:
allowBackup=
"true"
- android:
icon=
"@mipmap/ic_launcher"
- android:
label=
"@string/app_name"
- android:
roundIcon=
"@mipmap/ic_launcher_round"
- android:
supportsRtl=
"true"
- android:
theme=
"@Style/AppTheme"
>
- <
activity android:
name=
".MainActivity"
- android:
configChanges=
"orientation"
- android:
screenOrientation=
"portrait"
>
- <
intent-
filter>
- <
action android:
name=
"android.intent.action.MAIN"
/>
- <
category android:
name=
"android.intent.category.LAUNCHER"
/>
- </
intent-
filter>
- </
activity>
- </
application>
- </
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.
- package
layout
;
- import
android.content.Context
;
- import
android.net.Uri
;
- import
android.os.Bundle
;
- import
android.support.v4.app.Fragment
;
- import
android.view.LayoutInflater
;
- import
android.view.View
;
- import
android.view.ViewGroup
;
- import
com.razormist.simplefragmentapplication.R
;
- public
class
FirstFragment extends
Fragment {
- @Override
- public
View
onCreateView(
LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState)
{
- // Inflate the layout for this fragment
- return
inflater.inflate
(
R.layout
.fragment_first
, container, false
)
;
- }
- }
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
- <
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=
"layout.blankfragment"
>
- <!--
TODO:
Update blank fragment layout -->
- </
FrameLayout>
fragment_first.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=
"layout.FirstFragment"
- android:
background=
"#00ff00"
>
- <!--
TODO:
Update blank fragment layout -->
- </
FrameLayout>
fragment_second.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=
"layout.SecondFragment"
- android:
background=
"#ff0000"
>
- <!--
TODO:
Update blank fragment layout -->
- </
FrameLayout>
fragment_third.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=
"layout.ThirdFragment"
- android:
background=
"#0000ff"
>
- <!--
TODO:
Update blank fragment layout -->
- </
FrameLayout>
fragment_fourth.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=
"layout.FourthFragment"
- android:
background=
"#ff00ff"
>
- <!--
TODO:
Update blank fragment layout -->
- <
TextView
- android:
layout_width=
"wrap_content"
- android:
layout_height=
"wrap_content"
- android:
text=
"Fourth Fragment"
- android:
layout_centerInParent=
"true"
- android:
textSize=
"30sp"
- android:
textColor=
"#ff00ff"
/>
- </
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.
- package
com.razormist.simplefragmentapplication
;
- import
android.support.v7.app.AppCompatActivity
;
- import
android.os.Bundle
;
- import
android.support.v4.app.Fragment
;
- import
android.support.v4.app.FragmentManager
;
- import
android.support.v4.app.FragmentTransaction
;
- import
android.view.View
;
- import
layout.FirstFragment
;
- import
layout.FourthFragment
;
- import
layout.SecondFragment
;
- import
layout.ThirdFragment
;
- public
class
MainActivity extends
AppCompatActivity {
- @Override
- protected
void
onCreate(
Bundle savedInstanceState)
{
- super
.onCreate
(
savedInstanceState)
;
- setContentView(
R.layout
.activity_main
)
;
- }
- public
void
UpdateFragment(
View
view)
{
- Fragment fragment;
- if
(
view ==
findViewById(
R.id
.btn_first_fragment
)
)
{
- fragment =
new
FirstFragment(
)
;
- FragmentManager fragmentManager =
getSupportFragmentManager(
)
;
- FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction
(
)
;
- fragmentTransaction.replace
(
R.id
.f_display
, fragment)
;
- fragmentTransaction.commit
(
)
;
- }
else
if
(
view ==
findViewById(
R.id
.btn_second_fragment
)
)
{
- fragment =
new
SecondFragment(
)
;
- FragmentManager fragmentManager =
getSupportFragmentManager(
)
;
- FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction
(
)
;
- fragmentTransaction.replace
(
R.id
.f_display
, fragment)
;
- fragmentTransaction.commit
(
)
;
- }
else
if
(
view ==
findViewById(
R.id
.btn_third_fragment
)
)
{
- fragment =
new
ThirdFragment(
)
;
- FragmentManager fragmentManager =
getSupportFragmentManager(
)
;
- FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction
(
)
;
- fragmentTransaction.replace
(
R.id
.f_display
, fragment)
;
- fragmentTransaction.commit
(
)
;
- }
else
if
(
view ==
findViewById(
R.id
.btn_fourth_fragment
)
)
{
- fragment =
new
FourthFragment(
)
;
- FragmentManager fragmentManager =
getSupportFragmentManager(
)
;
- FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction
(
)
;
- fragmentTransaction.replace
(
R.id
.f_display
, fragment)
;
- fragmentTransaction.commit
(
)
;
- }
- }
- }
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!!!
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
You must upgrade your account or reply in the thread to view hidden text.