Monday, 25 December 2017

Create an application that will display toast (Message) on specific interval of time

Create an application that will display toast (Message) on specific interval of time



Code:

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jayesh.chronometer">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

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="com.jayesh.chronometer.MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium" />

    <Chronometer
        android:id="@+id/cnm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:format="Timer : %s"/>

</RelativeLayout>

MainActivity.java

package com.jayesh.chronometer;

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

import static android.R.attr.duration;

public class MainActivity extends AppCompatActivity {

    Chronometer c;
    int i=0;
    int duration=10;
    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 arg0) {

                tv.setText("Meaasge will be displayed after " + (duration - (i + 1)) + " seconds");
                i++;
                if (i >= duration)
                {
Toast.makeText(getApplicationContext(),"Message"+(i/10),   Toast.LENGTH_LONG).show();
                    duration=duration+10;
                }

            }
        });
    }

}



OUTPUT:



No comments:

Post a Comment