www.androidchunk.com

Hi Developer, Please visit www.androidchunk.com for Latest Updates. Thank you!
Showing posts with label Fragments Example For Beginners. Show all posts
Showing posts with label Fragments Example For Beginners. Show all posts

Simple Android Fragments Example In Android Studio

Category: Android Codez | Last Updates: 31/07/2017

Hey android developer, In this part We will know about Simple Android Fragment.
Let's see few points about Android Fragments:

Fragment
-has its own Layout
-has its own Behavior
-has its life cycle
-can be used in single as well as multiple activities
-used as part of the Activity

Now we are going to start creating simple android fragments.

Create New project: 
Create New project with Empty Activity in Android Studio.

Android Studio>>Empty Activity>>Finish

Create Fragments:
To create new blank fragment, We have made two simple fragments.
Android Studio>File>New>Fragment>Fragment (Blank)

res/layout/fragment_my_first.xml

src/MyFirstFragment.java
res/layout/fragment_my_second.xml

src/MySecondFragment.java

Define Fragment container:
We have to define a container in main activity's layout file. This container will hold our fragment. For setting (Adding/Removing) fragments, We have taken two buttons.

res/layout/activity_main.xml


Add/Remove Fragment in Activity:
By clicking the button from the MainActivity, We will set the fragment to fragment container.

src/MainActivity.java

res/values/strings.xml


Output: