Create Age Calculator with Android Studio

Age Calculator is a simple Application to begin with Android Studio, when you want to learn something you need to choose a point to start from.

In order to do that start a new project in Android Studio, if you don’t know how please go HERE.

  • Then go to ” res > layout > activity_main.xml ” don’t worry ill explain everything.

Age-Calculator

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
                          xmlns:app=”http://schemas.android.com/apk/res-auto”
                         xmlns:tools=”http://schemas.android.com/tools”
                        android:layout_width=”match_parent
                       android:layout_height=”match_parent
                      tools:context=”.MainActivity“>
….
</RelativeLayout>

RelativeLayout : Our Container enables us to specify how child views are positioned relative to each other.

                           with height/width=match_parent means take height/width of parent view.

  • TextView : Simple Text ” Enter Birth Year ” to tell the user what information he should enter.

                          height/width=wrap_content mean take what you need.

                          margin Left/right space between this widget and the others.

                          CenterVertical : take Mid vertical position.

  • EditText : where the user will enter the “YEAR” with input type ” number ” only numbers accepted.

  • Another TextView with id for using this text in java class , layout_below : position below EditText.

  • The Button is where we will code the calculation we need the id for OnClickListener Function, you can also chose the text color and the background color if you want.

Now Go to app > java > “your package name” > MainActivity.

  • MainActivity is a java Class where we will do some coding.

As you can see we need to identify some widgets and give them names like ” TextView ” = type, textAge ” = New Name, so the widget from the layout with id ” age ” well be textAge on java class.

We have ” onCreate ” is a method that start when this application will be created thats why we need to put our code there and also ” setContentView(R.layout.activity_main) ” Basically what this function does is display the Layout created through XML or the Dynamically created layout view in the Screen.

findViewById is a method that finds the view from the layout resource file that are attached with current Activity.

calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (birthYear.getText().length() == 0 || birthYear.getText().equals(” “)) {
//if the field is empty Show the message ” Enter Your Birth Year”//
Toast.makeText(MainActivity.this, “Enter Your Birth Year”, Toast.LENGTH_LONG).show();
} else {            //Calculate
birthY = Integer.valueOf(birthYear.getText().toString());
calculator = 2019 – birthY;
newAge = “You Have ” + calculator + ” Years Old”;
textAge.setText(newAge);
birthYear.clearComposingText();
}
}
});

calculate.setOnClickListener : basically what could happen when you click the button ” Calculate “, this code will do the operation (2019 – yourage) and put the result into the text, example : “You Have 20 Years Old”.

Layout XML :

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>
<TextView
android:id=”@+id/enter”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:gravity=”center”
android:text=”Enter Birth Year :”
android:layout_alignParentLeft=”true”
android:layout_centerVertical=”true”
android:layout_marginLeft=”20dp”
android:layout_marginRight=”5dp”/>
<EditText
android:id=”@+id/year”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_toRightOf=”@+id/enter”
android:layout_centerVertical=”true”
android:hint=”Ex : 1989″
android:inputType=”number”/>
<TextView
android:id=”@+id/age”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”You Have 0 Years Old”
android:layout_centerHorizontal=”true”
android:layout_margin=”5dp”
android:layout_below=”@id/year”/>
<Button
android:id=”@+id/calculate”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Calculate”
android:background=”@color/colorB”
android:textColor=”@color/colorW”
android:layout_centerHorizontal=”true”
android:layout_below=”@id/age”
android:layout_margin=”5dp”/>
</RelativeLayout>

JAVA CLASS :

public class MainActivity extends AppCompatActivity {
    TextView textAge; // Text named textAge
    EditText birthYear; // input where we will take age from
    Button calculate;  // Button
    String newAge; // String like text
    int birthY; // int birth year : number
    int calculator; //int : number will calculate the age

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //Layout that we will use for this Activity
        // find the widgets on the layout by ID
        textAge = findViewById(R.id.age);
        birthYear = findViewById(R.id.year);
        calculate = findViewById(R.id.calculate);
        /// When you Click the Button ///
        calculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (birthYear.getText().length() == 0 || birthYear.getText().equals(" ")) {
                    //if the field is empty Show the message " Enter Your Birth Year"//
                    Toast.makeText(MainActivity.this, "Enter Your Birth Year", Toast.LENGTH_LONG).show();
                } else {//Calculate
                    birthY = Integer.valueOf(birthYear.getText().toString());
                    calculator = 2019 - birthY;
                    newAge = "You Have " + calculator + " Years Old";
                    textAge.setText(newAge);
                    birthYear.clearComposingText();
                }
            }
        });
    }
}

Android Dev

Android Developer.

One thought on “Create Age Calculator with Android Studio

Leave a Reply

Your email address will not be published. Required fields are marked *