Create an application that will display toast (Message) on specific interval of time.
MainActivity.java
MainActivity.java
package com.example.assignment8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Chronometer;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Chronometer c;
int i = 0;
int duration = 5;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c = (Chronometer) findViewById(R.id.cnm);
tv = (TextView) findViewById(R.id.tv);
c.start();
c.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
tv.setText("Message will be displayed after " + (duration - (i + 1)) + " seconds");
i++;
if (i >= duration)
{
Toast.makeText(getApplicationContext(),"Message"+(i/5), Toast.LENGTH_LONG).show();
duration = duration+5;
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
android:textColor="#0B0A0A"
android:textSize="24sp" />
<Chronometer
android:id="@+id/cnm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:format="Timer : %s"
android:textSize="24sp" />
</RelativeLayout>
No comments:
Post a Comment