Android Development: Toast, Sum, and Date/Time

Android Development: Three Practical Examples

Example 1: Displaying a “Hello World!” Toast Message

This example demonstrates how to display a simple “Hello World!” message using the Toast class in Android.

package com.example.program1;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void fun(View v) {
        Toast.makeText(this, "Hello World!", Toast.LENGTH_LONG).show();
    }
}

Example 2: Calculating the Sum of Two Numbers

This example shows how to take two numbers as input from EditText fields and calculate their sum, displaying the result in a Toast message.

package com.example.program2;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
    EditText e1, e2;
    int num1, num2, sum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1 = (EditText) findViewById(R.id.editText1);
        e2 = (EditText) findViewById(R.id.editText2);
    }
    public void sum(View v) {
        num1 = Integer.parseInt(e1.getText().toString());
        num2 = Integer.parseInt(e2.getText().toString());
        sum = num1 + num2;
        Toast.makeText(this, "Sum is: " + sum, Toast.LENGTH_LONG).show();
    }
}

Example 3: Selecting Date and Time

This example demonstrates how to use DatePickerDialog and TimePickerDialog to allow the user to select a date and time, and then display the selected date and time in a Toast message.

package com.example.program3;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
    int month, day, year, minute, hour;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void date(View v) {
        Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        Toast.makeText(getApplicationContext(), dayOfMonth + "-" + (monthOfYear + 1)
                                + "-" + year, Toast.LENGTH_LONG).show();
                    }
                }, year, month, day);
        datePickerDialog.show();
    }
    public void time(View v) {
        Calendar c = Calendar.getInstance();
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);
        TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay,
                                          int minutes) {
                        Toast.makeText(getApplicationContext(), hourOfDay + ":"
                                + minutes, Toast.LENGTH_LONG).show();
                    }
                }, hour, minute, false);
        timePickerDialog.show();
    }
}