Sample Application, Better Than “Hello World”, in Android

This sample application I have created as first hand application after learning about Android. I am not experienced in Java and Android is in Java. So, practicing simple application will allow me to really test knowledge and get accustomed in Java as well. Few things in Java is really different than PHP. Hello World! example app is posted in preview post.

This application accepts integer value and square it after clicking button.

How this sample application looks:

android-application-sample

File -> New -> New Android Application:

- Application Name: Square It
- Project Name: SquareIt
- Package Name: com.satyaweblog.squareit

File: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:visibility="visible"
    tools:context=".MainActivity" >
    <EditText
        android:id="@+id/square"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="false"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:hint="@string/number_to_square"
        android:inputType="number"
        android:text="@string/default_value"
        android:textIsSelectable="true"
        android:visibility="visible" />
    <TextView
        android:id="@+id/result"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/square"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:textIsSelectable="true"
        android:visibility="visible" />
    <Button
        android:id="@+id/button1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/result"
        android:layout_centerHorizontal="true"
        android:height="4dp"
        android:text="@string/calculate" />
</RelativeLayout>

File: MainActivity.java

package com.satyaweblog.squareit;
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.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
	EditText txtNum;
	TextView res;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		txtNum = (EditText) findViewById(R.id.square);
		res = (TextView) findViewById(R.id.result);
		Button bttn = (Button) findViewById(R.id.button1);
		bttn.setOnClickListener(this);
	}
	public void onClick(View v) {
		int num1;
		if (txtNum.getText().toString().trim().length() == 0) {
			return;
		}
		num1 = Integer.parseInt(txtNum.getText().toString().trim());
		num1 = num1 * num1;
		res.setText(String.valueOf(num1)); // Wrong: res.setText(num1);
											// http://stackoverflow.com/questions/15191092/android-content-res-resourcesnotfoundexception-string-resource-id-fatal-except
	}
	@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;
	}
}

File: strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="action_settings">Settings</string>
<string name="calculate">Calculate</string>
<string name="default_value">5</string>
<string name="number_to_square">Number to square</string>
</resources>