In this tutorial, we will learn to create a "Hello User" application. Users can enter a text into the textbox, then click the button. The application will get the value from the textbox and display the hello message on the screen. The application will also print out the current time and date.
Object-Oriented programming is the most current way of programming. The concept of OO is now applied to not only programming, but also database system, interactive interface, application structure, and various areas. This tutorial will help students to understand how OOP works, how convenient the OOP is! Students will easily start to get into the concept of OO programming and use it for future programming.
ü Java JDK, JRE 1.7 or newer
ü Android Studio 2.2.3
Chose "File ----> New Project"
Create a new project by clicking on "Empty Activity"
We give the first project name "HelloWorld", keep the settings default.
After clicking on the "Finish" button, we will get this interface, then from drop down menu click "Run" and then "Run 'app' "
After run the first interface shall look like this.
After step four we have to //Copy and paste the following code in 'MainActivity.java' and 'activity_main.xml'
After run the preset code, the first interface shall look like this.
Save and run the project on the AVD, we will get this interface
Enter your name into the textbox, click the “Submit” button, the result will be like this interface.
MainActivity.java
package com.example.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
Button btnGreeting;
TextView txtGreeting;
EditText editTextName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGreeting = (Button) findViewById(R.id.btnGreeting);
txtGreeting = (TextView) findViewById(R.id.txtGreeting);
editTextName = (EditText) findViewById(R.id.editTextTextPersonName);
btnGreeting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String currentDateTimeString = DateFormat.getDateInstance().format(new Date());
String name = editTextName.getText().toString();
txtGreeting.setText("Hello " + name + "!\n"+ "Today is: " + currentDateTimeString);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_marginLeft="10dp"
tools:context=".MainActivity"
android:layout_marginStart="10dp">
<TextView
android:id="@+id/txtGreeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginStart="10dp"
android:text="Hello World!"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnGreeting"
android:layout_marginLeft="10dp" />
<Button
android:id="@+id/btnGreeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Greeting"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Please enter you name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
After run the code, we will get this interface
Entering the data shall result like this interface