Welcome 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
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">CensusApp</string> <string name="action_settings">Settings</string> <string name="contact_name_hint">Enter Name</string> <string name="contact_phone_number_hint">Enter Phone Number</string> </resources>
activity_census_app.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragmentContainer" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- This will be the container for our fragments --> </FrameLayout>
fragment_contact.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/contactNameEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="@string/contact_name_hint" android:inputType="textPersonName" > <requestFocus /> </EditText> </LinearLayout> <!-- 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 -->
Contact.java
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) { this.name = name; } }
ContactFragment.java
package com.newthinktank.censusapp; import android.os.Bundle; import android.support.v4.app.Fragment; 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. @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); contact = new Contact(); } // Used to inflate the Fragment, or show it on the screen @Override 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(R.id.contactNameEditText); // If text in the EditText box is edited it will change the // name. contactNameEditText.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } @Override 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 @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { contact.setName(arg0.toString()); } }); // 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. return theView; } }
CensusApp.java
package com.newthinktank.censusapp; //We will use the android.support.v4.app.Fragment //support library so our app runs on older versions //of Android import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; //Change Activity to FragmentActivity // The FragmentManager ads Fragments to an Activity's view public class CensusApp extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_census_app); FragmentManager fragManager = getSupportFragmentManager(); // Check if the FragmentManager knows about the Fragment // id we refer to Fragment theFragment = fragManager.findFragmentById(R.id.fragmentContainer); // 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. fragManager.beginTransaction() .add(R.id.fragmentContainer, theFragment) .commit(); } } }
Could you provide a PNG file?
What do you want a PNG of?
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
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 MainActivity.java
I hope that helps 🙂
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!
Nvm, I figured it out!
I’m glad you got it. Sorry I couldn’t get to you quicker.
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!
Have you seen this http://scriptasylum.com/tutorials/infix_postfix/algorithms/infix-postfix/
This is a very clear algorithm for the converter and is very helpful! However, while writing my actual code, I have got a problem about my own stack (can’t use built-in stack in java). The operator that is scanned from a String (i.e, ‘+’ in the String ‘a+b’) has a type of ‘char’, how do I put it into my stack? I’m kinda a beginner here, sorry about any of the incorrect terms. Thanks for your help!
Nvm, I just found out all I have to do is type “Character” instead of “char” in the . Thanks for your help again!
I guess my next question would be how to create a proper stack for this specific converter?
Nvm, figured it out! But I have a new problem now. How do I add a “pop-out” item from the stack to my postfix String? I typed “postfix += stack.pop();” in my code, but it didn’t add the “pop-out” item to the postfix string. Thanks!
Nvm, figured it out! Thanks!
Sorry I couldn’t get to you quicker, but I’m glad you figured it out!
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
http://screencast.com/t/MedK4C30crvW
Please help to solve this Error
Waiting For your reply
Thanking you
Manish
Hi Manish,
I covered how to Install Eclipse for Java here. I then show how to Install Android Development tools.
I hope they help 🙂
hi derek, i need a help. please upload a tutorial on email template setup and usage if possible. thanks
Hi, The easiest thing you could do is to use MailChimps free tools. I made a tutorial about them Create Email Templates. I hope that helps 🙂
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.
Thank you 🙂 It is now best to skip the install videos at the beginning and instead watch my new ADT installation video.
Then decide if you want to start off making Android apps in an easier, but some what limiting way with my App Inventor video tutorial.
If you want to make complex Android apps right now you’ll need to know Java first. Here is a Java tutorial (Parts 1 – 18, minus 8 and 10 is enough). Then you’ll need the Android video tutorial.
i hope that helps 🙂
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…
You’re very welcome 🙂 I’m very happy that you are finding my site useful
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!)
They will definitely teach a lot. I still need to do more tutorials on Android and I will over the rest of this year.
Hello Derek
Wonderful and very useful tutorials.
Can you provide links to download the source code? for this series?
Thanks
Thank you 🙂 Here is the whole package for this app. I hope that helps
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.
Thank you I’ll see what I can do.
Hi Mr. Banas, have you done an android tutorial on Widgets, Alarms, Geo Location,
Great we need more people to do tutorials 🙂