Progress bar inside alert dialog containing webview - java

I have created an activity in which, on clicking a particular button, an alert dialog will open containing a WebView.
Everything is working fine, except when the url of the WebView is getting loaded. While loading the content of the WebView, the alert dialog's tile and button is shown, which looks a bit weird.
This is the screenshot of the alert dialog while loading the webpage for the webview inside it:
After successfully loading the content of the webview, it looks like this:
What I want is while loading the webview's content, a progress bar will be shown or a fixed background image will be show. How to achieve that?
The method which I am using to create this alert is:
//method to zoom images
public void zoomImage(String imageUrl)
{
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Zoomed Image");
WebView wv = new WebView(this);
wv.setBackgroundColor(Color.WHITE);
wv.getSettings().setBuiltInZoomControls(true);
wv.setInitialScale(400);
wv.loadUrl(imageUrl);
wv.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
alert.setView(wv);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int id)
{
}
});
alert.show();
}
Thanks in advance!

You can take a progress bar in your alert dialog custom layout, and make the visibility gone in
the OnPageFinishedMethod.
you can create your custom alert dialog like this:
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.big_east, (ViewGroup) getCurrentFocus());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.setTitle("Zoomed Image");
and you should override two methods just like "shouldOverrideUrlLoading".
Methods are "OnPageStart" and "OnPageFinished".
In OnPageStart show the dialog and in OnPageFinish make the progress bar gone.
Hope it helps.

You can use a WebView.PictureListener (it is deprecated, but still works. Not sure if there is a usable alternative for it):
wv.setPictureListener(new WebView.PictureListener() {
#Override
public void newPicture(WebView view, Picture arg1) {
alert.show();
}
}

Related

How to style AlertDialogs android

I need help with the dialog interface in android. i don't get it.. i' ve searched here to, i got many answers but with every code i used from here my application crashed..
So I make a notes app an you can choice a with an alert dialog which type of note you want.
The dialog window is black. So can someone show me how to chage the color maybe simple in white so that i understand how it works?
here is my code:
private void showNewNoteChoices() {
final CharSequence[] items = {
getResources().getString(R.string.text_note_type),
getResources().getString(R.string.log_note_type),
getResources().getString(R.string.important_type),
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select type of note");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
loadNoteFragment(item, newNoteTitles, null);
}
});
AlertDialog alert = builder.create();
alert.show();
}
I know that i have to make an xml file for the specific layout and i have to make an style . Can someone show me how I can make my dialog interface in white?
I would recommend you using Dialog. You can style it the way you want.
For example, you create a custom dialog with custom layout like this;
final Dialog dialog = new Dialog(MainActivity.this);
dialog .setContentView(R.layout.custom_layout);
Then you can create views and listeners;
Button button = (Button) dialog.findViewById(R.id.button);
//Other views and listeners etc..
Finally you show it;
dialog.show();

How to display popup option of "set as wallpaper" when user press on image for 3-5 sec in inageviewer app?

Following... this link ..How to set image as wall paper in viewpager app?. I am able to set wallpaper directly located in my drawable folder. However, i want to give user a chance to set wallpaper by displaying pop up dialogue box which should be displayed. When user clicks on images for 3-5 sec.
I am kind of new to android programming.. So, please help..
android framework already supporting class "alertDialogue.builder". you can set message whatever you wanna show in addition to dialogue buttons, title etc.
http://developer.android.com/reference/android/app/AlertDialog.Builder.html
5sec is too long to stay in one fingerpoint. in onsimplegesturedetector class, there is already "long press" detector
http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html
you can set your own ontouchListener that implements onTouchListener that has gesture detector, detects long press and can show dialogue .
there can be better way but I'm using this logic in my project so you can reference and fix it if you find better way
findViewById("your wall paper image id").onTouchListener(new MyOnTouchListener());
class MyOnTouchListener implements onTouchListener{
GestureDetector gd = new GestureDetector(new SimpleOnGestureListener(){
#Override
public void onLongPress(MotionEvent e) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("Your Title");
alertDialogBuilder
.setMessage("click yes to set wallpaper!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//setting wallpaper
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}

DialogFragment not visible but selectable

I have a very strange problem on Android using DialogFragments.
I have a FrameLayout with no content and the OnClickListener set to open a FragmentDialog where the user can choose what type of content he wants to add.
If I choose an image from the gallery, this image will be loaded and an image view inside the Frame-Layout is created and the image is shown. If the user clicks again on the layout the selection dialog should open again and the user can select a new image and the old one would be replaced.
This works quite well on my device with Android 4.1. But if I test it on Android 2.3 something strange happens:
The first dialog appears and the user can choose an image from the gallery. But if the user clicks again the dialog is not shown again. But the display becomes darker as if the dialog would be there but is not shown. If I click on the position where the selection dialog should be the gallery is started again. So the dialog is definitely there, but it is simply not shown.
I have tried almost everything that came to my mind(and what I found in the internet) to fix this issue, but it does not help anything. Of course I am using the support library to import the Fragment and DialogFragment.
I start this dialog from a Fragment which is embedded in a ViewPager. So it is basically a tabbed view. What's interesting: I I run into this bug and the display is just getting darker but no dialog is visible, I can cancel the invisible dialog and just drag the ViewPager a bit to left or right(to the next fragment) and if I get back and click on the content again the dialog is shown again.
But if I am dragging the ViewPager around there are no log messages so I have no idea why the dialog is suddenly visible again if I firstly move the page(only a bit is enough).
Here is some of my code:
in the onCreateView method I do the following:
rootView = inflater.inflate(args.getInt(ARG_OBJECT_TYPE), container, false);
editorActivity = ((NoteEditorActivity) EditorSectionFragment.this.getActivity());
// ...
if( fragmentId == R.layout.fragment_note_preferences_editor ){
// the other page
else if( fragmentId == R.layout.fragment_note_editor ) {
final View addLeftElement = rootView.findViewById( R.id.addLeftElement );
final View addRightElement = rootView.findViewById( R.id.addRightElement );
final View addTopElement = rootView.findViewById( R.id.addTopElement );
final View addBottomElement = rootView.findViewById( R.id.addBottomElement );
final FrameLayout contentLayout = (FrameLayout) rootView.findViewById( R.id.contentLayout );
showNavigation(editorActivity, contentLayout, editorActivity.currentPosition.x, editorActivity.currentPosition.y);
contentLayout.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("CONTENT CREATOR", "create new element at " + editorActivity.currentPosition);
//((NoteEditorActivity) EditorSectionFragment.this.getActivity()).showContentSelectionDialog();
((NoteEditorActivity) EditorSectionFragment.this.getActivity()).showCameraChooseDialog();
}
});
}
showCameraChoose(I also did it without the FragmentTransaction, but this didn't work either)
protected void showCameraChooseDialog() {
getSupportFragmentManager().executePendingTransactions();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("cameraChoose");
if( prev != null ){
Log.i("PREVIOUS", "Remove previous dialog");
ft.remove(prev);
}
(new CameraSelectionDialog()).show( ft, "cameraChoose");
}
CameraSelectionDialog:
public static class CameraSelectionDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final CharSequence[] items = {"Camera", "Gallery"};
AlertDialog.Builder builder = new AlertDialog.Builder( this.getActivity() );
builder.setTitle("Choose how to get the image!");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if( which == 0){
((NoteEditorActivity)(getActivity())).startCamera();
CameraSelectionDialog.this.dismiss();
}
else{
((NoteEditorActivity)(getActivity())).startGallery();
CameraSelectionDialog.this.dismiss();
}
}
});
return builder.create();
}
}
The startGallery method simply starts an gallery intent:
protected void startGallery() {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , ActionCodes.GALLERY_ACTION_CODE);
}
This image is handled in the onActivityResult method. But it does not matter what I choose to do in the onActivityResult method. Even if I don't create the image, the problem occurs.
I have no idea what I can do to solve this problem and I hope that maybe you can think of a reason for this strange bug. I am grateful for any advice or hint what could be wrong.
Thank you in advance!

How to set font size for text of dialog buttons

I have an android app that uses some custom dialogs which are inflated from XML layouts. The contents of the dialog's view come from the XML layout, but the actual positive and negative buttons are added by calling the builder's setPositiveButton and setNegativeButton methods, so I have no control over (or at least don't know how to control) the styling of the buttons themselves.
See the onCreateDialog method below from my LoginConfirmationDialog.java file which extends DialogFragment. It basically pops a very simple dialog up that asks for confirmation of who is logging in (i.e. "Are you Joe Schmoe?", with Yes and No buttons).
The XML layout in this case has just a single TextView, and to make this easy (because the users will be construction workers with big knobby dirty fingers who need large text and large buttons), I made the font for the TextView pretty big. The two buttons though have much smaller font for their text, and since they aren't part of my layout and are added with the setPositiveButton and setNegativeButton methods, how do I control the font size?
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle args = this.getArguments();
String empName = args.getString("empName");
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_login_confirmation, null);
TextView message = (TextView)view.findViewById(R.id.txtLoginConfirmationMessage);
message.setText("Are you " + empName + "?");
builder.setView(view);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onEmpConfirmPositiveClick(LoginConfirmationDialog.this);
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onEmpConfirmNegativeClick(LoginConfirmationDialog.this);
}
});
// Create the AlertDialog object and return it
return builder.create();
}
Instead of returning builder.create(), try this.-
final AlertDialog alert = builder.create();
alert.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Button btnPositive = alert.getButton(Dialog.BUTTON_POSITIVE);
btnPositive.setTextSize(TEXT_SIZE);
Button btnNegative = alert.getButton(Dialog.BUTTON_NEGATIVE);
btnNegative.setTextSize(TEXT_SIZE);
}
});
return alert;
Took me a while, to integrate Asok's answer, since I used anonymous inner classes for buttons, so I needed to get a handle on the button references. This works. Make sure it goes after the messageDialog.show() line:
messageDialog.show();
messageDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextSize(TypedValue.COMPLEX_UNIT_SP, 25.0f);
messageDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextSize(TypedValue.COMPLEX_UNIT_SP, 25.0f);
Note: It's recommended to use sp as a unit for text size. Unlike px, it is device density independent.
You can try this:
AlertDialog.Builder builder = new AlertDialog.Builder(CompQuestionsActivity.this);
builder.setMessage("Message");
builder.setPositiveButton("Yes", dialogClickListener);
builder.setNegativeButton("No", dialogClickListener);
AlertDialog alertDialog = builder.create();
alertDialog.show();
//For positive button:
Button button1 = alertDialog.findViewById(android.R.id.button1);
button1.setTextSize(25);
//For negative button:
Button button2 = alertDialog.findViewById(android.R.id.button2);
button2.setTextSize(25);
Since you are already using an xml file for the dialog why not just include the two buttons in the layout and set the onClick handlers in the dialog creation, something like this should work. I am using something similar.
Here is a quick example:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_login_confirmation, null);
TextView message = (TextView)view.findViewById(R.id.txtLoginConfirmationMessage);
message.setText("Are you " + empName + "?");
Button positiveBtn = (Button) view.findViewById(R.id.dialogButtonPositive);
// Set size of button in relation to screen size
positiveBtn.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) 25);
positiveBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mListener.onEmpConfirmPositiveClick(LoginConfirmationDialog.this);
}
});
Button negativeBtn = (Button) view.findViewById(R.id.dialogButtonNeg);
// Set size of button in relation to screen size
negativeBtn.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) 25);
negativeBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mListener.onEmpConfirmNegativeClick(LoginConfirmationDialog.this);
}
});
builder.setView(view);
return builder.create();
I am also quite fond of using the following for setting text sizes, this allows for various screen sizes to get a different size of text (You can play with the float value to suit your needs):
.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) 25);
You should check out the following answer:
In Dialog.java (Android src) a ContextThemeWrapper is used. So you could copy the idea and do something
You just have to change the following line of code:
<item name="android:textSize">10sp</item> to your desired size.
And don't forget to check the comments of the answer also.
Best of luck.
I have tried many devices using setOnShowListener . But it did not work for all devices. In the end I come to a decision that the most easiest way is to using a Theme for your alertDialog.
Add this to the style file.
<style name="MyAlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textSize">10sp</item>
</style>
Now Use this in your AlertDialog
val dialog = AlertDialog.Builder(requireActivity(),R.style.MyAlertDialogTheme)
Thats all.
My approach is to obtain the buttons in onResume() and configure them there
public class LoginConfirmationDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// your code here remains unchanged
}
#Override
public void onResume() {
super.onResume();
Button positiveButton = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
Button negativeButton = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_NEGATIVE);
negativeButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
}
}

Android: Jumping effect of Status Bar

I have a problem with my app. I use the full screen option like so:
public void setFullscreen() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
But, later during the game, when I show the AlertDialog for Help /
Info, the status bar comes back for a fraction of a second and
disappears again creating a flashing or jumping effect. Also, the
Dialog itself is not centered as it should, almost as if it thought
there was a status bar.
Here is how I display the dialog:
protected Dialog onCreateDialog(int id) {
TextView tv;
switch (id) {
case MENU_HELP:
tv = new TextView(this);
tv.setText(R.strings.help_msg);
return new AlertDialog.Builder(BlackJackView.this)
.setIcon(android.R.drawable.ic_menu_help)
.setTitle("How-To")
.setView(tv)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.create();
}
………
}
return null;
}
Does anyone know what may be causing this appearing / disappearing?
How do I make it stop this jumping behavior?
Thanks for all your help.
Simply set dialog's window to fullscreen too
Dialog mPreviewDialog = new Dialog(this);
Window dialog_window = mPreviewDialog.getWindow();
dialog_window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
mPreviewDialog.show()
It fixes the blinking of status bar.

Categories