Fragments


 

  1. Fragments Basic
  2. Fragments LifeCycle
  3. Fragments Transactions
  4. Fragments MultiPane
  5. Call a Fragment from Fragment

 

1. Fragments Basic

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 : Open res -> layout -> Create new xml file, fragment_one.xml and add following code :

Step 4 : Open src -> package -> Create new class, FragmentOne.java and add following code :

Step 5 : Open res -> layout -> Create new xml file, fragment_two.xml and add following code :

Step 6 : Open src -> package -> Create new class, FragmentTwo.java and add following code :

Step 7 : Open src -> package -> MainActivity.java and add following code :

Step 8 : Open AndroidManifest.xml and add following code :

ScreenShot:

frags

Download SourceCode

 

2. Fragments LifeCycle

Step 1 :Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 : Open res -> layout -> Create new xml file, fragment_one.xml and add following code :

Step 4 : Open src -> package -> Create new class, FragmentOne.java and add following code :

Step 5 : Open src -> package -> MainActivity.java and add following code :

Step 6 :Open AndroidManifest.xml and add following code :

ScreenShot:

frag_life1 frag_life2 frag_life3 frag_life4

Download SourceCode

 

3. Fragments Transactions

Step 1 :Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 :Open res -> layout -> Create new xml file, fragment_one.xml and add following code :

Step 4 : Open src -> package -> Create new class, FragmentOne.java and add following code :

Step 5 :Open res -> layout -> Create new xml file, fragment_two.xml and add following code :

Step 6 : Open src -> package -> Create new class, FragmentTwo.java and add following code :

Step 7 : Open src -> package -> MainActivity.java and add following code :

Step 8 : Open AndroidManifest.xml and add following code :

ScreenShot:

frag_tran frag_tran2 frag_tran3 frag_tran4 frag_tran5

Download SourceCode

 

4. Fragments MultiPane

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 : Open res -> Create new folder, layout-land and Create new xml file, activity_main.xml and add following code :

Step 4 : Open res -> layout -> Create new xml file, fragment_main_panel.xml and add following code :

Step 5 : Open src -> package -> Create new class, MainPanelFragment.java and add following code :

Step 6 : Open res -> layout -> Create new xml file, fragment_side_panel.xml and add following code :

Step 7 : Open src -> package -> Create new class, SidePanelFragment.java and add following code :

Step 8 : Open src -> package -> MainActivity.java and add following code :

Step 9 : Open AndroidManifest.xml and add following code :

ScreenShot:

frag_multi frag_multi2

Download SourceCode

 

5. Call a Fragment from Fragment

Step 1 : Select File -> New -> Project -> Android Application Project. Fill the forms, create “Blank Activity” and click “Finish” button.

Step 2 : Open res -> layout -> activity_main.xml and add following code :

Step 3 : Open res -> layout -> Create new xml, fragment_one.xml and add following code :

Step 4 : Open res -> layout -> Create new xml, fragment_two.xml and add following code :

Step 5 : Open src -> package -> MainActivity.java and add following code :

Step 6 : Open src -> package -> Create new class, FragmentOne.java and add following code :

Step 7 : Open src -> package -> Create new class, FragmentTwo.java and add following code :

Step 8 : Open AndroidManifest.xml and add following code :

Step 9 : Open build.gradle(Module: app) and add following code :

ScreenShot:

 call fragment from fragment call fragment from fragment2

Download SourceCode