You want to download a file from internet or load all the resources of your game or do any other task which will take some time, and simultaneously you also have to entertain the user. What will you do?
Here is the ProgressDialog class which will show a nice loading icon until your task gets completed.
This example shows three types of ProgressDialog, and further you can enhance as per your wish:
Here is the ProgressDialog class which will show a nice loading icon until your task gets completed.
This example shows three types of ProgressDialog, and further you can enhance as per your wish:
The Xml file loading_activity.xml:
<?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="#81D4FA"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:gravity="top"
android:weightSum="2">
<Button
android:id="@+id/spin_progressBTN"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#0288D1"
android:text="Spin"
android:onClick="spinProgressDialog"
android:textSize="15sp"
android:layout_marginRight="5dp"
android:textColor="#ffffff"/>
<Button
android:id="@+id/bar_progressBtn"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#4FC3F7"
android:text="Bar"
android:layout_marginLeft="5dp"
android:onClick="barProgressDialog"
android:textSize="15sp"/>
</LinearLayout>
<Button
android:layout_marginTop="5dp"
android:padding="5dp"
android:id="@+id/fancy_bar_progressBtn"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#0091EA"
android:text="Fancy Bar"
android:layout_marginLeft="5dp"
android:onClick="fancyBarProgressDialog"
android:textSize="15sp"
android:textColor="#ffffff"/>
</LinearLayout>
The fancy_progress_bar.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<gradient
android:startColor="#0277BD"
android:centerColor="#0b131e"
android:centerY="1.0"
android:endColor="#0d1522"
android:angle="270"
/>
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<gradient
android:startColor="#80D8FF"
android:centerColor="#0091EA"
android:centerY="1.0"
android:endColor="#06101d"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
The LoadingActivity.java:
package com.apps.yourappname;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class LoadingActivity extends AppCompatActivity {
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loading_activity);
}
public void spinProgressDialog(View v) {
mProgressDialog = new ProgressDialog(LoadingActivity.this);
mProgressDialog.setMessage("Simple Spin Progress Dialog");
doProgress();
}
public void barProgressDialog(View v) {
mProgressDialog = new ProgressDialog(LoadingActivity.this);
mProgressDialog.setMessage("Simple Bar Progress Dialog");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMax(100);
doProgress();
}
public void fancyBarProgressDialog(View v) {
mProgressDialog = new ProgressDialog(LoadingActivity.this);
mProgressDialog.setMessage("Fancy Bar Progress Dialog");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setProgressDrawable(getResources().getDrawable(R.drawable.fancy_progress_bar));
mProgressDialog.setMax(100);
doProgress();
}
void doProgress() {
mProgressDialog.show();
new Thread(){
@Override
public void run() {
while(mProgressDialog.getProgress()<mProgressDialog.getMax()) {
try{
Thread.sleep(300);
}catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendMessage(handler.obtainMessage());
if(mProgressDialog.getProgress()>=mProgressDialog.getMax()) {
mProgressDialog.dismiss();
}
}
}
}.start();
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
mProgressDialog.incrementProgressBy(1);
}
};
}
Just add the LoadingActivity's entry in 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=".LoadingActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No changes in the gradle files.
No comments:
Post a Comment