Handy Maths Tutorial

Introduction

This tutorial will take 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 How to install 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.content.pm.ActivityInfo;
import android.os.Bundle;
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.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements View.OnClickListener{
String sel1;
String sel2;
String sel3;
String sel5;
double sel4;
double w,x,y,z;
EditText edtMph, edtRevs, edtDiff, edtGears;
TextView txtView1, txtView2, txtView3, txtView4, txtResult;
RadioGroup rg;
Button btnMph, btnRpm, btnDiff, btnTyre, btnGears, btnReset;

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

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.handy_maths);

txtView1 = (TextView) findViewById(R.id.txtView1);
txtView2 = (TextView) findViewById(R.id.txtView2);
txtView3 = (TextView) findViewById(R.id.txtView3);
txtView4 = (TextView) findViewById(R.id.txtView4);
txtResult = (TextView) findViewById(R.id.txtResult);

edtMph = (EditText) findViewById(R.id.edtMph);
edtRevs = (EditText) findViewById(R.id.edtRevs);
edtDiff = (EditText) findViewById(R.id.edtDiff);
edtGears = (EditText) findViewById(R.id.edtGears);

btnMph = (Button) findViewById(R.id.btnMph);
btnRpm = (Button) findViewById(R.id.btnRpm);
btnDiff = (Button) findViewById(R.id.btnDiff);
btnTyre = (Button) findViewById(R.id.btnTyre);
btnGears = (Button) findViewById(R.id.btnGears);
btnReset = (Button) findViewById(R.id.btnReset);
btnMph.setOnClickListener(this);
btnDiff.setOnClickListener(this);
btnRpm.setOnClickListener(this);
btnTyre.setOnClickListener(this);
btnGears.setOnClickListener(this);
btnReset.setOnClickListener(this);

rg = (RadioGroup) findViewById(R.id.rg1);
rg.clearCheck();


rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup rg, int checkedId) {
// find which radio button is selected

if (checkedId == R.id.rdBtnMph) {
txtView1.setText("Tyre Size");
txtView2.setText("Top Revs");
txtView3.setText("Diff Ratio");
edtGears.setVisibility(View.INVISIBLE);
txtView4.setText("");
edtGears.setText("");
txtResult.setText("");
Toast.makeText(getApplicationContext(),"choice: Check Top Speed",
Toast.LENGTH_SHORT).show();

}

if (checkedId == R.id.rdBtnRpm) {
txtView1.setText("Tyre Size");
txtView2.setText("Diff Ratio");
txtView3.setText("Top Speed");
edtGears.setVisibility(View.INVISIBLE);
txtView4.setText("");
edtGears.setText("");
txtResult.setText("");
Toast.makeText(getApplicationContext(), "Check Maximum RPM",
Toast.LENGTH_SHORT).show();
}

if (checkedId == R.id.rdBtnDiff) {
txtView1.setText("Tyre Size");
txtView2.setText("Top Revs");
txtView3.setText("Top Speed");
edtGears.setVisibility(View.INVISIBLE);
txtView4.setText("");
edtGears.setText("");
txtResult.setText("");
Toast.makeText(getApplicationContext(), "choice: Check Diff Ratio",
Toast.LENGTH_SHORT).show();
}

if (checkedId == R.id.rdBtnTyre) {
txtView1.setText("Top Speed");
txtView2.setText("Diff Ratio");
txtView3.setText("Max Revs");
edtGears.setVisibility(View.INVISIBLE);
txtView4.setText("");
edtGears.setText("");
txtResult.setText("");
Toast.makeText(getApplicationContext(), "choice: Check Diff Ratio",
Toast.LENGTH_SHORT).show();
}

if (checkedId == R.id.rdBtnGears) {
txtView1.setText("Tyre Size");
txtView2.setText("Max Revs");
txtView3.setText("Diff Ratio");
txtView4.setText("Gear Ratio");
edtGears.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "choice: Selected GearBox Ratio",
Toast.LENGTH_SHORT).show();
}
}

});
}

@Override
public void onClick(View view) {

if(view==btnMph)
{
sel1 = edtMph.getText().toString();
sel2 = edtRevs.getText().toString();
sel3 = edtDiff.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 = w * 3.1415927 / 36 / 1760 *60 * x / y;
sel4 = Math.round(sel4 *100)/100.0;
txtResult.setText(sel4 + " Miles Per Hour");
return;
}
if(view==btnDiff)
{
sel1 = edtMph.getText().toString();
sel2 = edtRevs.getText().toString();
sel3 = edtDiff.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 = w * 3.1415927 / 36 / 1760 *60 * x / y;
sel4 = Math.round(sel4 *100)/100.0;
txtResult.setText("Final Drive Ratio = " + sel4);
return;
}

if(view==btnRpm)
{
sel1 = edtMph.getText().toString();
sel2 = edtRevs.getText().toString();
sel3 = edtDiff.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 = 1760 * 36 / (w * 3.1415927) * x * y / 60;
sel4 = Math.round(sel4 *100)/100.0;
txtResult.setText("Revs Per Minute = " + sel4);
return;
}
if(view==btnTyre)
{
sel1 = edtMph.getText().toString();
sel2 = edtRevs.getText().toString();
sel3 = edtDiff.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 = (w * x / y) /60 *1760 * 36 / 3.1415927;
sel4 = Math.round(sel4 *100)/100.0;
txtResult.setText("Tyre Size = " + sel4 + " Inches");
return;
}
if(view==btnGears)
{
sel1 = edtMph.getText().toString();
sel2 = edtRevs.getText().toString();
sel3 = edtDiff.getText().toString();
sel5 = edtGears.getText().toString();

if (TextUtils.isEmpty(sel1) || TextUtils.isEmpty(sel2) || TextUtils.isEmpty(sel3) ||
TextUtils.isEmpty(sel5)) {
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));
z=Double.parseDouble(String.valueOf(sel5));

sel4 = (w * 3.1415927) / 36 / 1760 * 60 * x / y / z;
sel4 = Math.round(sel4 *100)/100.0;
txtResult.setText("Selected Gear = " + sel4 + " MPH");
return;
}
if(view==btnReset)
{
txtView1.setText("");
txtView2.setText("");
txtView3.setText("");
txtView4.setText("");
edtGears.setText("");
edtRevs.setText("");
edtMph.setText("");
edtDiff.setText("");
txtResult.setText("");

}
}
}

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="120dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true
" android:id="@+id/frameLayout1">

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="@+id/rg1">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MPH"
android:id="@+id/rdBtnMph"
android:checked="false"
android:textSize="24sp"
android:textColor="#FFFFFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Diff Ratio"
android:id="@+id/rdBtnDiff"
android:layout_gravity="right"
android:layout_marginTop="-35dp"
android:checked="false"
android:textSize="24sp"
android:textColor="#FFFFFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tyre Size"
android:id="@+id/rdBtnTyre"
android:layout_gravity="right"
android:checked="false"
android:textSize="24sp"
android:textColor="#FFFFFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RPM"
android:id="@+id/rdBtnRpm"
android:layout_marginTop="-30dp"
android:checked="false"
android:textSize="24sp"
android:textColor="#FFFFFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gear Ratio"
android:id="@+id/rdBtnGears"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="24sp"
android:textColor="#FFFFFF" />


android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/frameLayout1"
android:layout_centerHorizontal="true"
android:id="@+id/frameLayout2"
android:layout_marginTop="10dp">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtView1"
android:layout_gravity="left|center_vertical"
android:textSize="24sp"
android:textColor="#FFFFFF" />

android:layout_width="180dp"
android:layout_height="wrap_content"
android:background="@drawable/shape_me"
android:id="@+id/edtMph"
android:layout_gravity="center"
android:textSize="24sp"
android:layout_marginLeft="60dp"
android:paddingLeft="10dp"
android:inputType="numberDecimal" />


android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/frameLayout2"
android:layout_centerHorizontal="true"

android:id="@+id/frameLayout3">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtView2"
android:layout_gravity="left|center_vertical"
android:textSize="24sp"
android:textColor="#FFFFFF" />

android:layout_width="180dp"
android:layout_height="wrap_content"
android:background="@drawable/shape_me"
android:id="@+id/edtRevs"
android:layout_gravity="center"
android:textSize="24sp"
android:layout_marginLeft="60dp"
android:paddingLeft="10dp"
android:inputType="numberDecimal" />


android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/frameLayout3"
android:layout_centerHorizontal="true"
android:id="@+id/frameLayout4">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtView3"
android:layout_gravity="left|center_vertical"
android:textSize="24sp"
android:textColor="#FFFFFF" />

android:layout_width="180dp"
android:layout_height="wrap_content"
android:background="@drawable/shape_me"
android:id="@+id/edtDiff"
android:layout_gravity="center"
android:textSize="24sp"
android:layout_marginLeft="60dp"
android:paddingLeft="10dp"
android:inputType="numberDecimal" />


android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="@+id/frameLayout4"
android:layout_centerHorizontal="true"
android:id="@+id/frameLayout5">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtView4"
android:layout_gravity="left|center_vertical"
android:textSize="24sp"
android:textColor="#FFFFFF" /
>
android:layout_width="180dp"
android:layout_height="wrap_content"
android:background="@drawable/shape_me"
android:id="@+id/edtGears"
android:layout_gravity="center"
android:textSize="24sp"
android:layout_marginLeft="60dp"
android:visibility="invisible"
android:paddingLeft="10dp"
android:inputType="numberDecimal" />


android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_below="@+id/frameLayout5"
android:layout_centerHorizontal="true"
android:id="@+id/frameLayout6">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtResult"
android:layout_gravity="center"
android:textSize="20sp"
android:textColor="#000000"
android:lines="2" />


android:layout_width="fill_parent"
android:layout_height="160dp"
android:layout_below="@+id/frameLayout6"
android:layout_alignRight="@+id/frameLayout6"
android:layout_alignEnd="@+id/frameLayout6">

android:layout_width="95dp"
android:layout_height="35dp"
android:background="@drawable/btn_shape"
android:text="MPH"
android:id="@+id/btnMph"
android:layout_gravity="left|top"
android:textSize="18sp"
android:clickable="true" />

android:layout_width="115dp"
android:layout_height="35dp"
android:background="@drawable/btn_shape"
android:text="Diff Ratio"
android:id="@+id/btnDiff"
android:layout_gravity="center_horizontal|top"
android:textSize="18sp"
android:clickable="true" />

android:layout_width="95dp"
android:layout_height="35dp"
android:background="@drawable/btn_shape"
android:text="RPM"
android:id="@+id/btnRpm"
android:layout_gravity="right|top"
android:textSize="18sp" />

android:layout_width="95dp"
android:layout_height="35dp"
android:background="@drawable/btn_shape"
android:text="Tyres"
android:id="@+id/btnTyre
" android:layout_gravity="left|center_vertical"
android:textSize="18sp"
android:layout_marginTop="20dp" />

android:layout_width="115dp"
android:layout_height="35dp"
android:background="@drawable/btn_shape"
android:text="Gear Ratio"
android:id="@+id/btnGears"
android:layout_gravity="center"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:clickable="true" />

android:layout_width="95dp"
android:layout_height="35dp"
android:background="@drawable/btn_shape"
android:text="Reset"
android:id="@+id/btnReset"
android:layout_gravity="right|center_vertical"
android:textSize="18sp"
android:layout_marginTop="20dp" />


Next the menu_main.xml file

alternate text

This is the strings.xml file

alternate text

This is the Manifest.xml file

alternate text

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