Android - Simple Converter For Beginners

Submitted by: 
Operating System: 
Visitors have accessed this post 603 times.

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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simpleconverter.MainActivity">
  11.  
  12. <TextView
  13. android:id="@+id/tv_foot"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="Foot"
  17. android:textSize="20sp"
  18. android:layout_alignBaseline="@+id/et_foot"
  19. android:layout_toLeftOf="@+id/et_inches"/>
  20.  
  21. <EditText
  22. android:id="@+id/et_foot"
  23. android:layout_height="wrap_content"
  24. android:layout_width="match_parent"
  25. android:layout_centerInParent="true"
  26. android:layout_alignParentTop="true"
  27. android:layout_marginTop="100dp"
  28. android:layout_toRightOf="@+id/tv_foot"
  29. android:inputType="number"/>
  30.  
  31. <TextView
  32. android:id="@+id/tv_inches"
  33. android:layout_height="wrap_content"
  34. android:layout_width="wrap_content"
  35. android:text="Inches"
  36. android:textSize="20sp"
  37. android:layout_alignBaseline="@+id/et_inches"
  38. android:layout_alignBottom="@+id/et_inches"/>
  39.  
  40. <EditText
  41. android:id="@+id/et_inches"
  42. android:layout_height="wrap_content"
  43. android:layout_width="match_parent"
  44. android:layout_centerInParent="true"
  45. android:layout_below="@+id/et_foot"
  46. android:layout_toRightOf="@+id/tv_inches"
  47. android:inputType="number"/>
  48.  
  49. android:id="@+id/btn_convert"
  50. android:layout_height="wrap_content"
  51. android:layout_width="wrap_content"
  52. android:layout_centerInParent="true"
  53. android:layout_below="@id/et_inches"
  54. android:text="Convert"/>
  55.  
  56. android:id="@+id/btn_clear"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:layout_centerInParent="true"
  60. android:layout_below="@+id/btn_convert"
  61. android:text="Clear"/>
  62.  
  63. </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="<a href="http://schemas.android.com/apk/res/android"
  3. package="com.razormist.simpleconverter">
  4.  
  5. <application
  6. " rel="nofollow">http://schemas.android.com/apk/res/android"
  7. package="com.razormist.simpl...</a> android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </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!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.