In this tutorial we will be using Handler, which is a very useful class in Android that allows to display a cool animation and also perform various operations where tasks scheduling is required. It also has many other capabilities but for now we will just concentrate on the task scheduling in a very creative way.
In this example we will be creating a slide show app where the user can see the slide show of photos in forward as well as backward. For forward and backward slide show we have just set a boolean in onUserInteraction() method.
For understanding onUserInteraction() method, just hit this link.
Please note: The images are taken from Pixabay.
-----------------------The xml file's content --------------------------
----------------The Java file's code as follows------------------
---------------------------- Just declare the Activity name in your Manifest file --------------------
<?xml version="1.0" encoding="utf-8"?>-------------------------------------------------------------------------
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1c6d8d">
<ImageView
android:id="@+id/bbd_imgIV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/i1"/>
</LinearLayout>
----------------The Java file's code as follows------------------
package com.app.yourappname;------------------------------------------------------------------
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class HandlerSlideShowActivity extends AppCompatActivity {
private boolean forwardSlideShow = true;
private Handler imgHandler = new Handler();
private ImageView imgIV;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.handler_slideshow_activity);
imgIV = findViewById(R.id.bbd_imgIV);
}
private Runnable mUpdateTimeTask = new Runnable() {
int imgs[] = {R.drawable.i0, R.drawable.i1, R.drawable.i2, R.drawable.i3, R.drawable.i4};
int no = 0;
public void run() {
if(forwardSlideShow)
no++;
else
no--;
if(no>imgs.length-1)
no = 0;
if(no<0)
no = imgs.length-1;
if(imgIV!=null)
imgIV.setImageResource(imgs[no]);
imgHandler.postDelayed(mUpdateTimeTask, ( 2 * 1000)) ;
}
};
@Override
public void onUserInteraction() {
super.onUserInteraction();
if(forwardSlideShow) forwardSlideShow = false;
else forwardSlideShow = true;
}
@Override
protected void onResume() {
super.onResume();
imgHandler.removeCallbacks(mUpdateTimeTask);
imgHandler.postDelayed(mUpdateTimeTask, 500);
}
}
---------------------------- Just declare the Activity name in your Manifest file --------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">
<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=".HandlerSlideShowActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment