how to load existing webview in alert dialog android - java

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.

Related

Android Studio - DatePicker in AlertDialog, null object reference

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

Soft KeyBoard does not show up in Landscape orientation

In my app, I have a dialog with EditText. I have coded it in such a way that as soon as the dialog shows up,the soft keyboard shows up, the edit text will get focus , all the existing text in the editText is preselected. And user can start typing. This all works fine when I am holding my phone in portrait orientation. But the same thing does not work when I have phone in landscape orientation. Part of my code in my fragment is pasted below.
final EditText inputDeviceId = new EditText(getActivity());
inputDeviceId.setInputType(InputType.TYPE_CLASS_TEXT);
String savedDevId = getDeviceID();
inputDeviceId.setText(savedDevId);
inputDeviceId.setSelectAllOnFocus(true);
// some more code ... to create dialog , ok cancel buttons then,
final AlertDialog alertDialog = alertDialogBuilder.create();
inputDeviceId.setOnFocusChangeListener(new View.OnFocusChangeListener() {
if(hasFocus){
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
});
alertDialog.show();
Now this all works in portrait orientation and does not work in landscape.
In landscape mode the dialog with edittext shows up , it has focus and existing text is preselected but it does not show the keyboard. User has to click in the editText for keyboard to come up. Why? I am using nexus-5 to test.

gallery view on android in alertdialog

I have source code for a slider puzzle. it delivers a random image for the puzzle and you can change alot of settings like tiles and numbers and what not. Its really easy to add pictures you just have to have them start with image_ and put them in the drawable folder. Only thing is it does not have a layout.xml i guess everything is written through the activity.
I want to have all of the pictures in like a gallery view when a button is clicked in the menu. i have seen all the gallery codes need code in the layout which this source code does not have so essentially i would like
rather than this
public void showAbout()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.title_about);
builder.setMessage(R.string.content_about);
builder.setPositiveButton(R.string.label_ok, null);
builder.setIcon(R.drawable.ic_launcher);
AlertDialog dlg = builder.create();
dlg.show();
((TextView) dlg.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
}
i just want it to show up as a photo gallery

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