Display toast: Message on the specific interval of Time

display toast: message on a specific time of interval using a chronometer

Create an application that will display toast (Message) on the specific interval of Time.

Steps Description
1  create a project in android studio with any name.
2 Modify src/MainActivity.java and add the code
3 Modify layout XML file res/layout/activity_main.xml

Also, Check –> Simple Hello World Application in android

Content of src/MainActivity.java

package com.example.kenilvavaliya.timer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Chronometer;
import android.widget.Toast;

import java.util.Timer;

public class MainActivity extends AppCompatActivity {

private Chronometer timer;
int i=-1;
int duration=10;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer=(Chronometer)findViewById(R.id.chronometer);
timer.start();
timer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
i++;
if (i >= duration) {
Toast.makeText(MainActivity.this, "Message" + (i / 10), Toast.LENGTH_LONG).show();
duration = duration + 10;


}
}
});
}
}

Content of res/layout/activity_main.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">

<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp" />

</RelativeLayout>

Output:

The output of this application is shown in the image 

It will Toast the message at the time of 10 second

If you have to change then change in src/MainActivity.java in that change the value of I from -1 to your value for changing message change in “Toast” method.

Timer chronometer

Stay Connect with our app: –¬†

https://play.google.com/store/apps/details?id=com.edu.easyengineer

For Assignment Questions and Explanation of a theory topic visit:

http://mycandal.com/mobile-application-devlopmentseit2021/

About easyengineering 37 Articles
Easyengineering.in provides you to all subject and exam related materials online like GPSC, UPSC, IES, GATE, etc. As well as we provide daily job notification, some life-related books and other online courses which are useful too in your study.

2 Comments

Leave a Reply

Your email address will not be published.


*