Tuesday 30 December 2014

Android Programming - My First App


In this post we will learn how to develop a basic android application and installing it to your phone.

Here we use eclipse IDE with ADT plugin.


A Simple Calculator : 

Follow the steps :



1) Open Eclipse .
2) Navigate File --> New --> Other--> Android--> Android Application Project
3) Then fill the details in that dialogue. 
  
    See the following screenshots.. 







After this process has finished , we will design the layout of the app first.

4) Navigate MyFirstApp -> res -> layout -> activity_main

 Here we design the app , in that layout file place the following code

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter 1st num."
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" >
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Enter 2nd num."
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="50"
            android:text="ADD" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="50"
            android:text="SUB" />
   </LinearLayout>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Now your graphical layout looks like this 






















5) Now we will write some java code to add functionality to our app.

Navigate MyFirstApp -> src -> tuts.coolestapps.MyFirstApp->MainActivityIt will look like this












Now add the following code to it .
package tuts.coolestapps.myfirstapp;

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

public class MainActivity extends Activity implements OnClickListener {
    
    EditText number1, number2;
    TextView result;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        
    }
    
    private void initialize() {
        number1 = (EditText) findViewById(R.id.editText1);
        number2 = (EditText) findViewById(R.id.editText2);
        TextView lnumber1 = (TextView) findViewById(R.id.textView1);
        TextView lnumber2 = (TextView) findViewById(R.id.textView2);
        result = (TextView) findViewById(R.id.textView3);
        Button add = (Button) findViewById(R.id.button1);
        Button sub = (Button) findViewById(R.id.button2);
        add.setOnClickListener(this);
        sub.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
            Double addnum1 = Double.valueOf(number1.getText().toString());
            Double addnum2 = Double.valueOf(number2.getText().toString());
            Double sum = addnum1 + addnum2;
            result.setText("Sum is: " + String.valueOf(sum));
            break;
            case R.id.button2:
            Double subnum1 = Double.valueOf(number1.getText().toString());
            Double subnum2 = Double.valueOf(number2.getText().toString());
            Double sub = subnum1 - subnum2;
            result.setText("Difference is: " + String.valueOf(sub));
            break;
        }
        
    }
    
}




6)  Save the whole project (ctrl + s).

7)  Running the app in your device
Navigate Windows -->Android Virtual Device Manager --> Select Your AVD --> Start
















8) After the emulator finished loading 
     Right Click on the project -> Run As -> Android Application






















9) Exporting the app.
 
Right Click on the project ->Android Tools -> Export Signed Application Package 




















Now it will ask for keystore , then create a new keystore






















NOTE : This Keystore is used when you are uploading your app in the playstore, make sure you remember the keystore password  else you face problems while upgrading your app.Same keystore must be used. 


In the next step confirm keystore and set the destination location  of  the app


































Now copy that MyFirstApp.apk to your device and run it.

You can also install the app in your phone directly by connecting your device to system by using USB cable in step 8.

That's it we  are done . We have successfully developed our first app and installed it in our mobile devices.