Android - Simple Converter For Beginners

Operating System
In this tutorial we will try to create a Simple Converter Using Android. Android is a widely-adopted open-source project. Android is an open source so that developer find it easy to develop and expand new features. Android is available to any devices such as TV, phones, watches etc. So now let's do the coding.....

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things. Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the activity_main.xml and click text to view the script. Then copy and paste the code below.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="com.razormist.simpleconverter.MainActivity">
  8.  
  9.     <TextView
  10.         android:id="@+id/tv_foot"
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="wrap_content"
  13.         android:text="Foot"
  14.         android:textSize="20sp"
  15.         android:layout_alignBaseline="@+id/et_foot"
  16.         android:layout_toLeftOf="@+id/et_inches"/>
  17.  
  18.     <EditText
  19.         android:id="@+id/et_foot"
  20.         android:layout_height="wrap_content"
  21.         android:layout_width="match_parent"
  22.         android:layout_centerInParent="true"
  23.         android:layout_alignParentTop="true"
  24.         android:layout_marginTop="100dp"
  25.         android:layout_toRightOf="@+id/tv_foot"
  26.         android:inputType="number"/>
  27.  
  28.     <TextView
  29.         android:id="@+id/tv_inches"
  30.         android:layout_height="wrap_content"
  31.         android:layout_width="wrap_content"
  32.         android:text="Inches"
  33.         android:textSize="20sp"
  34.         android:layout_alignBaseline="@+id/et_inches"
  35.         android:layout_alignBottom="@+id/et_inches"/>
  36.  
  37.     <EditText
  38.         android:id="@+id/et_inches"
  39.         android:layout_height="wrap_content"
  40.         android:layout_width="match_parent"
  41.         android:layout_centerInParent="true"
  42.         android:layout_below="@+id/et_foot"
  43.         android:layout_toRightOf="@+id/tv_inches"
  44.         android:inputType="number"/>
  45.  
  46.     <Button
  47.         android:id="@+id/btn_convert"
  48.         android:layout_height="wrap_content"
  49.         android:layout_width="wrap_content"
  50.         android:layout_centerInParent="true"
  51.         android:layout_below="@id/et_inches"
  52.         android:text="Convert"/>
  53.  
  54.     <Button
  55.         android:id="@+id/btn_clear"
  56.         android:layout_width="wrap_content"
  57.         android:layout_height="wrap_content"
  58.         android:layout_centerInParent="true"
  59.         android:layout_below="@+id/btn_convert"
  60.         android:text="Clear"/>
  61.  
  62. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.razormist.simpleconverter">
  4.  
  5. <application
  6.     android:allowBackup="true"
  7.     android:icon="@mipmap/ic_launcher"
  8.     android:label="@string/app_name"
  9.     android:roundIcon="@mipmap/ic_launcher_round"
  10.     android:supportsRtl="true"
  11.     android:theme="@style/AppTheme">
  12.     <activity android:name=".MainActivity"
  13.         android:configChanges="orientation"
  14.         android:screenOrientation="portrait">
  15.         <intent-filter>
  16.             <action android:name="android.intent.action.MAIN" />
  17.  
  18.             <category android:name="android.intent.category.LAUNCHER" />
  19.         </intent-filter>
  20.     </activity>
  21. </application>
  22. </manifest>

The Main Function

This code contains the main function of the application.This code will automatically calculate the values of each Edit Text when Convert button is click.
  1. package com.razormist.simpleconverter;
  2.  
  3. import android.os.Debug;
  4. import android.provider.Settings;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14.  
  15.     EditText et_foot, et_inches;
  16.     Button btn_convert, btn_clear;
  17.  
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22.  
  23.         et_foot = (EditText)findViewById(R.id.et_foot);
  24.         et_inches = (EditText)findViewById(R.id.et_inches);
  25.         btn_convert = (Button)findViewById(R.id.btn_convert);
  26.         btn_clear = (Button)findViewById(R.id.btn_clear);
  27.  
  28.         btn_convert.setOnClickListener(new View.OnClickListener() {
  29.             @Override
  30.             public void onClick(View v) {
  31.                     String strCentimeter = et_foot.getText().toString();
  32.                     String strInches = et_inches.getText().toString();
  33.  
  34.                     if(!strCentimeter.equals("") && strInches.equals("")) {
  35.                         double foot = Double.valueOf(et_foot.getText().toString());
  36.                         double inches = foot * 12;
  37.                         et_inches.setText(String.valueOf(inches));
  38.  
  39.                     }else if (!strInches.equals("") && strCentimeter.equals("")){
  40.                         double inches = Double.valueOf(et_inches.getText().toString());
  41.                         double foot = inches * 0.0833333;
  42.                         et_foot.setText(String.valueOf(foot));
  43.  
  44.                     }else if(!strCentimeter.equals("") && !strInches.equals("")){
  45.                         Toast.makeText(getApplicationContext(), "Please clear the text field first!", Toast.LENGTH_SHORT).show();
  46.                     }
  47.             }
  48.         });
  49.  
  50.         btn_clear.setOnClickListener(new View.OnClickListener() {
  51.             @Override
  52.             public void onClick(View v) {
  53.                 et_foot.setText("");
  54.                 et_inches.setText("");
  55.             }
  56.         });
  57.  
  58.     }
  59. }
There you have it we successfully created a Simple Convert using Android. I hope that this simple tutorial help you to learn android in a basic way. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Tags

Add new comment