An android application that will display toast(Message) on specific interval of time. - android programming

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

Program:

AndroidManifest.xml

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

    <application
       
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="@string/app_name"
       
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:tools="http://schemas.android.com/tools"
   
android:id="@+id/activity_main"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:paddingBottom="@dimen/activity_vertical_margin"
   
android:paddingLeft="@dimen/activity_horizontal_margin"
   
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
tools:context="com.android.infiapps.p7.MainActivity"
>

    <TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Hello World!"
/>
</RelativeLayout>

MainActivity.java

package com.android.infiapps.p7;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    Timer t=new Timer();


    @Override
   
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Handler h=new Handler();
        final Runnable r=new Runnable() {
            @Override
           
public void run() {
                Toast.makeText(getApplicationContext(),"hello",Toast.LENGTH_SHORT).show();
            }
        };
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
           
public void run() {
                h.post(r);
            }
        },1000,5000);
    }

    protected void onDestroy(){
        Toast.makeText(this,"Finish",Toast.LENGTH_SHORT).show();
        t.cancel();
        t.purge();
    }
}

Output:

No comments :

Post a Comment