Next Tutorial
Coding a Share Intent For Text
Prev Page
View All Tutorials
Coding a Tabbed Fragment
If you are just arriving at this page; and you are not familiar with creating Android Apps, we suggest you begin with our tutorials:
Android Tutorials i and
How To Create a Simple App in Android
From there follow along with each tutorial lesson.
COPY/PASTE CODE
First, create a java/xml android template app using AIDE, (or Android Studio). Then you can get the code for this app from the EXAMPLE CODE section on this page.
Replace the code on the pages you need to(with our tutorial code); in order to create this app.
Sometimes, new pages have to be created.
All the other coded pages in your app
can remain as they are; they don't need to be changed.
This is the App - Fragment Tabs
It has three fragments, and three tabs; one fragment view in each tab.
Where - How To Use Fragments
Fragments can be added to your layout activity. If you only have one activity,(one app page), you can still add as many fragments as you want to it.
For this app; we created a tabbed layout design using fragments.
We added three fragments which give us three distinct views in the app. We added three tabs also. Each fragment is added to each tab. To browse each view, you just goto each tab.
You can add whatever you like to each fragment view; text,photos, buttons; design the layout as you like. With fragments, you can design a modulized or multi paneled layout.
Adding Tabbed Fragments
Each fragment you add to your app must be defined in your layout file(for this app, is main.xml file).
Fragment Container
Because we are adding three fragments; and we want to browse them using tabs; we need a container to hold the fragments. The 'Frame Layout' is used for this purpose.
So instead of defining the 3 individual fragments in our layout file(main.xml); instead we only have to define the FrameLayout element; which will be our fragment_container; as
shown in this code.
main.xml
And, we gave it an android id as such:
an android:id: fragment_container. This android id is then coded in another page; the TabListener.java file, as such:
'R.layout.fragment_container'. By doing so, the
android system attaches these two pages, and then loads and displays the fragments into the view. The TabListener.java file has the code that allows us to click on each tab and see the fragment view.
Creating - Naming Your App Using AIDE
When creating your app,(from left menu) choose:
Create New Project;
Then, choose
New Android App(gradle, android Java/xml)
or
Hello World App Java/xml
if your using the original version of AIDE
Name your app:
Fragment Tabs for the App Name and,
for the package name(next line) use:
com.aac.fragmenttabexample.
Where Code is Coded - The Coded Pages
Code for the app - Fragment Tabs
For this app you will use these coded pages:
1 layout file, main.xml, located at res/layout/.
3 fragments, you must create 3 new xml pages, one for each fragment; and put them at res/layout/fragment1.xml; and res/layout/fragmenttab2.xml, res/layout/fragmenttab3.xml
Each fragment xml page created requires a fragment java file also. Xml pages and java pages are used together. This is true for coding fragment classes or coding activities.
The java class file is where we define the location of the fragment xml file.
The android system needs this in order to display the fragment layout(view) on the screen.
So, for fragmenttab2.xml,(shown here in photo), we also created FragmentTab2.java. Looking at the FragmentTab2.java
file, you see the fragmenttab2.xml file is declared as the layout at: R.layout.fragmenttab2
We require 3 fragment java pages, you must create 3 new java pages; one for each fragment; these files are where you define the
'R.layout' for each.fragment xml file created; like
shown in the photo.
Put the these java files at: src/java/FragmentTab1.java and src/java/FragmentTab2.java, src/java/FragmentTab3.java
1 Java class
MainActivity.java file;
The MainActivity.java file, is where we define everything about the tabbed fragments; the tabs, declare the tab variables(their names),their titles, drawables for the tabs if your adding any; and defines the TabListener code.
1 Java class file;
TabListener.java; you must create this page, and add it to src/java/TabListener.java. This coded page controls the clicks on the tabs.
1
AndroidManifest.xml file;
1
strings.xml, page where you define the app name - Fragment Tabs, located at res/values/strings.xml. Also, we added text to each
fragment view; and this text is added to our strings.xml file also as: strings - Fragment1, Fragment2, Fragment3.
All these pages are created for us as we used the 'template' app to create our app. To update the code for each page just
copy and paste from our Example Code section this page. Some new pages, as mentioned, have to be created.
When creating new
xml pages, goto folder
res/layout, and select 'create new xml file'; and for creating the new
java class pages , goto folder
java; and select 'create new class'. Java class file names are camel cased; so use, MyNewPage.java; and not, mynewpage.java. Xml page file names are lower case.
FYI:
The manifest xml page, where we define our app's activity java page(and other things); however, since we are not adding any new activities to our app; we don't need to define any new java pages in the
manifest file; so we can leave it as it is.
As for the fragments we are adding; fragments do not need to be defined in the manifest.
Since this app has no images; we don't have to add any to our drawable-hdpi folder.
The folder src/res/drawable-hdpi also has our ic_launcher.png image; but we don't need to add it because it was
added automatically when we created our android template app.