Android App Development with Hello World Project

December 23, 2014 Posted by WithU Technologies , , , ,
This is our second post on Android app development which is the basic of Android app development. Every Android developer start the app development course with this project. So, learn it carefully because it is the basic of Android app development.

Though we made this app a little bit advanced as it features a pop out message within a small window and this feature is known as Toast.

What is Toast?

Toast

A toast provides simple feedback about an operation in a small popup window. Toasts would automatically disappear after a timeout.

Before heading towards the Hello world project development, we assume that a certain step is already completed by you and that is:

So, let’s start our First Android Application.

  • Open Android Studio IDE.


Quick start window

  • Click on New project and Name it.


New project

  • Select the project location and Click Next.


minimum sdk

  • Select the minimum SDK. (I recommend to set it up to Android 2.2 Froyo).

  • Select Blank activity in the next window and Click next.


Activity

  • Name your activity without any space and I recommend to name it with lowercase letters.

  • Click on Finish. Then the Android studio build the project and load the components of the project so it may take few seconds.

  • A Basic structure is already created by Android Studio and now we have to add the function which we want to do from the application.


Let me remind you that today we are going to create a basic application which will say "Hello" along with your name in a popup window after you type it in the box

First, we have to change the string.xml file:

  • From the Navigation bar on the left side, navigate to the following path-


Your application name -> res -> values -> strings.xml

strings before

  • By default the above strings were written on the box


Strings after

  • Change it to the code shown on the picture above.


For the Layout design of the app we need following components:

  • Android screen layout

  • TextView

  • EditText

  • Buttonactivity main


So, to add those with orientation and dimension follow the steps shown below:

  • Navigate to the following path through the navigation bar on the left side.


Your application name -> res -> layout -> activity_main.xml (The name of the xml file and your application name will vary as it depends on the name you typed during new project creation)

  • Edit and code your main.xml layout file as per the picture shown: (I'll also give it as CODE, but don't just copy-paste)








<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"

android:orientation= "vertical" >

<TextView

android:text="@string/hello_world"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

<EditText

android:id="@+id/name"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

<Button

android:id="@+id/tap"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text= "@string/tap"/>

</LinearLayout>






 

I add the codes in a simple copy paste format, but don't just copy and paste them to your code editor, as in that way you didn’t able to learn Android app development. So, it will be better for your learning process that you type the codes by yourself. Be careful about the cases as java is a case sensitive language i.e. android and Android resembles different meanings in java.

 

Now we have to edit the java file. The java file holds the function of your app.Let me introduce you to a few functions from our Hello World app java codes.

import

  • The import word along with the android and particular function name signifies that it is importing certain functions from library as per your need.


 

public class

  • In java classes are divide into three and they are Public class, Private class and Protected class. Public class means that the function under that class is accessible by anyone.


 

button id

  • This function will finds the Button which was added to your layout by its ID which is "tap" in our example.Open the layout file and you would find the button layout id is denoted as "tap". You can change it to anything, but keep it short.


 

Button function

  • This function tolds that when the user tap the button it will respond by the do something function.

  • In reply of tapping or pressing the button it will show the word typed in the Edit Text layout for a certain duration called as Toast.LENGTH_LONG;

  • It shows the Character sequence by adding the word side by side.

  • "Hello " + name.getText() +"!";        It will Show Hello, you can change it to Hi or something else you want then it get the text which was typed by user and then it ends with an exclamation mark.

  • Toast toast = Toast.makeText(context, text, duration);toast.show();           It setup the context text and duration and show it in a popup window.


To edit the java code follow the following steps:

  • Navigate to following path:


Your Application name\app\src\main\java\com\example\rjchakraborty\helloworld\main.java    (The application name, username will vary)

  • Type the full code given below and careful about case sensitivity.






 


import android.widget.Toast;import android.view.View.OnClickListener;import android.content.Context;

import android.view.View;

EditText name;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Capture our button from layout

Button button = (Button)findViewById(R.id.tap);

// Register the onClickListener with the implementation above

button.setOnClickListener(taplistener);

}

// Create an anonymous implementation of OnClickListener

private OnClickListener taplistener = new OnClickListener() {

public void onClick(View v) {

long id = 0;

// Do something when the button is clicked

try {

name = (EditText)findViewById(R.id.name);

Context context = getApplicationContext();

CharSequence text = “Hello ” + name.getText() +”!”;

int duration = Toast.LENGTH_LONG;

Toast toast = Toast.makeText(context, text, duration);

toast.show();

}

catch(Exception e) {

Context context = getApplicationContext();

CharSequence text = e.toString() + “ID = ” + id;

int duration = Toast.LENGTH_LONG;

Toast toast = Toast.makeText(context, text, duration);

toast.show();

}

}

};

}


 




 

  • After completion save the project from File-> Save all   or   ctrl+S.


AVD manager icon

  • Open your AVD Manager from the icon highlighted above with yellow and launch the Android Device.


AVD manager

  • After the startup Completion of your virtual android device Click on Run 'app'   or   Shift+F10.

  • If there were no error throughout the app, then the app will be installed on your virtual device and launched.


Hello world app

  • Now test your first Android app. Hope you like it!.


If any error persist during the process, feel free to ask us below in our comment box. For more posts on Android and Android development project Check out our updates under the Android Modifier menu.

 

Click to download the full project developed by RJ Chakraborty. The Link is given below:

Hello world app