Progress Dialog launches another AlertDialog - android

I want to show another alert message(alert box/alert dialog) after the progress bar reaches 100%. How do I do that?
And Also is there any way to style that box (or both of them)?
Below is my code for an ProgressBar:
public class MainActivity extends Activity {
Button progress_button;
ProgressDialog pro_dialog;
Handler pro_handler;
int progress;
private static final int MAX_PROGRESS = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
// intiliazing the buttons
progress_button = (Button) findViewById(R.id.button1);
progress_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Creating progress dialog interface setting
// title,progressstyle,max_progress
pro_dialog = new ProgressDialog(MainActivity.this);
pro_dialog.setTitle("Making everything OK is in progress! Please be patient.");
pro_dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pro_dialog.setMax(MAX_PROGRESS);
progress = 0;
pro_dialog.show();
pro_dialog.setProgress(0);
pro_handler.sendEmptyMessage(0);
}
});
// set onclick listener for buttons
pro_handler = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (progress >= MAX_PROGRESS) {
pro_dialog.dismiss();
} else {
progress++;
pro_dialog.incrementProgressBy(2);
pro_handler.sendEmptyMessageDelayed(0, 100);
}
}
};
}

First of all, it would make more sense if you could replace:
pro_dialog.incrementProgressBy(2);
with this:
pro_dialog.setProgress(progress);
Current code dismisses dialog after progress variable reaches MAX_PROGRESS, but current value of this variable isn't presented by the progress dialog.
If you would like to show AlertDialog, you can do it for example after "pro_dialog.dismiss()".
Case of Progress Dialog styling has been discused here.

Related

Run a function after dialog box is closed

I have a custom dialog box that I am displaying in the following way:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dict_add_word_dialog_box);
ok = findViewById(R.id.dictDialog_confirmButton);
cancel = (Button) findViewById(R.id.dictDialog_cancelButton);
ok.setOnClickListener(this);
cancel.setOnClickListener(this);
}
This displays when tapping the Floating Action Button, via:
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DictCustomDialogBoxClass customDialog = new DictCustomDialogBoxClass(DictionaryActivity.this);
customDialog.show();
refreshRecyclerView();
}
});
I'd like the refreshRecyclerView(); to run only once the user has pressed the OK button on the dialog box. How would I go about doing this?
In addition, how would I go about running it only if the user has pressed OK, and not Cancel?
Create a runnable with your refreshRecyclerView method in it:
Runnable r = new Runnable() {
#Override
public void run() {
refreshRecyclerView();
}
}
then create a handler for that runnable:
Handler handler = new Handler();
inside your onClickListener for the ok button trigger the runnable by calling the following:
handler.post(r);
Why not add a listener to your custom dialog?
var listener: Listener
public interface Listener {
void onPositiveActionPressed();
}
Then in your custom dialog's ok.onClickListener you'd have the below;
if(listener != null) {
listener.onPositiveActionPressed();
}
Finally;
DictCustomDialogBoxClass customDialog = new DictCustomDialogBoxClass(DictionaryActivity.this)
customDialog.listener = self
Of course having implemented DictCustomDialogBoxClass.Listener

Button OnClick action initiated after 6 seconds in android

I have 5 buttons in my activity inside a linear layout.
I have written a code to display a toast message when I click one of those buttons.
It is displaying the toast message after 6 seconds of Click action.
I couldn't think what the problem could be..
Here is the code I have written in android studio
public class HomePage extends AppCompatActivity implements View.OnClickListener {
private Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
makeNotificationBarTransparent();
loginButton = (Button)findViewById(R.id.login_btn);
loginButton.setOnClickListener(this);
Intent i = getIntent();
Toast.makeText(getApplicationContext(),i.getStringExtra("UserName"),Toast.LENGTH_LONG).show();
}
private void makeNotificationBarTransparent() {
//Making notification bar transparent
if(Build.VERSION.SDK_INT >= 21){
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.login_btn:
Toast.makeText(getApplicationContext(),"CLicked",Toast.LENGTH_LONG).show();
break;
}
}
}
Can anyone tell me What could be the problem?
It might be the fact that you show another toast message that you set to show up, as two toast messages cannot display at once.
I am referring to this toast message:
Toast.makeText(getApplicationContext(),i.getStringExtra("UserName"),Toast.LENGTH_LONG).show();
You mean the Toast message will be show after clicking the button for 6 seconds?
Try to use this:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// show toast here...
}
}, 6000); // 6 seconds
Or you can use CountDownTimer:
new CountDownTimer(6000, 1000) {
public void onTick(long millisUntilFinished) {
// do every 1 second
}
public void onFinish() {
// show your toast after 6 seconds.
}
}.start();
Is this what you need? If not, tell me more about your problems :)

how help screen show for one time only in android

in the following code work properly and show help screen when open activity but I want show one time forever,
what can i do?
What should I add in the code?
my code:
public class KhatmMain extends Activity implements OnClickListener{
Context ctx;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.khatmmain);
showOverLay();
.
.
.
}
private void showOverLay(){
final Dialog dialog = new Dialog(ctx, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.overlay_view);
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.overlayLayout);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
}
}
You can use SharedPrefereces to set a variable that will check if you've shown the dialog yet to the user or not, here's an example:
SharedPreferences prefs = this.getSharedPreferences("com.you.app", Context.MODE_PRIVATE);
Boolean dialogShown = prefs.getBoolean("dialogShown", false);
Then check if the value of dialogShown is false (you don't need to set it first since it will default to false the way we are calling it), then on the following code we execute some code, only if dialogShown is false, meaning we can do all the dialog stuff inside that conditional:
if(!dialogShown){
//Your show dialog code
prefs.edit().putBoolean("dialogShown",true).commit();
}
So the next time we check for the dialogShown value on the shared preferences it will be true therefor not showing the dialog. I believe this is the most common way of doing it.
There is a solution ..
when application first time start then save the shared preference to the app..
Now each and every time You retrieve the shared preference and check if it is there then move to next screen
Use this code:
public class KhatmMain extends Activity implements OnClickListener{
Context ctx;
Boolean showOneTime = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
setContentView(R.layout.khatmmain);
showOverLay();
.
.
.
}
private void showOverLay(){
if (showOneTime == false) {
return;
}
final Dialog dialog = new Dialog(ctx, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.overlay_view);
LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.overlayLayout);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
showOneTime = false;
}
}

Android Dialog dismisses instead of cancel

I'm having the following issue developing in android 2.2 (API 8):
I have a customized Dialog class like this:
public AuthDialog(final Context context, OnDismissListener dismissListener, OnCancelListener cancelListener) {
super(context);
setOnDismissListener(dismissListener);
setOnCancelListener(cancelListener);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userpassdialog);
setTitle("Enter email and password");
setCancelable(true);
setCanceledOnTouchOutside(true);
authEmail = (EditText) findViewById(R.id.authEmail);
authPass = (EditText) findViewById(R.id.authPass);
alertMessage = (TextView) findViewById(R.id.auth_alert);
Button authButton = (Button) findViewById(R.id.authButton);
View.OnClickListener onClickListener = new View.OnClickListener() {
public void onClick(View v) {
if (checkCredentials())
dismiss();
else
showAlert();
}
};
authButton.setOnClickListener(onClickListener);
}
private void showAlert() {
alertMessage.setText("Wrong user/pass");
authEmail.setText(null);
authPass.setText(null);
}
private boolean checkCredentials() {
// Empty user/pass for now
boolean checkEmail = authEmail.getText().toString().equals("");
boolean checkPassword = authPass.getText().toString().equals("");
return checkEmail && checkPassword;
}
#Override
public void onBackPressed() {
cancel();
}
And I create a new AuthDialog like this:
private void authenticateThenAccept() {
OnDismissListener dismissListener = new OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
accept();
}
};
OnCancelListener cancelListener = new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
cancel();
}
};
AuthDialog dialog = new AuthDialog(context, dismissListener, cancelListener);
dialog.show();
}
I'm using the debugger, and I see that when I cancel (using the back button or pressing outside the dialog) the app dismisses the dialog instead of cancelling.
Anybody has had this kind of issue with Dialogs?
Thanks in advanced.
onDismiss() is always fired when dialog closes. The documentation for setOnCancelListener() states: "This will only be invoked when the dialog is canceled, if the creator needs to know when it is dismissed in general, use setOnDismissListener", i.e. it's not either onCancel or onDismiss but both when a dialog is canceled. I agree though that it would have made more sense had that not been the case.
Assuming this dialog should be modal, make your dialog a new activity.
setCancelable(false) will prevent the back button from doing anything. Many developers just turn off the ability of the back button to close the dialog since it's unclear whether that is a cancel or ok action to the user.

ProgressDialog not showing up in activity

I am trying to include a ProgressDialog in my application. But it is not showing up.
Here's the code snippet where i use the ProgressDialog:
public class abcActivity extends Activity {
public boolean onOptionsItemSelected(MenuItem item) {
case XYZ:
ProgressDialog dialog = ProgressDialog.show(abcActivity.this, "", "Please wait for few seconds...", true);
callSomeFunction();
dialog.dismiss();
showToast(getString(R.string.SomeString));
break;
}
}
Does anyone know why the dialog is not showing up? Any clues?
I think your code is wrong in a sense that you do all in the UI thread. You have to put callsomefunction() into a background thread.
public void runSomething()
{
showDialog(BACKGROUND_ID);
Thread t = new Thread(new Runnable()
{
public void run()
{
//do something
handler.post(finishThread);
}
});
t.start();
// The progress wheel will only show up once all code coming here has been executed
}
And as well
protected Dialog onCreateDialog(int id)
{
if(progressDialog == null) progressDialog = new ProgressDialog(this);
return progressDialog;
}
#Override
protected void onPrepareDialog(int id, Dialog dialog)
{
if(id == BACKGROUND_ID)
{
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.setMessage("running for long ...");
}
}
Runnable finishThread = new Runnable()
{
public void run()
{
//long running
if(progressDialog != null) progressDialog.dismiss();
}
};
i think issue is in your switch condition pls verify it.
here is another method to display dialog in android try this.
public static final int DIALOG2_KEY = 1;
public static ProgressDialog dialog1;
showDialog(DIALOG2_KEY); // use it where you want to display dialog
dialog1.cancel(); // use it to cancel the dialog
#Override
public Dialog onCreateDialog(int id) {
if (id == 1) {
dialog1 = new ProgressDialog(this);
dialog1.setMessage("Please wait...");
dialog1.setIndeterminate(true);
dialog1.setCancelable(true);
}
return dialog1;
}
Ensure you have this code called by debugging or adding a log. The code seems right to me.
Also, if you want to perform some operations in background and show a progress dialog while the performing, please use AsyncTask with ProgressDialog bounded, like here.
ProgressDialog is deprecated in Android O , Use ProgressBar now.

Categories

Resources