Skip to content


Getting Started With Android

Getting Started With Android
0 votes, 0.00 avg. rating (0% score)

Google Android is an open source platform that includes Linux Kernal based operating system and SDK for mobile or tablet PC application development. When “Android SDK release 1″ came in 21 October 2008, lots of mobile platforms were already available for mobile application developers. But because of its familiar java programming support and open source platform it became popular very soon in smartphone application development.
At the time of writing this post, current version of android is 2.3.3 (Gingerbread) for smartphones and Android 3.0 (Honeycomb) for tablet PCs.

I have developed mobile applications in different mobile platforms like windows phone 7, Blackberry RIM etc., but whenever I develop any application in android I really enjoy the coding. So if you are interested in mobile application development I’ll suggest to try android platform atleast once.


Environment setup:
“http://developer.android.com/sdk/installing.html” provides updated and complete step by step process to setup android Environment setup with troubleshooting.

In this post we will create “AddTwoNumbers” application In which we will learn.
1. How to create simple screens in android?
2. How to add simple UI components on the screens in android?
3. How to listen button click event?
4. How to switch between screens?
5. How to send data from one screen to another?

Step 1: Create new android project in eclipse. In “New Android Project” wizard you have to fill some information about your project.

Project name: AddTwoNumbers
Build Target: Android 2.2
Application name: AddTwoNumbers
Package name: com.vimviv
Create Activity: HomeActivity
Min SDK Version: 8

When you click on “finish” button it will automatically generate different folder structures, HomeActivity.java file and other files that are useful for your project.

Step 2: The thing which I like most in android is that UI development part is completely separate from your logical java code. In Android you can do UI designing in 3 different ways.
1. using drag and drop interface.
2. using XML UI file structure.
3. In you java code also you can do UI designing.

I usually prefer XML interface for UI designing. So go to res -> layout -> main.xml
For this particular application I am using two TextViews as a label, two EditTexts for numbers input and one “Add” button.



    
    
    
    
    

Explanation of above XML is as below:

LinearLayout: Androids provides different layouts for arranging UI components on the screen. In this example I have used LinearLayout. As the name suggest using LinearLayout you can arrange UI components in horizontally or vertically. Default orientation of LinearLayout is horizontal. But in this example I want to arrange UI components vertically so I have given orientation “vertical”.

android:layout_width & android:layout_height: We can specify width and height of the layout but for good portability on different screen sizes, we should give general values like “fill_parent”(take all available space) and “wrap_content” (takes the space that will be enaugh to show the content).

android:id : We have to provide some unique id to the UI component that will be helpful to UI component from java code.

Now in the layout folder create a new layout “result.xml” for result screen and add this code.



    

Explanation of above XML is as below:

android:gravity : Using gravity property we give alignment to the TextView’s text. In this example I have given “center_vertical|center_horizontal” alignment.

Step 3: Open the HomeActivity.java and add this code.


package com.vimviv;

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

public class HomeActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final EditText numberText1 = (EditText) findViewById(R.id.number1_text);
        final EditText numberText2 = (EditText) findViewById(R.id.number2_text);
        Button addButton = (Button) findViewById(R.id.add_button);
        
        addButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				int num1 = Integer.parseInt(numberText1.getText().toString());
				int num2 = Integer.parseInt(numberText2.getText().toString());
				int result = num1+num2;
				Intent intent = new Intent(HomeActivity.this,ResultActivity.class);
				intent.putExtra("result","Result: "+result);
				startActivity(intent);
			}
		});
        
    }
}

Explanation of the above code is as below:

Activity: For creating an activity you have extend the Activity class.

onCreate: When activity is first created onCreate method is called. In this method first line should be super.onCreate(savedInstanceState) . setContentView method will set the content view to main.xml that we have created as our first screen.

findViewById:
Using findViewById method we can access xml defined UI components in java code.

Listener: In android listener pattern is simple, if you want to listen for any event call the method setOnListener, pass the OnListener object and you will catch the event in on method.

In this example we are adding the click event listener so just replace with click.

Intent: Using intents we can launch an activity and using putExtra method of intent we can send data to the other activity.

Now create new Activity ResultActivity.java and add this code.

package com.vimviv;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ResultActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);
		String result = getIntent().getStringExtra("result");
		
		TextView resultView = (TextView) findViewById(R.id.result_view);
		resultView.setText(result);
	}
}


Step 4: Whenever you create a new activity don’t forget to add its information in AndroidManifest.xml


The application is ready now. Go to windows->Android SDK and AVD manager and create new virtual device and run this application in that device.

Incoming search terms:

Posted in Android.

Tagged with .