• We just launched and are currently in beta. Join us as we build and grow the community.

Android - Simple Converter For Beginners

gabykh

De-Anonymization Specialist
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
28
Likes
68
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
Operating System

Android

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. <

    TextView
  9. android:

    id=

    "@+id/tv_foot"
  10. android:

    layout_width=

    "wrap_content"
  11. android:

    layout_height=

    "wrap_content"
  12. android:

    text=

    "Foot"
  13. android:

    textSize=

    "20sp"
  14. android:

    layout_alignBaseline=

    "@+id/et_foot"
  15. android:

    layout_toLeftOf=

    "@+id/et_inches"

    />

  16. <

    EditText
  17. android:

    id=

    "@+id/et_foot"
  18. android:

    layout_height=

    "wrap_content"
  19. android:

    layout_width=

    "match_parent"
  20. android:

    layout_centerInParent=

    "true"
  21. android:

    layout_alignParentTop=

    "true"
  22. android:

    layout_marginTop=

    "100dp"
  23. android:

    layout_toRightOf=

    "@+id/tv_foot"
  24. android:

    inputType=

    "number"

    />

  25. <

    TextView
  26. android:

    id=

    "@+id/tv_inches"
  27. android:

    layout_height=

    "wrap_content"
  28. android:

    layout_width=

    "wrap_content"
  29. android:

    text=

    "Inches"
  30. android:

    textSize=

    "20sp"
  31. android:

    layout_alignBaseline=

    "@+id/et_inches"
  32. android:

    layout_alignBottom=

    "@+id/et_inches"

    />

  33. <

    EditText
  34. android:

    id=

    "@+id/et_inches"
  35. android:

    layout_height=

    "wrap_content"
  36. android:

    layout_width=

    "match_parent"
  37. android:

    layout_centerInParent=

    "true"
  38. android:

    layout_below=

    "@+id/et_foot"
  39. android:

    layout_toRightOf=

    "@+id/tv_inches"
  40. android:

    inputType=

    "number"

    />

  41. <

    Button


  42. android:

    id=

    "@+id/btn_convert"
  43. android:

    layout_height=

    "wrap_content"
  44. android:

    layout_width=

    "wrap_content"
  45. android:

    layout_centerInParent=

    "true"
  46. android:

    layout_below=

    "@id/et_inches"
  47. android:

    text=

    "Convert"

    />

  48. <

    Button


  49. android:

    id=

    "@+id/btn_clear"
  50. android:

    layout_width=

    "wrap_content"
  51. android:

    layout_height=

    "wrap_content"
  52. android:

    layout_centerInParent=

    "true"
  53. android:

    layout_below=

    "@+id/btn_convert"
  54. android:

    text=

    "Clear"

    />

  55. </

    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. <

    application
  5. android:

    allowBackup=

    "true"
  6. android:

    icon=

    "@mipmap/ic_launcher"
  7. android:

    label=

    "@string/app_name"
  8. android:

    roundIcon=

    "@mipmap/ic_launcher_round"
  9. android:

    supportsRtl=

    "true"
  10. android:

    theme=

    "@Style/AppTheme"

    >
  11. <

    activity android:

    name=

    ".MainActivity"
  12. android:

    configChanges=

    "orientation"
  13. android:

    screenOrientation=

    "portrait"

    >
  14. <

    intent-

    filter>
  15. <

    action android:

    name=

    "android.intent.action.MAIN"

    />

  16. <

    category android:

    name=

    "android.intent.category.LAUNCHER"

    />
  17. </

    intent-

    filter>
  18. </

    activity>
  19. </

    application>
  20. </

    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. import

    android.os.Debug

    ;
  3. import

    android.provider.Settings

    ;
  4. import

    android.support.v7.app.AppCompatActivity

    ;
  5. import

    android.os.Bundle

    ;
  6. import

    android.util.Log

    ;
  7. import

    android.view.View

    ;
  8. import

    android.widget.Button

    ;
  9. import

    android.widget.EditText

    ;
  10. import

    android.widget.Toast

    ;

  11. public

    class

    MainActivity extends

    AppCompatActivity {

  12. EditText et_foot, et_inches;
  13. Button

    btn_convert, btn_clear;

  14. @Override
  15. protected

    void

    onCreate(

    Bundle savedInstanceState)

    {
  16. super

    .onCreate

    (

    savedInstanceState)

    ;
  17. setContentView(

    R.layout

    .activity_main

    )

    ;

  18. et_foot =

    (

    EditText)

    findViewById(

    R.id

    .et_foot

    )

    ;
  19. et_inches =

    (

    EditText)

    findViewById(

    R.id

    .et_inches

    )

    ;
  20. btn_convert =

    (

    Button

    )

    findViewById(

    R.id

    .btn_convert

    )

    ;
  21. btn_clear =

    (

    Button

    )

    findViewById(

    R.id

    .btn_clear

    )

    ;

  22. btn_convert.setOnClickListener

    (

    new

    View

    .OnClickListener

    (

    )

    {
  23. @Override
  24. public

    void

    onClick(

    View

    v)

    {
  25. String

    strCentimeter =

    et_foot.getText

    (

    )

    .toString

    (

    )

    ;
  26. String

    strInches =

    et_inches.getText

    (

    )

    .toString

    (

    )

    ;

  27. if

    (

    !

    strCentimeter.equals

    (

    ""

    )

    &&

    strInches.equals

    (

    ""

    )

    )

    {
  28. double

    foot =

    Double

    .valueOf

    (

    et_foot.getText

    (

    )

    .toString

    (

    )

    )

    ;
  29. double

    inches =

    foot *

    12

    ;
  30. et_inches.setText

    (

    String

    .valueOf

    (

    inches)

    )

    ;

  31. }

    else

    if

    (

    !

    strInches.equals

    (

    ""

    )

    &&

    strCentimeter.equals

    (

    ""

    )

    )

    {
  32. double

    inches =

    Double

    .valueOf

    (

    et_inches.getText

    (

    )

    .toString

    (

    )

    )

    ;
  33. double

    foot =

    inches *

    0.0833333

    ;
  34. et_foot.setText

    (

    String

    .valueOf

    (

    foot)

    )

    ;

  35. }

    else

    if

    (

    !

    strCentimeter.equals

    (

    ""

    )

    &&

    !

    strInches.equals

    (

    ""

    )

    )

    {
  36. Toast.makeText

    (

    getApplicationContext(

    )

    , "Please clear the text field first!"

    , Toast.LENGTH_SHORT

    )

    .show

    (

    )

    ;
  37. }
  38. }
  39. }

    )

    ;

  40. btn_clear.setOnClickListener

    (

    new

    View

    .OnClickListener

    (

    )

    {
  41. @Override
  42. public

    void

    onClick(

    View

    v)

    {
  43. et_foot.setText

    (

    ""

    )

    ;
  44. et_inches.setText

    (

    ""

    )

    ;
  45. }
  46. }

    )

    ;

  47. }
  48. }

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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

437,153

314,794

314,803

Top