Calculate Tyres 2 Rims 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.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;


public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener {
private Spinner sp1,sp2,sp3,sp4,sp5,sp6;

TextView textView6,textView7,textView11,textView12,textView15,textView16,textView20;
String[] treadArray = {"130","135","145","155", "165", "175", "185","195","205","215",
"225","235", "245", "255", "265","275", "285"};
String[] profileArray = {"30","35","40","45", "50", "55", "60","65","70","75","80","85"};
String[] rimArray = {"10","12","13","14", "15", "16", "17","18","19","20","21","22","23"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sp1 = (Spinner) findViewById(R.id.spinner1);
sp2 = (Spinner) findViewById(R.id.spinner2);
sp3 = (Spinner) findViewById(R.id.spinner3);
sp4 = (Spinner) findViewById(R.id.spinner4);
sp5 = (Spinner) findViewById(R.id.spinner5);
sp6 = (Spinner) findViewById(R.id.spinner6);

textView6 = (TextView)findViewById(R.id.textView6);
textView7 = (TextView)findViewById(R.id.textView7);
textView11 = (TextView)findViewById(R.id.textView11);
textView12 = (TextView)findViewById(R.id.textView12);
textView15 = (TextView)findViewById(R.id.textView15);
textView16 = (TextView)findViewById(R.id.textView16);
textView20 = (TextView)findViewById(R.id.textView20);
//InitialSetUpUI();
initControls();

}
public void initControls()
{
sp1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter1 = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item,treadArray);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter1);
sp2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter adapter2 = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item,profileArray);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(adapter2);

sp3 = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter adapter3 = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item,rimArray);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(adapter3);

sp4 = (Spinner) findViewById(R.id.spinner4);
ArrayAdapter adapter4 = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item,treadArray);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp4.setAdapter(adapter4);

sp5 = (Spinner) findViewById(R.id.spinner5);
ArrayAdapter adapter5 = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item,profileArray);
adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp5.setAdapter(adapter5);

sp6 = (Spinner) findViewById(R.id.spinner6);
ArrayAdapter adapter6 = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item,rimArray);
adapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp6.setAdapter(adapter6);


sp1.setOnItemSelectedListener(this);
sp2.setOnItemSelectedListener(this);
sp3.setOnItemSelectedListener(this);
sp4.setOnItemSelectedListener(this);
sp5.setOnItemSelectedListener(this);
sp6.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView parent, View view, int position, long id) {

// double a=0;
double b;
double b1;
double c;
double c1;
double e1;
double w;
double x;
double y;
double z;
double z1;
double e;
double w1;
double x2;
double y3;
double diff,diff1,diff2,diff3,diff4;

((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);
((TextView) parent.getChildAt(0)).setTextSize(28);


double sel1 = Double.parseDouble((String) sp1.getSelectedItem());
double sel2 = Double.parseDouble((String) sp2.getSelectedItem());
double sel3 = Double.parseDouble((String) sp3.getSelectedItem());
double sel4 = Double.parseDouble((String) sp4.getSelectedItem());
double sel5 = Double.parseDouble((String) sp5.getSelectedItem());
double sel6 = Double.parseDouble((String) sp6.getSelectedItem());
w=Double.parseDouble(String.valueOf(sel1));
x=Double.parseDouble(String.valueOf(sel2));
y=Double.parseDouble(String.valueOf(sel3));

w1=Double.parseDouble(String.valueOf(sel4));
x2=Double.parseDouble(String.valueOf(sel5));
y3=Double.parseDouble(String.valueOf(sel6));

b =(w * x /2540*2 + y);
z = Math.round(b *100)/100.0;
c = b * 25.4;
e = Math.round(c *100)/100.0;
textView6.setText(String.valueOf(z + " Inches"));
textView7.setText(String.valueOf(e) + " MM");

b1 =(w1 * x2 /2540*2 + y3);
z1 = Math.round(b1 *100)/100.0;
c1 = b1 * 25.4;
e1 = Math.round(c1 *100)/100.0;
textView11.setText(String.valueOf(z1 + " Inches"));
textView12.setText(String.valueOf(e1 + " MM"));

diff1 = e1;
diff2 = e;
diff = diff1 - diff2;
diff = Math.round(diff * 100)/100.0;
textView15.setText(String.valueOf(diff + " MM"));

diff3 = (diff1 - diff2) / (diff2) * 100;
diff3 = Math.round(diff3 * 100)/100.0;
textView16.setText(String.valueOf(diff3 + " %"));

diff4 = diff3 + 100;
textView20.setText(String.valueOf(diff4 + " Klm"));
}

@Override
public void onNothingSelected(AdapterView parent) {

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

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="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Old Tyre Sizes"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:textColor="#FFF" />

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:id="@+id/frameLayout1"
android:background="@drawable/bg6"
android:layout_marginTop="10dp">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tread Width"
android:id="@+id/textView2"
android:layout_gravity="left|top"
android:textColor="#FFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile Height"
android:id="@+id/textView3"
android:layout_gravity="center_horizontal|top"
android:textColor="#FFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rim Diameter"
android:id="@+id/textView4"
android:layout_gravity="right|top"
android:textColor="#FFF" />



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

android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/btn_spin"
android:id="@+id/spinner1"
android:layout_gravity="left|top"/>

android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/btn_spin"
android:id="@+id/spinner2"
android:layout_gravity="center|top" />

android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/btn_spin"
android:id="@+id/spinner3"
android:layout_gravity="right|top" />


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

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Overall Diameter"
android:id="@+id/textView5"
android:layout_gravity="left|top"
android:textColor="#FFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView6"
android:layout_gravity="center_horizontal|top"
android:textColor="#FFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MM"
android:id="@+id/textView7"
android:layout_gravity="right|top"
android:textAllCaps="true"
android:layout_marginRight="20dp"
android:textColor="#FFF" />



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

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do Not Enter Impossible Tyre Sizes"
android:id="@+id/textView8"
android:layout_gravity="center_horizontal|top"
android:textSize="18sp"
android:background="@drawable/bg6"
android:textColor="#FFF" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/frameLayout5"
android:layout_marginTop="220dp"
android:isScrollContainer="false">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter New Tyre Sizes"
android:id="@+id/textView9"
android:layout_gravity="center"
android:textSize="18sp"
android:textColor="#FFF" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:id="@+id/frameLayout11"
android:background="@drawable/bg6"
android:layout_marginTop="230dp">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tread Width"
android:id="@+id/textView02"
android:layout_gravity="left|top"
android:textColor="#FFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile Height"
android:id="@+id/textView03"
android:layout_gravity="center_horizontal|top"
android:textColor="#FFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rim Diameter"
android:id="@+id/textView04"
android:layout_gravity="right|top"
android:textColor="#FFF" />



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

android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/btn_spin"
android:id="@+id/spinner4"
android:layout_gravity="left|center_vertical" />

android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/btn_spin"
android:id="@+id/spinner5"
android:layout_gravity="center" />

android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/btn_spin"
android:id="@+id/spinner6"
android:layout_gravity="right|center_vertical" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_below="@+id/frameLayout5"
android:id="@+id/framelayout7">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Overall Diameter"
android:id="@+id/textView10"
android:layout_gravity="left|top"
android:textColor="#FFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView11"
android:layout_gravity="center_horizontal|top"
android:textColor="#FFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView12"
android:layout_gravity="right|top"
android:layout_marginRight="20dp"
android:textColor="#FFF" />



android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="390dp"
android:layout_centerHorizontal="true"
android:id="@+id/framelayout8"
android:background="@drawable/bg6">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recommend a maximum of +/-2.5% Difference"
android:id="@+id/textView13"
android:layout_gravity="center_horizontal|top"
android:textSize="12sp"
android:background="@drawable/bg6"
android:textColor="#FFF" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="420dp"
android:layout_centerHorizontal="true"
android:id="@+id/frameLayout9">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Diameter Difference="
android:id="@+id/textView14"
android:layout_gravity="left|top"
android:textColor="#FFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView15"
android:layout_gravity="center_horizontal|top"
android:layout_marginLeft="40dp"
android:textColor="#FFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView16"
android:layout_gravity="right|top"
android:textColor="#FFF" />


android:layout_width="fill_parent"
android:layout_height="95dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="460dp"
android:background="@drawable/bg6"
android:id="@+id/frameLayout10">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="At 100 Klm Reading on your clock"
android:id="@+id/textView17"
android:layout_gravity="left|top"
android:layout_marginTop="10dp"
android:textColor="#FFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Actual speed you are traveling at"
android:id="@+id/textView18"
android:layout_gravity="left|top"
android:layout_marginTop="50dp"
android:textColor="#FFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView19"
android:layout_gravity="center_horizontal|top"
android:layout_marginLeft="50dp"
android:layout_marginTop="30dp"
android:textColor="#FFF" />

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView20"
android:layout_gravity="right|top"
android:layout_marginTop="30dp"
android:textAllCaps="false"
android:layout_marginRight="10dp"
android:textColor="#FFF" />



This is the strings.xml file

alternate text 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