Cubic Capacity Tutorial

Introduction

This tutorial walks you through creating a simple application for Android using Android Studio. You need to have Android Studio installed on your system,I won’t go into this right now, they are plenty of tutorials you can check out on that subject.

Creating a project

It’s extremely easy to start using Android development! In this tutorial we’ll follow the starting up process with Android Studio. If using Intellij IDEA with Android, the process is almost the same. First let’s create a new project. We choose Start a new Android Studio project or File | New project. The following dialogs walk us through the process of new project creation. We need to name the project and choose which Android SDK version we have installed. Most options can be left with their default values, so we can press ‘Enter’ several times.

Next Name the project:

alternate text Choose the Android version you want to use. The lower API level you use allows the user to use your app on an older device alternate text

Choose an activity from the selection that will be generated for you, use a blank activity for this project.:

alternate text

Next Name the activity, or you can leave the default settings.

alternate text

You have created a new project with one Java activity that was generated for us. Now to add the code for the Main Activity class.
Below is the Full code for the Main Activity.java

import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
TextView txtResult;
EditText edtBore, edtStroke, edtCyl;
Button btnCalc, btnReset;
String sel1, sel2, sel3;
Double w, x, y, sel4;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// setRequestedOrientation (SCREEN_ORIENTATION_PORTRAIT);

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.cc72);


edtBore = (EditText) findViewById(R.id.edtBore);
edtStroke = (EditText) findViewById(R.id.edtStroke);
edtCyl = (EditText) findViewById(R.id.edtCyl);
txtResult = (TextView) findViewById(R.id.txtResult);

btnCalc = (Button) findViewById(R.id.btnCalc);
btnReset = (Button) findViewById(R.id.btnReset);
addListenerOnButton();

}

public void addListenerOnButton() {

btnCalc.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

sel1 = edtBore.getText().toString();
sel2 = edtStroke.getText().toString();
sel3 = edtCyl.getText().toString();

if (TextUtils.isEmpty(sel1) || TextUtils.isEmpty(sel2) || TextUtils.isEmpty(sel3))
Toast.makeText(getApplicationContext(), "Please fill in all fields to continue",
Toast.LENGTH_SHORT).show();
//return;
}

w=Double.parseDouble(String.valueOf(sel1));
x=Double.parseDouble(String.valueOf(sel2));
y=Double.parseDouble(String.valueOf(sel3));

sel4=3.14159*(w/10/2)*(w/10/2)*(x/10)*y;
sel4=Math.round(sel4*100)/100.0;
txtResult.setText(" Engine Capacity Is "+sel4+" CC");

});

}

btnReset.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
edtBore.setText("");
edtStroke.setText("");
edtCyl.setText("");
txtResult.setText("");
edtBore.requestFocus();
}
}

}

Next The activity_main.xml

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/bg6">

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/frame1">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Cubic Capacity"
android:id="@+id/txtView1"
android:layout_gravity="center_horizontal|top"
android:textSize="24sp"
android:textColor="#FFF" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/frame1"
android:layout_centerHorizontal="true"
android:id="@+id/frame2"
android:layout_marginTop="30dp">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bore Size In MM"
android:id="@+id/txtBore"
android:layout_gravity="left|top"
android:textSize="20sp"
android:textColor="#FFF"
android:layout_marginTop="10dp" />

android:layout_width="150dp"
android:layout_height="45dp"
android:background="@drawable/shape_me"
android:id="@+id/edtBore"
android:layout_gravity="center_horizontal|top"

android:textColor="#000"
android:textSize="20sp"
android:layout_marginLeft="90dp"
android:paddingLeft="20dp"
android:inputType="numberDecimal" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/frame2"
android:layout_centerHorizontal="true"
android:id="@+id/frame3"
android:layout_marginTop="20dp">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Crank Stroke MM"
android:id="@+id/txtStroke"
android:layout_gravity="left|top"
android:textSize="20sp"
android:textColor="#FFF"
android:layout_marginTop="10dp" />

android:layout_width="150dp"
android:layout_height="45dp"
android:background="@drawable/shape_me"
android:id="@+id/edtStroke"
android:layout_gravity="center_horizontal|top"
android:textColor="#000"
android:layout_marginLeft="90dp"
android:textSize="20sp"
android:paddingLeft="20dp"
android:inputType="numberDecimal" />


android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/frame3"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:id="@+id/frame4">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number Of Cyls"
android:id="@+id/txtCyls"
android:layout_gravity="left|top"
android:textSize="20sp"
android:textColor="#FFF"
android:layout_marginTop="10dp" />

android:layout_width="150dp"
android:layout_height="45dp"
android:background="@drawable/shape_me"
android:id="@+id/edtCyl"
android:layout_gravity="center_horizontal|top"
android:textColor="#000"
android:textSize="20sp"
android:layout_marginLeft="90dp"
android:paddingLeft="20dp"
android:password="false"
android:inputType="number" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/frame4"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:id="@+id/frame5">

android:layout_width="150dp"
android:layout_height="55dp"
android:background="@drawable/btn_shape"
android:text="Calculate"
android:id="@+id/btnCalc"
android:layout_gravity="left|top"
android:textSize="20dp" />

android:layout_width="150dp"
android:layout_height="55dp"
android:background="@drawable/btn_shape"
android:text="Reset"
android:id="@+id/btnReset"
android:layout_gravity="right|top"
android:textSize="20dp" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/frame5"
android:layout_centerHorizontal="true"
android:id="@+id/frame6"
android:layout_marginTop="40dp">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtResult"
android:layout_gravity="center_horizontal|top"
android:textColor="#FFF"
android:textSize="20sp"
android:lines="2"
android:linksClickable="false"
android:hint="Result" />


Next the menu_main.xml file

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">

android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />


This is the strings.xml file

?xml version="1.0" encoding="utf-8"?>


string name="app_name">Cubic Measurement /string
string name="cubic_capacity">Calculate Cubic Capacity string
string name="action_settings">Settings /string

/resources>

This is the Manifest.xml file


package="example.cubiccapacity">

android:allowBackup="true"
android:icon="@drawable/cc72"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".MainActivity"
android:label="@string/app_name" >










When you have entered all of the code,
this is what the design page should look like

alternate text

You should now be able to click run and you should have a working App