First Android Project

Here we are discussing how to create an Android project. This section on creating the android project will be your stepping stone for a strong foundation in developing an Android Project

1) Create Project

Select File → New → Other → Android → Android Project and create the Android project “com.android.temperature”. Enter the following.

Create Android Project
Create Android Project
New Android Project
New Android Project
New Android Project Application Info
New Android Project Application Info

Press “Finish”. This should create the following directory structure.

Android Project Structure
Android Project Structure

 

“res” consists of structured values which are known to the android platform. The directory “assets” helps to store any kind of data. The AssetsManager and getAssets methods are helps to extract these data in Java.

2) Two faces of things

To define certain artifacts like strings and UT’ the developer uses the Android SDK in two ways.

1) Through rich editor

2) Directly through XML

We can switch between both things by selection on the tab of the lower part of the screen.

3) Create attributes

Android permits to make attributes for resources like strings or colors and these attributes can be used in our destination through XML or in our Java source code. We can add the following “String” attributes and it permits the developer to translate the application at a later point.

Name Value
celsius to Celsius
fahrenheit to Fahrenheit
calc Calculate
Android Resources
Android Resources

Click Add and give Name as myColor and Value as #3399CC

Switch to the XML representation and validate the values.

Eg: –

<resources>
 <string name="app_name">Temparature Convertor</string>
 <string name="hello_world">Hello world!</string>
 <string name="menu_settings">Settings</string>
 <string name="title_activity_main">Temparature Convertor</string>
 <color name="myColor">#3399CC</color>
 <string name="celsius" >to Celsius</string>
 <string name="fahrenheit">to Fahrenheit</string>
 <string name="calc">Calculate</string>
 </resources>

4) Add UI Elements

This editor permits to create the UI through drag and drop or through the XML source code. We can switch between both representations through the tabs at the bottom of the editor and use the outline view for changing the position and grouping elements.

Eg: – The below figure shows the screenshot of the Palette view and this view changes frequently so it leads to your view might be a bit different.

Android Palette
Android Palette

From the Palette section Form Widgets, drag a Button object onto the layout.

Android Palette with Button
Android Palette with Button

Switch to “main.xml” and verify that your XML looks like the following.

Eg: –

<?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" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EditText" >
</EditText>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" >
</RadioButton>
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" >
</RadioButton>
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Android:text="Button" >
</Button>
</LinearLayout>

5) Edit UI properties

 

We can change the UI properties through the properties view or through the right mouse menu or directly in the XML. The faster way of changing the UI properties is through the XML files.

Open the file “main.xml” We will eliminate the initial text for the XML EditText field. Go to the XML tab called “main.xml” and delete the android: text = “EditText” the EditText property. Go to the “Graphic Design” tab and verify that the text was deleted.

Use the right mouse button on the first radio button to assign the “Celsius” string attribute property “Text”. Assign and “Fahrenheit” string attribute of the second option.

Set the property “Checked” to the truth for the first RadioButton. Assign “layer” to the text property of the button and assign “myClickHandler” to “onclick” property. Set the “Input Type” property “numberSigned” and “numberDecimal” in EditText. All other user interface controls are in a LinearLayout. We assign a background color to this LinearLayout. Right-click an empty space in graphics mode, then select Properties Other Properties → All by name in the background. Select “Color” and then “MyColor” in the list.

Switch to the “main.xml” tab and verify that the XML is correctly maintained.

Eg: –

<?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:background="@color/myColor"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal|numberSigned" >
</EditText>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/celsius" >
</RadioButton>
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fahrenheit" >
</RadioButton>
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClickHandler"
android:text="@string/calc" >
</Button>
</LinearLayout>

6) Code your Application

For the generation of the new android project we define that an Activity called ConvertActivity must be created and the project wizard also created the corresponding Java class.

Eg: –

package com.android.temperature;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class ConvertActivity extends Activity {
private EditText text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (EditText) findViewById(R.id.editText1);
}
// This method is called at button click because we assigned the name to the
// "On Click property" of the button
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.button1:
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
if (text.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
if (celsiusButton.isChecked()) {
text.setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
celsiusButton.setChecked(false);
fahrenheitButton.setChecked(true);
} else {
text.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
break;
}
}
// Converts to celsius
private float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// Converts to fahrenheit
private float convertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}
}

7) Start Project

To start the Android Application, select your project, right click on it, and select Run-As → Android Application. The emulator takes time to start and this will how it will be.