jay's old blog

this blog will be deleted soon - please visit my new blog - https://thesanguinetechtrainer.com

android training – Episode 6 – input and output

Hello and welcome back. May name is Jay, and I am your trainer for today. We are going to assume that you already went through Episode 1 to 5.

We are jumping into development now. That means, the page will be heavy with screenshots. I have tried my best to use low sized images so that you can load this even on a 2G connection. If you are having issues loading any of our tutorial pages, contact us. You will find the contact information, at the end of the blog.

You can download the code for this project, from GitHub, right here – https://github.com/Jay-study-nildana/sn_tr_inputoutput

If you don’t know about GitHub, check out our blog post here – http://blog.studynildana.com/post/study-nildana-and-github

Topics Covered In This Episode

In this episode, we will look at doing the following.

  • Using ‘EditText’ widget to collect input
  • Using ‘TextView’ to show output - This has already been taken care of in an earlier episode and hence will focus more on EditText this time.
  • Using a simple ‘Button’ widget. This is new to you, so we will spend additional time on this. 

As usual, we will not just focus on using these widgets. In this input and output project/tutorial, I will build a simple app that accepts the name as an input, and then says “Hey, (entered name)! Welcome to training at study nildana”, when the button is pressed.

The idea is, if you can understand basic input and output like this, you can probably start doing more things. To take our usual example app, alert nildana, we are generating a secret key based on the information entered by the user. In your app, you could collect some other type of input and show some output based on the app design you are aiming for.

Further, we also spend some time on using a button. A lot of times, you want to trigger an action based on user action. If you remember, any app you use, you are almost always getting things by tapping on something. For instance, you enter the username/password and then press the ‘login’ button. You select a number of photos and then hit ‘send’ to send it to someone else. That means, you are always using one or the other kind of button all the time.

Taking Care of Design UI

As your project starts, your project should look something like this. As with the previous episodes, I have made the app full screen and removed the menu on the top right corner. Use my earlier episodes to see how you can do this.




There are three parts in the above image.

  • Two circles with the heading ‘1’. That is where I am dragging an ‘Edit Text’ widget from the palette on the left and putting it on our design view. This is the same procedure you used in the previous episodes to add TextView widgets. As with TextView, we have a special post on EditText as well. Check out that blog right here –
  • The circle with the heading ‘2’ is where you can see the same EditText we just added. You will notice a small exclamation symbol. That is because we have not set a ‘input type’. Check out our specific blog on TextView to find out what this is all about.
  • The circle with the heading ‘3’ is where you will find properties. This is where I can set the properties, and right now all I want to do is give this widget a proper name, also called as ‘id’. As with variable and function names, we should be using meaningful names for the id too. If you are not sure why or what is the importance of meaningful names, check out our blog on that topic, right here –
After setting the input type (to text) and the id (to editText_input_user), the same picture will look something like this.





Next, I will need a TextView, to display the output. The TextView has already been dealt with in previous episodes, but you can always refresh yourself by visiting this link about TextView –

Here is the picture after adding TextView. You will notice that Android Studio is flagging me, or warning me about something. I have marked it with a circle.




The error is about “hardcoded strings”. Hardcoded strings are string values (English words, sentences or anything else you type in double quotes i.e. string constants) that are directly typed into the widget. As a rule, more like programmer good habits, it is better to use string resources that are stored in “strings.xml” file. Find out more about string resources and the strings.xml right here, at this blog –

I have gone ahead and added the string resource, and that error is now gone. I have added a new string resource called ‘input_user’ and to that resource, I inserted a value called ‘output will come here’.




So, the input and the output box is here, it is time to include a button. As usual, we have a specific focus blog post on ‘Button’ widget that you can find here –




And, I set the button a title (in the text property) and set an id. Here is the picture to reference that.




At this point, we have the three widgets that we need to get going. The EditText to collect the input. The TextView to display the output and lastly, the button to make the words appear. To make this happen, we need to move to the code file or the “.java” file of the activity we are working on.

Taking Care Of Code

I really hope you have downloaded the code from GitHub, or you are at least looking at the code directly on the website. Follow the comments that populate the code. Otherwise, you won’t understand what is happening.

Now, I need a function that will “listen” to button taps in the entire activity. For that, I type something like this picture, and android studio will prompt me automatically if I wish to create this function.





Once I let it create the method, the code snippet should look something like this. Feel free to copy this code to your project, if you are having difficult or run into errors.


    private void button_listener()
    {

    }//end of button_listener

Now, in this button listener, I want to do the following.

  • Connect the button widget (in the UI) with a button object in the code.
  • Get the button object, and connect that to an onclick listener.
  • Then, inside this listener function, collect input (from EditText) and then display output (on TextView)

Here is where I am creating a Button object.

    //creating the Button object

    Button button_for_hey_there;

When you try to add the above, Android Studio will prompt you to automatically include a library that is needed for this code to work.




You will see prompts like this whenever you include a code that needs some missing library. When you see this, as the prompt suggests, use “Alt and Enter” and Android Studio will do the rest for you. Of course, you don’t do this, you will run into errors. So, don’t skip the “Alt and Enter” step.

Now, connecting this button object to the button UI widget

        //connecting the button object to the button in the UI page
        button_for_hey_there = (Button) findViewById(R.id.button_hey_there);

Now creating the listener. Do remember that the listener will be automatically created for you or you can copy paste the above code.

Finally, you should have something like this, the full code.

        //creating the onclick listener
        button_for_hey_there.setOnClickListener(new View.OnClickListener()
        {
            //this is the OnClick function where we need to put all the things that are supposed to happen when the user taps the button

            @Override
            public void onClick(View view)
            {

            }
        });

Do you see that onClick code?




Whatever code you put inside that function, will be executed when the button “button_hey_there” is tapped by the user.

In this code, I am going to connect the EditText object to the EditText UI element in the UI page.

                //connecting the editText object with the editText widget in the UI
                //this will be the persons name
                EditText editText_collect_user_input = (EditText) findViewById(R.id.editText_input_user);

Now, I am going to collect whatever the user has entered in that widget.

                //collecting the string that the user has entered
                String temp_collected_name = editText_collect_user_input.getText().toString();

Now, I am going to create an output string.

                //creating an output string
                //I want an output something like - "Hey, Jay! Welcome to training at study nildana
                //for those who are new to Java, '+" here acts like a concatenation operator, adding up strings together
                String output_string = "Hey, " + temp_collected_name + "! Welcome to training at study nildana";

Now, connecting the TextView object to the TextView UI in the UI page.

                //connecting the textView object with the textView
                TextView textView_output = (TextView) findViewById(R.id.textView);

Finally, I am going to ‘set’ the output string to the TextView on the UI page, through the TextView object.

                //setting the output string to the textView_output object
                textView_output.setText(output_string);

Here is the final code, of the entire file. If you put this code into your java file, it should do what we plan to do with this lesson.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
{
    //creating the Button object

    Button button_for_hey_there;

    //the OnCreate function is the first function to be called when the current activity starts
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //to make the button work, we need a function called that will "listen" to buttons being clicked.
        //you can call this function anything you want
        //I am calling it "button_listener" you can call it "amazon" if you want.
        //just make sure you put it in the OnCreate function, right here

        button_listener();
    }//end of onCreate

    private void button_listener()
    {
        //connecting the button object to the button in the UI page
        button_for_hey_there = (Button) findViewById(R.id.button_hey_there);

        //creating the onclick listener
        button_for_hey_there.setOnClickListener(new View.OnClickListener()
        {
            //this is the OnClick function where we need to put all the things that are supposed to happen when the user taps the button

            @Override
            public void onClick(View view)
            {
                //connecting the editText object with the editText widget in the UI
                //this will be the persons name
                EditText editText_collect_user_input = (EditText) findViewById(R.id.editText_input_user);

                //collecting the string that the user has entered
                String temp_collected_name = editText_collect_user_input.getText().toString();

                //creating an output string
                //I want an output something like - "Hey, Jay! Welcome to training at study nildana
                //for those who are new to Java, '+" here acts like a concatenation operator, adding up strings together
                String output_string = "Hey, " + temp_collected_name + "! Welcome to training at study nildana";

                //connecting the textView object with the textView
                TextView textView_output = (TextView) findViewById(R.id.textView);

                //setting the output string to the textView_output object
                textView_output.setText(output_string);

            }
        });

    }//end of button_listener

}//end of class MainActivity

Now, let’s run the app and see if we achieved our goals here. How it looks when app is launched.




Here is when the app is accepting input.




Here is the final input.




Well, there you go. That takes care of a simple app that accepts input and shows output. Don’t forget that you can get the source code for this app at GitHub – https://github.com/Jay-study-nildana/sn_tr_inputoutput

Next episode, we will do page navigation. In page navigation, on pressing a button, the app will go from one page (activity) to another activity. We will also send data from one activity to another.

Follow me on twitter, facebook and instagram for more updates. Thanks!

android training - TextView



This is a bit tutorial to give you some insights about the Android widget – TextView – and stuff that about it that just might be useful to you.

  • TextView is perhaps the most basic of widgets that you will be using when building your apps. You will use it primarily to display information. Here are some scenarios where you might use TextView
  • You want to display a message, asking the user to do something.
  • For instance, in the ‘alert nildna’ app, we are using TextView to display messages like ‘enter the random key’ and also to display the downloaded GPS data.
  • You want to update information after it has been collected from the user.

The Android Studio already provides a number of different types of TextView widgets. Look at this image here. You see, all those four options are all ‘TextView’ widgets but with different options.




You will get the properties option, when you select the TextView widget like this. On the left side is the TextView display some basic information and on the right is the properties of this widget.




Further, you may want to consider doing some additions to the properties.

  • Id – use this to set the id of your widget.
  • Text – use this to set the text.
  • Width – you can adjust the width of this property, height as well as width.
Follow me on twitter, facebook and instagram for more updates. Thanks!

android training – Episode 5 - Deploy to Device



Hello and welcome back. May name is Jay, and I am your trainer for today. We are going to assume that you already went through Episode 1 to 4 Part I and Part II.

We are jumping into development now. That means, the page will be heavy with screenshots. I have tried my best to use low sized images so that you can load this even on a 2G connection. If you are having issues loading any of our tutorial pages, contact us. You will find the contact information, at the end of the blog.

You can download the code for this project, from GitHub, right here – https://github.com/Jay-study-nildana/helloworld3.git

If you don’t know about GitHub, check out our blog post here – http://blog.studynildana.com/post/study-nildana-and-github

Topics Covered In This Episode

In this episode, we will look at doing the following.

  • What is APK?
  • Deploying APK to the phone – method 1
  • Deploying APK to the phone – method 2

What is an APK?

APK is the ‘executable’ file that needs to be actually installed on a device for the app to run. Every time you build an app, an APK file is created. This APK will then be installed on the emulator device or an actual device to see your app in action.

There are two types of APK

  • Debug APK – The debug version of APK that is generated whenever you build on Android Studio.
  • Signed (release) APK – this is the version that you generate when you are ready to push the app to the store so it will be accepted by the Google Play Store.

In this tutorial, we will focus on deploying the Debug APK. Release APK is something we will do later.

Assuming that you have gone through all the episodes till now, you have built a simple ‘hello world’ app and already deployed it to the emulator. Despite app working fine on the emulator, you will still need to deploy the app you built on to your phone.

Go to this blog episode here to find out why you need to deploy to an actual device - http://blog.studynildana.com/post/study-nildana-android-training-deploy-to-device

Deploying APK to the phone – method 1

The first method involves, finding the APK from the project folder. Then, simply copy pasting to the phone and then installing it.

Lets go back to our app that we are building till now, the one that shows “study nildana”. This is how it looks, and focus on the project structure on the top right corner. Right click on the “app” name, and you will get a menu like this.




Once you do that, you will be taken to the project folder, where you should be looking for a folder called ‘app’.



Go inside that folder, and you will find something called “build”. Then, you will find another folder called “outputs”. Here, you will find a folder called “apk”.



You should get that file called “app-debug.apk”. Just copy that file and put it directly into your android phone. After that, you simply need to tap on that apk file from your android phone’s file explorer and you should be able to install it on your phone and then test it.

Remember, due to security settings, you may be unable to install apps by this method. If so, depending on your phone model, you will have to allow “3rd party apps” to be installed in the phone settings.

Deploying APK to the phone – method 2

The above method of copy pasting works fine. In fact, it is the easiest way to put apps on your android phone for testing purposes. However, the problem is that, you may build your app anywhere from 5 to 500 times in any given day. Now, imagine doing the above method, 500 times per day? Not practical and definitely frustrating.

The second method requires you to connect your phone to computer (and installing any drivers that are need for the same). If the phone is actually connected and detected by your computer, you will be able to see your phone under “My Computer” or “This PC’. Like this photo.



After that, you will have to enable developer options and also USB debugging. This is how it looks on my phone. It might look different on your phone.





Now, here enable developer options as well as USB debugging.



If you have done the above, then come back to android studio and build the app. Then, Run the app as if you are trying to run the app using the emulator. Now, along with the emulator, you will also see the option to run the app on your phone. It will look something like this.




Select OK and the app will be displayed on your phone directly. You may get some security warning, and you should give permission to install the app. After this, you can test your app directly on the phone, and do all the debugging as if you were running the app on the emulator.

That takes care of deploying the app on the phone itself. As mentioned above, check this blog post to see why you might want to deploy your app on