[ANDROID][TUT 2]Setting Up Project and Creating Your First Android Application

Welcome to second tutorial! Now I have assumed that you must have setup your development environment for Android applications. Before creating first application, I want to you to know about some basic things in Android app development :

There are two parts in an application: Design Part (Where we define the UI(User Interface)) and Coding part (Where we deal with actual logic). There are two languages which are used in an android application :

XML :  This is used for defining the design of the application. Here we show how the application looks-like. If you have not familiar with XML, don’t worry you will get used with it.  It is very easy language. All the XML files are located in the res folder of the project.

Which folder depicts what, I’ll tell you shortly.

Java : With Java, we insert actual logic in the application to do certain tasks. This is main part of the application. You must know the basic fundamentals of Java. Java Source files are located in the src folder. If you also don’t familiar with Java, just get through the basics of classes and packages. There are tons of tutorials online. Since Java is also an Object oriented languages, if you have studied C++ , you will quickly get used to it.

Alright! Lets create our first application!

Setting up the Application Project

1. Go to your adt bundle folder that you have downloaded. Go to eclipse folder => Launch eclipse. I assume you do all the formalities of setup your development environment like setting workspace etc. If not, Go to this tutorial.

2. Click on New Android Application if you have screen like this. If not, Goto File => New => Android Application Project.

Screenshot_2

3. This dialog window will appear :

Screenshot_3

Let me explain each of things:

  • Application Name : It is the application name that you will see on your device.
  • Project Name is the name of your project directory and the name visible in Eclipse.
  • Package Name is the package namespace for your app. It must be unique if you are going to upload your app on Play Store. Every application has unique package name.
  • Minimum Required SDK is the lowest version of Android that your app supports. To support as many devices as possible, you should set this to the lowest version available that allows your app to provide its core feature set.
  • Target SDK indicates the highest version of Android with which you have tested with your application. As new versions of Android become available, you should test your app on the new version and update this value to match the latest API level in order to take advantage of new platform features.
  • Compile With is the platform version against which you will compile your app. By default, this is set to the latest version of Android available in your SDK.
  • Theme specifies the default Android theme which is set your app. You can change this later.

Click on Next

4. On the next screen to configure the project, leave the default selections and click Next.

Screenshot_4
5. The next screen can help you create a launcher icon for your app. Click Next.

Screenshot_5

6. Now you can select an activity template from which to begin building your app. For this project, select BlankActivity and click Next.

Screenshot_6

7. Lastly, You can change default activity name or default XML layout file name or let them at their default state and click Finish.

Screenshot_7

Finally after setting the configuration for your application project, You came up with this screen. In the left side, there is Package Explorer which displays all the files which are included in your application.Screenshot_8

Now you must be thinking why there are so much folders and they actually means. Each folder plays their part and helps in generating final APK file. I will describe each folder about what they mean:

Screenshot_9

Creating Your application

As for this tutorial,  I will just take two things so that You can edit something in your first Android app.

Layout file: This is the layout file for your activity( If you are confused about this keyword, I will soon write up a tutorial about this). This is the actual design. In our project, activity_main.xml is the default xml file that is created for Mainactivity(which deals with coding part). Just double-click on activity_main.xml, You will get this window:

Screenshot_10

See the palette on the right-hand side, It contains all the things that you can use for designing your UI. You can directly drag and drop each object. It will automatically convert into XML code. This is graphical layout you are seeing, the actual design code is visible when you click on activity_main.xml at the bottom.

Screenshot_11

You must be thinking, What this code, How its works? Well, Don’t worry you will soon get used to it. You are just seeing the XML code. XML is all about tags and attributes. Here RelativeLayout is a tag which has certain attributes and must be closed </RelativeLayout>.

So, What this <RelativeLayout> means? Well, In android there are different types of layouts :

1. Linear Layout
2. Relative Layout
3. Table Layout
4. Grid View
5. Tab Layout
6. List View

For now, RelativeLayout is the layout in which you can position one elements relative to other elements. TextView is the element which is used for displaying texts. You can read about various elements on Android Developer Reference guide.

To make some change, Lets say you want to change the text “Hello world” to something. Change the TextView’s attribute android:text=”This is my MyDroid application”

Screenshot_12

Now, for example, you want to add some button. Go to Graphical layout by clicking at the bottom. Drag out the Button from the pallette to the screen area.

Screenshot_13

You can change the Button text from the properties windows. You can change lot of options from there. Just play with it, you will explore more about the features.

Screenshot_14

Source File: Okay, You have made your design part. Lets talk about the coding part which will include editing the source files in the src folder. The design we created is for MainActivity.java file.

So, lets make use of this button we have added. Lets change the default text when the button is clicked. For this logic, the MainActivity,java will use this code:

 


package com.droid.mydroid;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //Using SetContentView we are setting the design for MainActivity
        

      //Declaring the variables
        final TextView text = (TextView) findViewById(R.id.textView1);
        Button clickme = (Button) findViewById(R.id.button1);
        

        //This is ClickListener for button(When is button is clicked, the onClick function executes)
        clickme.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

               //When button is clicked, it will change the Textview default text to this
                text.setText("Yey!! I created my First App!");
            }
        });
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

After writing your code, Save all your files.

Running Your Application

 

Finally, the code is complete. Lets run the application. Click on Run icon on below menu bar:

Screenshot_17

Select Android Application. Click on OK

Screenshot_15

It will open the emulator and install your generated APK file and Launch the application. You can see the status in the Console bar at the bottom.

Screenshot_18

Lets see the our application in action:

Screenshot_19

After Clicking button

Screenshot_20

Download Files

So, This was your first Android application. Just play with the tool, you will only explore new options and get more used to it. Stay tuned for next tut!