show alert dialog in screenshot - java

I have to design an app in which on click of the neutral button, an image of the alert dialogue gets stored in the sdcard.
So I decided to programatically take a screenshot and then use the BitmapDecodeRegion class to crop the image.
But when i take the screenshot, the alert dialogue does not appear in it since it's not attached to the window.
How can I attach it to the window?
here is the code snippet:
public void btnClick(View v) {
Log.d("", "logger button clicked");
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("This is a demo!");
dialog.setMessage("Lets see if this works");
dialog.setCancelable(false);
dialog.setNeutralButton("Take Snap",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Bitmap bitmap;
View v1 = findViewById(android.R.id.content)
.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
GlobalObj.screenImg = bitmap;
v1.setDrawingCacheEnabled(false);
Intent i = new Intent(MainActivity.this,
ViewActivity.class);
startActivity(i);
}
});
AlertDialog newDialog = dialog.create();
newDialog.show();
Kindly help me out.

Try out this library: https://github.com/jraska/Falcon.
It takes screenshots of all active windows of application including dialogs and could figure out your problem.

In your code change this line:
View v1 = findViewById(android.R.id.content)
.getRootView();
to this:
View v1 = AlertDialog.class.cast(dialog).getWindow().getDecorView().getRootView();
It works for me and does not need rooted phone. However this captures only the alert dialog on a black background, without the underlying views from your activity. If you want to have everything (not that simple) you can try this answer from Aswin Rajendiran.

Related

Place icon at right of title in a AlertDialog builder

All the answers and info that i find at internet seems old and deprecated, i could add a simple icon at my title bar at left (user image), but i need to have 2 more images, a message and a phone at the right, like a simple user details view, the problem is that i don't know how to set the position of those images, the only thing i could do was this:
AlertDialog.Builder builder = new AlertDialog.Builder(Friends.this);
final User user = usersList.get(position);
builder.setTitle(user.getUsername());
builder.setMessage("You wanna delete friendgroup: " + user.getUsername());
builder.setIcon(R.drawable.ic_user);
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//myRef.child("Users").child(userId).child("FriendLists").child(friend.getName()).removeValue();
//friendList.remove(friend);
//mAdapter.notifyDataSetChanged();
}
});
builder.show();
with the builder.setIcon i could add a image at the left, but how can i add more icons and at the right of my title ? do i really need a custom builder to do this?
Thanks
You have to make a custom AlertDialog.
Try this:
Design a RelativeLayout that contains your icon, title and right message and phone icon and other things.
Set this layout to your AlertDialog using setView() method.
AlertDialog.Builder builder = new AlertDialog.Builder(Friends.this);
final User user = usersList.get(position);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.your_dialog_layout, null);
builder.setView(dialogView);
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//myRef.child("Users").child(userId).child("FriendLists").child(friend.getName()).removeValue();
//friendList.remove(friend);
//mAdapter.notifyDataSetChanged();
}
});
builder.show();
Here is a good tutorial
Hope this will help~

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!

Progress bar inside alert dialog containing webview

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

Categories