Android Development Tutorial 4

Android Development Tutorial 4Welcome to part 4 of my Android Development Tutorial. In the last part, I showed you how to put together an Android User Interface. This time I will add to that app in a big way!

Over the course of this tutorial and the next I will cover RadioButtons, RadioGroups, CheckBoxes, DropDows (Spinners), Buttons, Chronometers, ChangeListeners, ItemSelectedListeners, onClickListeners and much more. All of the code below should help you along.

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

Code From the Video

activity_crazy_tip_calc.xml

strings.xml

CrazyTipCalc.java

44 thoughts on “Android Development Tutorial 4”

  1. Hey Derek, if you can, could you please find a way to get rid of the line of numbers on the side of each line, or tell me an easy way to get rid of them if I copy them into eclipse. I have tried find and replace but that messes up the code and it is very difficult to fix it.

    1. If you drift your mouse over the upper right hand corner of the code 3 icons show up. Click on the one named view source. It will get rid of all the line numbers

  2. Absolutely fabulous tutorial.
    I imagine how hard Derek works to create a comprehensive lecture and not being drown into details that always obscure the real goal of any tutorial.
    For example the case with declaring a static final variable then assigning value to it later in the code seems against the java logic and it begs to be explained.
    However it would be deviation from the target – to learn how to program Android apps.
    Tech details can be clarified by docs.
    I would recommend to anybody do not copy/paste but write the code manually even change the names of variables to accelerate the learning process.

    1. I agree – these tutorials are wonderful. I have been doing what this comment suggests and manually building the apps, changing the variable names to suit my coding style, and looking back to Derek’s work as reference. I think it’s an amazing way to learn!
      I also find that adding a few new features forces me to make mistakes and then do research on how to fix my mistakes. On my version of this app, I added a “tip amount” that displays (and allows you to edit) the amount of the tip to write in on your credit card. Making these types of simple changes helps me to understand what I’m actually doing rather than just copying the code.
      Thanks again Derek for the stellar work!

  3. Derek, you are so awesome. The tutorials you make are like so good. You communicate, explain everything meanwhile creating an ENTIRE app. Unlike other you explain everything to create an actual app others explain Ie only listeners or other stuff. But you learn us how to use it in an app.

    I really appreciate your effort in this tutorials, I look over them, create a app and get smarter. I also have fun since I can see my result! Thank you so much. Keep on going!

    1. Thank you for taking the time to tell me that you liked the videos 🙂 I plan on making many more Android tutorials. The only problem with them is that I have to try and make good apps that are also easy to understand. I just gave up on another today, because I couldn’t figure out how to simplify it. I promise I’ll make many more.

      Derek

  4. Thanks for the tutorial, I have a problem, when I create the start, pause and reset button the text gets cut off how do I correct it and how do I change the font size

    1. As the tutorial continues I cover all of that. The font size is changed in the xml file with something like this android:ems=”10″

      Keep going and you’ll get it. I’m covering everything in extreme detail.

  5. can you help me..
    I do not know why in the graphical layout I don’t have the emulator for that I can not add the buttons and other things like you?!!

  6. I am just starting out though I have had many years in an education setting with Java. Eclipse is new to me… as is android anything. We always used a much simpler compiler so I just never had the opportunity to learn Eclipse. My issue is that in tutorial 3 for both bill and tip EditText I was unable to add in the Text or InputType via the properties box on the GUI. Those rows were grayed out for me and I was forced to add them in via code. Is this normal or was I doing something wrong?

    Loving your tutorials! Just amazing and so helpful. Exactly what I was looking for. Thanks so much for your time and effort!

    Pam

    1. Hi Pam,

      Developing for Android is most definitely different from straight Java, but once you get used to it it becomes second nature.

      I learned by digging through the API a few years ago. By doing that I’d say it took me about 2 months to get comfortable with writing Android apps. I hope these videos speed up that process.

      As per your error, what version of Eclipse are you using and what OS are you using? Eclipse Kepler can be a bit buggy unless you use Java 1.6 on some computers. I show how to fix that problem in my latest tutorial.

      I hope that helps
      Derek

      1. Derek,

        Thank you for your response. I am running win 7 64bit. Java is 7u45 and as for Eclipse I can only assume I am using Kepler. I just downloaded and started this Nov 10 and downloaded the latest versions offered. Looking at that tutorial you linked it looks as though I have to step back to 6u45 for the Java? Just did that. Will let you know if I have any more issues.

        I have to say that you have made it much easier to really get into the development. I have found myself working ahead slightly and then watching your vid to catch up and check myself. You putting out the files makes it much easier as well as I do not have to type it all out. The goal is to eventually make something (sneaky) for my son to get him to play and learn at the same time. We homeschool and he is so hyper that some days I can barely get him to stop spinning in circles much less do homework! (Will give you the shirt off his back as he chases a butterfly into the highway… =)) Your tutorials have really helped me feel that this is a possibility.

        Thank you so much.
        Pam

        1. Wow, I could never home school. I have no idea how you do it. My 4 and 1 year old are completely crazy. I’m very impressed by your skill as well as by your devotion 🙂

          I hope to eventually start creating videos and apps that work together to teach Math, Science, etc. They will all be free just like now. I have been moving towards that goal for a while. I hope it will provide the tools needed to give a free education to all. It is a very exciting goal for me.

          If you need any more help setting up Eclipse tell me. I think if you downgrade Java though that you’ll be ok.

          Derek

          1. Derek,

            I worked for about 5 years at the community college I received my associates. I wore a few different, but related, hats. TA (teaching assistant), tutor, and mentor. Subjects taught were computer science / math / language arts and college admission. I also worked as a tutor for the university I was attending for my (unfinished) BS – Computer Science. I had students come into the tutoring center (young* enough to be a grandparent of mine and teenagers having JUST graduated high school) that had difficulty adding and subtracting. Yes… these high schoolers couldn’t add yet had just graduated high school…. gotta love Texas education system. =( Wonder why I have chosen to home school???

            This just tore me up. It is discouraging to see all the difficulty these wonderful people had with (what I considered) simple math or writing. I decided then and there that one day I would come up with some program that was interactive, simple, and fun. One that would work for ages 3-103. Whatever it took, to develop a way for people to get the help they need for math. It is a dream of mine. I have searched high and low for the platform to do it… and well… the time.

            My kids are 9 and 11. After homeschooling them and everyday life it is difficult to find time. I can get done in one night what would take me a full week working with the code. If you are able to accomplish this before I can then heck yeah! I will scream it loud and often to these poor students. Anything to help them. I have seen some awesome, amazing people completely give up on their dreams because the concept of fractions stood in their way. I don’t have a ton of experience but I am a quick study and am a hard worker. If there is anything I can do to help YOU get to helping people education wise more quickly… just drop me an email and I will do what I can.

            Pam

            1. Thank you very much for sharing. Like you I’m pretty disgusted by the education system. I don’t blame teachers, but instead I blame the general system. The schools near my house are terrible as well.

              I’m not big on trying to solve problems through political channels so instead I do hope to make a ton of educational apps. I will probably work the rest of my life trying to make them better and better. I don’t think they’ll ever be perfect, but I think they’ll help some people and that is good enough for me.

              Hopefully along the way I can make this whole programming thing easier as well. That way more people will be able to join in. I haven’t by any means perfected this area of teaching yet, but I’m getting better each time. I plan on covering Android for quite some time. I will also eventually start over from the beginning and improve on this tutorial.

              Thank you for offering to help. It may sound strange, but just knowing that there are people out there that enjoy these videos is enough for me. Always feel free to tell me where I can improve. I need some negative comments as well 🙂

              Thanks
              Derek

              1. As you said, you wanted some negative comments to improve yourself. So there is some: You are speaking so quick and sometimes it is a little bit obscure mostly for no native speakers as me. It is hard to pay attantion to understanding speech and the content at the same time. But this is very subjective i think.

  7. Hi Derek, thank you very much for your videos. I have been so bored from webdeveloping and stuffs around web and this is a big opportunity for me to learn something new. The way you are teaching is more powerfull then reading book about android developing which i have. Realy good job.

    Sorry for my english, im not native speaker.

    1. Hi Martin,

      Thank you I’m doing my best to teach Android development in a new way. I’m enjoying these videos so much that I plan to make them for many more months.

      Your English is very good as well. Thanks for taking the time to say hi 🙂

  8. HI Derek,

    I have a one question. timeWaitingTextView is not being utilized or used anywhere in .java file. However, is that required to initialize it as we have done in statement
    timeWaitingTextView = (TextView)findViewById(R.id.timeWaitingTextView);

    Thanks in advance. I am really learning fast just because of your elaborative tutorials 🙂

  9. Derek you can’t believe how much I am thankfull for this tutorials you are the only one that actually explain everything and show your own apps
    Basiclly I want to say a giant thanks and I am going to publish my own app with all those stuff I learnt from this “course”

    1. Thank you 🙂 I’m very happy that you are enjoying them. I’ll keep making Android apps until anyone that wants to can learn to make most anything. They are great fun to make and quite profitable.

  10. Hi derek i really love your tutorials. just wanted to find out if there is any particular tutorials showing how an app communicates over the internet. yeah i know it sounds strange am actually working on a final year project please help

  11. I just want to say that this is by far the best Android tutorial series I’ve seen anywhere.
    You sir are really knowledgable and I appreciate very much you are taking the time to help the rest of us learn.
    Schools really became a business nowadays and don’t provide that good of an education (at least from my perspective as a student at UM).
    I’m hoping to start one of these educational websites myself eventually for my own country/language.
    Once again, thank you and big thumbs up 🙂

    1. Thank you for the very nice compliment 🙂 I always treated my website as if it was a type of trade school. Especially in the last 2 years I aimed to teach people exactly what they need to know to go off on their own. I wish I could better simulate in person teaching and I think I’ll get there some day.

  12. Amazing tutorials!!!
    Great flow…professional and friendly.
    Your tone of voice makes me feel calm, as if i can learn everything- no panic.
    thanks man.

  13. Hello Derek,

    your tutorials are very good and I understood a lot of things so far. And I really appreciate for that.

    But I have a problem with Java. If you compare it to C or similar programming languages, Java is so weird.
    There are so many identifiers and I didn’t figured out yet how I have to use them and so it is very complicated to follow all of your code.
    Do you know a good Java tutorial where I can learn it?

    Thank you in advance 😉

    1. and by the way:

      Where I can find the OnClickListeners for the three buttons?
      It is invoked but I can’t find the declaration.

  14. Good tutorial, you have bug in this code though
    when tip is updated with slidebar and than when you go back and enter new tip in text editor it does not work

    here is my code with a fix for it:

    package com.goxoft.crazytipcalc;

    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.ActionBar;
    import android.support.v4.app.Fragment;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.EditText;
    import android.widget.SeekBar;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    import android.os.Build;

    public class CrazyTipCalc extends Activity {

    private static final String TOTAL_BILL = “TOTAL_BILL”;
    private static final String CURRENT_TIP = “CURRENT_TIP”;
    private static final String BILL_WITHOUT_TIP = “BILL_WITHOUT_TIP”;

    private double billBeforeTip;
    private double tipAmount;
    private double finalBill;

    EditText billBeforeTipET;
    EditText tipAmountET;
    EditText finalBillET;

    SeekBar tipSeekBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crazy_tip_calc);

    if(savedInstanceState==null){
    billBeforeTip = 0.0;
    tipAmount = 0.15;
    finalBill = 0.0;
    } else {
    billBeforeTip = savedInstanceState.getDouble(BILL_WITHOUT_TIP);
    tipAmount = savedInstanceState.getDouble(CURRENT_TIP);
    finalBill = savedInstanceState.getDouble(TOTAL_BILL);
    }

    billBeforeTipET = (EditText) findViewById(R.id.billEditText);
    tipAmountET = (EditText) findViewById(R.id.tipEditText);
    finalBillET = (EditText) findViewById(R.id.finalBillEditText);

    tipSeekBar = (SeekBar) findViewById(R.id.changeTipSeekBar);

    billBeforeTipET.addTextChangedListener(billBeforeTipListener);

    /* tipEditText not updating bug
    * ********************************************************************
    */

    // this line here calls tipEditTextListener which checks if tip edit thext bar have changed
    tipAmountET.addTextChangedListener(tipEditTextListener);
    /***********************************************************************/

    tipSeekBar.setOnSeekBarChangeListener(tipSeekBarListener);

    }

    private TextWatcher billBeforeTipListener = new TextWatcher(){

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
    int count) {

    try {
    billBeforeTip = Double.parseDouble(s.toString());
    } catch (NumberFormatException e){
    billBeforeTip = 0.0;
    }

    updateTipAndFinalBill();
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
    };

    /* tipEditText not updating bug
    * ********************************************************************
    */
    private TextWatcher tipEditTextListener = new TextWatcher(){

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {
    // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
    int count) {

    // This guy updates a tip anytime it have changed */
    try {
    tipAmount = Double.parseDouble(s.toString());
    } catch (NumberFormatException e) {
    tipAmount = 2.0;
    }

    // Updates seek bar after tipEditText have been changed
    tipSeekBar.setProgress((int) (tipAmount*100));

    // call to our update method
    updateTipAndFinalBill();
    }

    @Override
    public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

    }

    };

    private OnSeekBarChangeListener tipSeekBarListener = new OnSeekBarChangeListener(){

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
    tipAmount = tipSeekBar.getProgress()*0.01;
    tipAmountET.setText(String.format(“%.2f”, tipAmount));

    updateTipAndFinalBill();
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

    }
    };

    private void updateTipAndFinalBill() {
    double tipAmmount = Double.parseDouble(tipAmountET.getText().toString());

    double finalBill = billBeforeTip + (billBeforeTip*tipAmmount);

    finalBillET.setText(String.format(“%.2f”, finalBill));
    }

    protected void onSaveInstanceState(Bundle outState){

    super.onSaveInstanceState(outState);

    outState.putDouble(BILL_WITHOUT_TIP, billBeforeTip);
    outState.putDouble(CURRENT_TIP, tipAmount);
    outState.putDouble(TOTAL_BILL, finalBill);
    }

    }

Leave a Reply

Your email address will not be published. Required fields are marked *