Android Development 18

Android Fragment TutorialWelcome to part 18 of my Android Development Tutorial. Over the course of the next few tutorials I will focus on a bunch of new topics as well as provide a review of topics people are struggling with.

This tutorial covers how to create custom icons for our app. We then build an app based around the use of fragments. We will be building an app that a Census taker would use to gather information on people. The tutorial starts slow, but it will build quickly. All of the code can be found below.

If you like videos like this it helps to tell Google+ with a click here [googleplusone]

Code from the Video



    <string name="app_name">CensusApp</string>
    <string name="contact_name_hint">Enter Name</string>
    <string name="contact_phone_number_hint">Enter Phone Number</string>



<FrameLayout xmlns:android=""
    android:layout_height="match_parent" >
    <!-- This will be the container for our fragments -->



<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:inputType="textPersonName" >

        <requestFocus />


A Fragment is sort of a mini Activity which you can add
or remove from another Activity.
You can use them to create reusable pieces you can add
to your interface.

They are often used so that depending on your screen size
you can add more or less fragments 

package com.newthinktank.censusapp;

public class Contact {
	private String name;
	private String phoneNumber;
	public Contact(){

	public String getphoneNumber() {
		return phoneNumber;
	public void setphoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;

	public String getName() {
		return name;

	public void setName(String name) { = name;


package com.newthinktank.censusapp;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class ContactFragment extends Fragment {
	private Contact contact;
	private EditText contactNameEditText;

	// Generate this with Right Click - Source - Override/Implement methods
	// This method is called when the Fragment is called for.
	// We initialize everything here.
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		contact = new Contact();

	// Used to inflate the Fragment, or show it on the screen
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Pass in the layout to inflate, the views parent and whether
		// to add the inflated view to the parent.
		// We mark this false because the Activity will add the view.
		View theView = inflater.inflate(R.layout.fragment_contact, container, false);
		// Get a reference to the EditText
		contactNameEditText = (EditText) theView.findViewById(;
		// If text in the EditText box is edited it will change the
		// name.
		contactNameEditText.addTextChangedListener(new TextWatcher() {

			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			// When text is changed the contact object is updated
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
		// to add the inflated view to the parent.
		return theView;


package com.newthinktank.censusapp;

//We will use the
//support library so our app runs on older versions
//of Android

import android.os.Bundle;

//Change Activity to FragmentActivity

// The FragmentManager ads Fragments to an Activity's view

public class CensusApp extends FragmentActivity {

	protected void onCreate(Bundle savedInstanceState) {
		FragmentManager fragManager = getSupportFragmentManager();
		// Check if the FragmentManager knows about the Fragment 
		// id we refer to
		Fragment theFragment = fragManager.findFragmentById(;
		// Check if the Fragment was found
		if(theFragment == null){
			// If the Fragment wasn't found then we must create it
			theFragment = new ContactFragment();
			// Creates and commits the Fragment transaction
			// Fragment transactions add, attach, detach, replace
			// and remove Fragments.
			// add() gets the location to place the Fragment into and
			// the Fragment itself.
				.add(, theFragment)


31 Responses to “Android Development 18”

  1. Eric Kim says:

    Could you provide a PNG file?

  2. Rex David says:

    Hey Derek!

    Your vidoes are just awsome! after watching them, I’m actually thinking of taking up Android Development as a full time job.

    Got a question- Is there any way I can hard code a Json/Rest reponse in an android project. The reason I want to do this is because the Webservices are not ready yet and I want to make a Demo app which uses values from a Json object returned on a GET Http response. So I was thinking, like to create a class which returns Json Object with bunch of mock values. Is it possible? Anyway you can show a simple example of it??

    Again, Thanks a Lot for these amazing video posts. Take care!

    Rex David

    • Derek Banas says:

      Thank you 🙂 I’m glad you are enjoying them. Android is awesome!

      You can do what you need to do quite easily. You just need to get some json and in the MainActivity where the StringBuilder gets the data just save you JSON in the StringBuilder. I’m referring to line 88 in

      I hope that helps 🙂

  3. Jack Zhang says:

    Sorry to bother you, but my question is irrelevant to this video. I’m wondering if you can show me how to move an object in a doubly linked list to the front/end. Thanks!

  4. Jack Zhang says:

    Hi, again my question is irrelevant to this video. I’m wondering if you can show me how to write a class for converting infix to postfix. Thanks!

  5. Manish Jain says:

    Respected Derek,

    Your Videos are Awesome Excellent! I am having Problem with the installation of Eclipse. I am getting the following Error while starting a New project. Can you please help me out

    Your tools installation appears to be out of date (or not yet installed)

    The wizard depends on templates distributed with the android sdk tools

    Here is the image for the Error

    Please help to solve this Error

    Waiting For your reply

    Thanking you


  6. arif says:

    hi derek, i need a help. please upload a tutorial on email template setup and usage if possible. thanks

  7. Sanjay says:

    Hey Derek, I’ve been searching for Android tutorials from scratch(basic) to professional on Youtube and found out your channel as the BEST. Can you please help me from which video i should start with and which to end.

    (I know this sounds stupid)
    Thanks,Waiting for your reply.

  8. Nilson says:

    Hi, Derek…
    I’m brazilian and I found yours site digging on the internet so I just wanna say: THANK YOU! You helped me a lot on a school project keep going man I assign your channel on Youtube now I have to see the others videos and learn more about Android…

    PS: Sorry my typping english it is not my main language…

  9. Sanjay says:

    So, you mean to say the Android videos(1-26) with your Java tutorial is more than enough?

    (I really appreciate your time for me,thanks a lot Derek Sir!)

  10. YZ says:

    Hello Derek
    Wonderful and very useful tutorials.

    Can you provide links to download the source code? for this series?


  11. Jaami Adam says:

    These videos are base of my app i am trying to complete. one thing i would like to request is that before you start writing code in videos if you can explain how many files will be in app and what purpose they will server. a slide that explain that will ge good enough for me to easily understand how many files i need in my app and for what purpose. It is already explained but really a slide explaining that will be great help. Thank you helping us to learn android.

  12. Tom says:

    Hi Mr. Banas, have you done an android tutorial on Widgets, Alarms, Geo Location,

Leave a Reply to Jack Zhang Cancel reply

Your email address will not be published.