Since the ProgressDialog is deprecated from the Android version O, I'm still finding a better way out to do my task. The task is to move from my activity to the fragment. Everything is working fine but the progressdialog is not visible. I've tried implementing it but... the progressdialog doesn't work.
It seems the progressbar would work but still not working. I need a progressdialog because it is simply easy for me to set my title and the message. I need a spinner progressDialog but don't know how to do it. Here is one of my work but not implementing :
Java Class
ublic class SaveVideo extends AppCompatActivity {
private Button button;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save_video);
mProgressDialog = new ProgressDialog(this);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
button = (Button) findViewById(R.id.saveVideo);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//where it must be seen when the button is pressed
mProgressDialog.setTitle("Title");
mProgressDialog.setMessage("Message");
mProgressDialog.show();
Intent intent = new Intent(SaveVideo.this,MainActivity.class);
intent.putExtra("change",2);
startActivity(intent);
//as soon as the page moves from this to another fragment
mProgressDialog.dismiss();
}
});
}
I'm new to Android Version O. Any help would give me new thing to learn!
This class was deprecated in API level 26. ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI.
In order to do this, you need to instantiate an object of this class. Its syntax is. ProgressDialog progress = new ProgressDialog(this); Now you can set some properties of this dialog.
Step by Step Implementation We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project. Navigate to the app > res > layout > activity_main. xml and add the below code to that file. Below is the code for the activity_main.
As it is mentioned in Android O documentation:
This class was deprecated in API level 26. ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar, which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.
You can create a custom view with TextView and ProgressBar and manage its visibilty. You can use this library also because it is using AlertDialog instead of ProgressDialog.
ProgressBar is very simple and easy to use, first step is that you can make xml layout of the dialog that you want to show, let say we name this layout
layout_loading_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp">
<ProgressBar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="center"
android:text="Please wait! This may take a moment." />
</LinearLayout>
next step is create AlertDialog which will show this layout with ProgressBar
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false); // if you want user to wait for some process to finish,
builder.setView(R.layout.layout_loading_dialog);
AlertDialog dialog = builder.create();
now all that is left is to show and hide this dialog in our click events like this
progress_dialog.show(); // to show this dialog
progress_dialog.dismiss(); // to hide this dialog
and thats it, it should work, as you can see it is farely simple and easy to implement ProgressBar (like ProgressDialog) instead of deprecated ProgressDialog. now you can show/dismiss this dialog box in either Handler or ASyncTask, its up to your need, hope you can use this to solve your problems, cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With