An Activity I have makes an API call, while it is making this call the activity displays ProgressDialog.
My problem is that while the ProgressDialog is being displayed the page is still clickable, and if the user clicks on a clickable object during this, it crashes the app.
How can I disable the onClick functions of the activity while the ProgressDialog is running?
EDIT: I would like to do this without removing the ability to cancel the loading.
try using this
progressDialog.setcancelable(false);
try this for Your comment
You are trying to set just the Property of Activity. Instead Bring up
a dialog using ProgressDialog, so that Ui will not be accessible
ProgressDialog dialog = ProgressDialog.show(this, "", "Please wait...", true);
Edit: If you do not want to use ProgressDialog, get the Root Layout and call
layout.setEnabled(false)
Hope It Helps U
dialogue.setCanceledOnTouchOutside(false)
Related
I have an activity in my app, where I want the user to pick the date from a DatePicker, which is contained in an AlertDialog. In the AlertDialog, I have set a view, to an xml layout file (, which contains only a LinearLayout, with a single DatePicker.
The code is really simple, and looks like this, just below onCreate().
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(R.layout.activity_alertdialog_date);
DatePicker datePicker = (DatePicker) findViewById(R.id.Activity_AlertDialog_SetStartDate);
// ... The rest of the AlertDialog, with buttons and all that stuff
alert.create().show()
The layout shows up in the AlertDialog, and that part works great.
However, when I try to add this line, I get a null object reference error.
datePicker.setMinDate(System.currentTimeMillis() - 1000);
Here is the error message.
Attempt to invoke virtual method 'void
android.widget.DatePicker.setMinDate(long)' on a null object reference
How can I fix this, or improve my code in another way?
I really appreciate all the help I can get. Thanks!
Your problem is that your findViewById is looking in the wrong place for the DatePicker view. Calling findViewById in an activity will call it on the Activity's layout hierarchy, not the dialog's layout. You need to inflate the layout for the alert dialog first, and then get a reference to the view. This can be acheived in a couple ways.
Probably the easiest is to inflate the view and get the reference before showing the dialog:
View dialogView = LayoutInflater.from(this).inflate(R.layout.activity_alertdialog_date, false);
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.Activity_AlertDialog_SetStartDate);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(dialogView);
// ... The rest of the AlertDialog, with buttons and all that stuff
alert.create().show();
You could also get the view from the alert dialog after it's been created:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(R.id.Activity_AlertDialog_SetStartDate);
// ... The rest of the AlertDialog, with buttons and all that stuff
AlertDialog dialog = alert.create();
dialog.show();
DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.Activity_AlertDialog_SetStartDate);
I am showing a list with messages and each message's row has a comment button.When i click on comment button opens a comment box with edit text and button for submitting the comment.When comment box appears on screen keypad also appears for entering text.If i pressed home button before entering text then application goes background but keypad remains on screen.This is the thing irritating me.For custom list i am using a custom adapter and code for comment box is written in that adapter.I tried using
inputmgr.hideSoftInputFromWindow(txtComments.getWindowToken(), 0);
but it is not working. So how i can hide this keypad programmatically.
Try using the code in https://stackoverflow.com/a/1109108/1904479. Hope you are not testing it in Android version 4.1.
please use this method to hide soft keyboard.
public static void hideSoftKeyboard(Activity context) {
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null)
inputManager.hideSoftInputFromWindow(context.getWindow().getDecorView().getApplicationWindowToken(), 0);
context.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
KeyEvent.KEYCODE_HOME can NOT be intercepted. You can hide the keypad inputmgr.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0); in onStop() method of your activity.
It does not require token from focused view editText.
I tried to make a dialog on startup of my application like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Hi Stackoverflow!").create().show();
}
But I want to make it so that if I click a checkbox in the dialog "Don't show again" then the second time I start the application the dialog doesn't appear. How can I do it?
If you just want a Dialog with a Button then you don't really need an AlertDialog. You can simply create a Dialog, create a layout for the Dialog in xml with the CheckBox and Button, then use setContentView() on the Dialog.
To not show the Dialog again simply create a boolean variable and use onCheckedChanged() to set that variable to true if the checkbox is checked. Save that in SharedPreferences and check that value on start up.
Good example of getting started with SharedPreferences
SharedPreferences complete docs
You can use SharedPreferences for this. Add a flag that tells you if the dialog has been shown before (or if the app has been launched before) and determine from there whether to show the dialog or not.
Hi I have an alert dialog with multichoice check box & i want to set one select all checkbox so that by clicking this SELECTALL CHECKBOX all items(i.e checkbox ) in the alert dialog list will automatically select programmatically.
Kindly help me.Thanks in advance.
You can read my answer here or example here or continue reading...
You need to setMultiChoiceItems(R.array.items, null, null) on AlertDialog.Builder and after that, get the ListView from an onCreated and shown dialog and setOnItemClickListener with little logic and you are done. Check my example here
I have an activity with some imageButtons in it. After I click on them i use setVisible(View.INVISIBLE); to have them gone. Now, when a user enter correct answer, a popup screen pops up with some info and OK button. I need to set all my imageButtons to be invisible when that popup window close. I tried to make some method:
private void removeImages(){
b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
b6.setVisibility(View.INVISIBLE);
b7.setVisibility(View.INVISIBLE);
}
and then call it onResume:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
removeImages();
}
But it does not work, it removes all my imageButtons as soon as I start that activity. How to do that after my popup windows closes, after I press OK button on that popup?
As per the Activity Lifecycle, onResume() is called before the Actviivty is in the foreground. You have a couple different options. You can use startActviityForResult() when you click an ImageButtonand check that value in onActivityResult() to set the Views how you wish. Or you could save a value in SharedPreferences to tell the Activity which Views to set invisible/visible in onResume()