Android Studio - DatePicker in AlertDialog, null object reference - java

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);

Related

how to load existing webview in alert dialog android

I have loaded a webview by default, with settings and url at the bottom of the page, i need to display that existing webview in an alert dialog box.
I don't want to create new webview since i need to maintain the state of the existing webview (i mean if i had clicked on the button inside the webivew and toggled the content, then i need to show the exact same content in alert dialog)
I tried to inflate by moving the webview along with some textview in seperate layout but it on alert dialog it shows textview but webview is not loaded.
Suggestions welcome!!
To be clear, i assume your question is to reuse an existing web view from a layout and display it in alert dialog box.
If so you can remove web view from the original layout and add it in alert dialog as follows
WebView myWebview = findViewById(R.id.myWebView);
ViewGroup parent = (ViewGroup) myWebview.getParent();
if (parent != null) {
parent.removeView(myWebview);
}
AlertDialog.Builder builder;
AlertDialog alert;
builder = new AlertDialog.Builder(this);
alert = builder.create();
alert.setView(myWebview);
alert.show();
so the existing webview will be removed from its layout and displayed on the alert dialog.
You can also do the vice versa (remove webview from alert dialog and append in the previous layout.

Can't access to a spinner in Java

I've got an activity where there is a button which opens an AlertDialog.
My dialog works and I decided to add a layout to it which contains a spinner.
So I have 3 documents :
mainActivity.java : Its role is to open a Dialog
activity_main.xml
dialog_main.xml : The dialog's layout containing the spinner
I try to retrieve in mainActivity.java the spinner declared in dialog_main.xml (in order to add it an adapter) :
Spinner mySpinner = (Spinner)findViewById(R.id.mySpinner);
However mySpinner = NULL, I can't find mySpinner. What is the problem ?
findViewById
as a method of the Activity class finds Views in your Activity's layout. When you are showing a Dialog, it is not part of your layout, so you have to findViewById in the Dialog's layout.
You are probably inflating a View for the Dialog that you show, you cou can use this view to find your Spinnner.
It probably looks like
View view = layoutInflater.inflate (...);
then you do
view.findViewById(...);
Post the code of how how show the Dialog and I'll show you if you can't do it.

Prevent onClick while Activity is displaying Progress Dialog (Android)

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)

Dialog start only the first time on startup?

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.

Dialog context issue - Android

I have created a dialog box like so using a custom layout:
dialog = new Dialog(FetchMenu.this, R.style.CustomDialogTheme);
dialog.setContentView(R.layout.custom_dialog_iab);
dialog.show();
I am now trying to edit a textbox within 'layout.custom_dialog_iab' for example:
TextView text = (TextView) findViewById(R.id.all_topics_unlock_button);
text.setText("Purchased");
My Question: How do I get the right context to be able to edit the textboxes?
P.S. I have tried 'dialog.getContext()' but still keep throwing null pointers?
You need to use:
TextView text = (TextView) dialog.findViewById(R.id.all_topics_unlock_button);
Note the dialog. in front of findViewById
regular findViewById() will search your activity layout rather than the dialog layout.

Categories

Resources