Hi I am developing a android application using Kotlin, when one activity starts I like to put progress bar. For example when user click sign up button in the login page the progress bar will show in the login page, so for that I use AsyncTask. I define the function in anther file and call it it login page. It works well. But I like to put the same thing in other pages also, like when click forget password from login or click login from signup or whatever, so I think passing activity name will be make it easier.
Here my code.
class Loading : AppCompatActivity() {
companion object {
class MyTask(private val logInActivity: LogInActivity) : AsyncTask<Void, Void, Void>() {
//TODO: Have to fix the error, when back to login the loading is showing
override fun doInBackground(vararg unUsed: Void?): Void? {
return null
}
override fun onPreExecute() {
logInActivity.progressDialog.show()
}
override fun onPostExecute(result: Void?) {
var intent = Intent(logInActivity, SignUp::class.java)
logInActivity.startActivity(intent)
super.onPostExecute(result)
}
}
}
}
In this code, it's just setting the progress bar to the login activity only, how to password and activity name into the function and rather statically define loginActivity how can I replace it with passed activity names.
like,
<activityName>.progressDialog.show()
Create a abstract BaseActivity and extend in all your Activities.
Like this:
public abstract class BaseActivity extends AppCompatActivity
{
protected Context context;
private AlertDialog alertDialog;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
context = BaseActivity.this;
}
protected void showProgressDialog()
{
if (alertDialog == null)
{
alertDialog = new CustomDialog(context);
}
if (!alertDialog.isShowing())
{
alertDialog.show();
}
}
protected void hideProgressDialog()
{
if (alertDialog != null && alertDialog.isShowing())
{
alertDialog.dismiss();
}
}
protected void showToast(String message)
{
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
use showProgressDialog() and hideProgressDialog() to show and hide dialog.
this approach is much easy to use.
You can create a separate class or method to take context and message... then show a progress view... I have done it like this in one of my project...
class ProgressService(private val mContext: Context) {
private var mProgressDialog: Dialog? = null
override fun showProgressBar(message: String?) {
hideProgressBar()
mProgressDialog = Dialog(mContext, R.style.ProgressViewTheme)
val view: View =
(mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater)
.inflate(R.layout.progress_indicator, null)
val progressView: AVLoadingIndicatorView = view.findViewById(R.id.progressView)
val progressMessageTv = view.findViewById<TextView>(R.id.progressMessageTv)
progressMessageTv.text = message
progressView.show()
mProgressDialog?.setContentView(view)
mProgressDialog?.setCancelable(false)
try {
mProgressDialog?.show()
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun hideProgressBar() {
try {
if (mProgressDialog != null) {
mProgressDialog?.dismiss()
mProgressDialog = null
}
} catch (e: Exception) {
e.printStackTrace()
}
if (mProgressDialog != null && mProgressDialog!!.isShowing) {
mProgressDialog!!.dismiss()
}
}
}
Here is the code for progress_indicator. I used a third party library to show fancy loading animation....
<?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"
android:background="#88000000">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/round_white_background"
android:gravity="center"
android:orientation="vertical">
<com.wang.avi.AVLoadingIndicatorView
android:id="@+id/progressView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:indicatorColor="@color/colorPrimary"
app:indicatorName="LineScalePulseOutRapidIndicator" />
<TextView
android:id="@+id/progressMessageTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/colorPrimaryDark"
tools:text="TextView" />
</LinearLayout>
</RelativeLayout>
Then from where you want to call it...
val progressIndicator = ProgressService(this)
progressIndicator.showProgressBar("Please wait...")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.