Anchor Android Dialog to a View - android

I wanted to know if it is possible to anchor a Dialog to a View in Android, or in general: just to display the dialog at a certain location on the screen.
P.S.
I don't want to use a PopupMenu because it is my understanding that one cannot customize the items displayed in the menu-- I'm ultimately trying to have text and put an image next to it to alert the user that they have a message or something new to see here.
Thanks for your time-

Use Window params.
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.copyFrom(getDialog().getWindow().getAttributes());
params.gravity = Gravity.BOTTOM;
params.y = YOUR_ANCHOR_VIEW.getHeight();
getDialog().getWindow().setAttributes(params);
I used Gravity.BOTTOM and view height to anchor dialog on top of anchor view.
Gravity.TOP will make y to apply from top and vise versa.
I used this code on onActivityCreated().

Related

Is there a way when displaying a dialog in Android to tell the app to show the pop up?

I have a full screen app that has buttons that want to have a custom drop down when they are clicked. I was thinking of having a custom dialog show up that will have the options. Is there a way to tell the dialog where to show up?
If you want to create a Dialog window you can change the position of the screen in which it will be displayed by doing something like this:
//Dialog reference
Dialog dialog;
//Instantiate here your dialog
dialog = ... ;
//Get window layout parameters reference
Window window = dialog.getWindow();
WindowManager.LayoutParams windowLayoutParams = window.getAttributes();
//Set the parameters you want
windowLayoutParams.gravity = Gravity.CENTER; //e.g. dialog's will be centered
window.setAttributes(windowLayoutParams);
You can instead use Gravity.BOTTOM or Gravity.TOP to display the dialog accordingly. Alternatively, for more specific positioning you can try setting attributes "windowLayoutParams.x" & "windowLayoutParams.y".

Is there a SoftInputMode workaround to show whole dialog

I have a DialogFragment with two EditText fields and another field with an ImageView to increment its value underneath these, they all live in a ScrollView.
The problem is neither adjust mode for the soft keyboard shows my entire DialogFragment at once, despite there being space.
adjustResize causes the ScrollView to resize and hide the bottom row. adjustpan keeps the ScrollView size intact but the soft keyboard overlaps the bottom row.
Removing the ScrollView means either option causes the keyboard to overlap.
What I would like is for the DialogFragment to move up the screen without resizing. Can I make that happen? Ideally I'd like to keep the ScrollView in my Layout to better support very small screens.
The only solution I found was to change the window options for the dialog fragment itself. Obviously on a smaller screen this will still be an issue so I am still looking for a better answer.
#Override
#NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
//I was using android.R.style.Theme_Translucent_NoTitleBar for another issue
//but I don't think the theme makes any difference to how the window is laid out
//that is relevant to the below code
Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Translucent_NoTitleBar);
... //Do your usual stuff here
dialog.getWindow().setContentView(...);
final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.TOP; //this is the important part
dialog.setCanceledOnTouchOutside(false);
return dialog;
}

Style AlertDialog with SeekBar to mimic native volume controls

I have created an AlertDialog with custom view that displays a SeekBar. Styling the seekbar etc. is not a problem, but I'd like to know how I could make the theme of my AlertDialog mimic the Dialog that appears when changing phone's sound volume. I mean both the position on the screen (about 1/5th from the top) as well as the width (almost match parent).
For clarification a screenshot I found on the internet of the volume controls:
http://triggertrap.com/wp-content/uploads/2013/09/media_volume.png
I found out that I can move the dialog using method described in this post:
https://stackoverflow.com/a/6050911
AlertDialog dialog = builder.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = 100; //x position
wmlp.y = 100; //y position
dialog.show();
It's still not the exact same length, but I guess without some global attribute or style to use, this would be difficult to mimic, as on tablets not only is the dialog specific length, it also has settings button to change volume of notifications and media.
I couldn't find such global style, I looked in Android source and even though I found VolumePreference class that seems to be close to what I'm trying to achieve, it didn't have any specific theme associated with it.

Android Activity with Transparent Layout

I wish to create a activity that look like a popup window
That is with transparent background for activity als i wish to show it in custom position on scree that is on right corner of device screen
what i did was inside onCreate of pop over like activty
Display display = getWindow().getWindowManager().getDefaultDisplay();
WindowManager.LayoutParams params = getWindow().getAttributes();
// params.x = -20;
params.height = (display.getHeight()) / 2;
params.width = (display.getWidth()) / 2;
// params.y = -10;
params.gravity = Gravity.RIGHT;
getWindow().setAttributes(params);
in maifest
<activity android:name=".DialogAct"
android:theme="#android:style/Theme.Holo.Light.NoActionBar"></activity>
This is what my launcher activity looks like
So when i click on search on action bar i'm sending intent to my new activity .I want to make it look like pop just below the search icon
This what i obtained is
You can see search of previous activity is clicked and new activity is loaded(blue portion).
As you can see the new activity moves to centre of screen . but i want it just below the action bar icon.
i tried params.gravity = Gravity.TOP | Gravity.RIGHT;. then i got this
I want to place it just below the action bar of previous activity . I tried many ways to achieve it but failed. so can anyone suggest a methode
In your manifest you wrote
<activity android:name=".DialogAct"
android:theme="#android:style/Theme.Holo.Light.NoActionBar">
you should remove the NoActionBar part
<activity android:name=".DialogAct"
android:theme="#android:style/Theme.Holo.Light">
I can think of two things that may help. First, you could try changing the Theme in your manifest to android:theme="#android:style/Theme.Dialog and this should give you the wanted look.
Edit
You can hide the title bar with
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
just make sure to call it before calling setContentView()
Also, if it doesn't need to be a separate Activity you could use PopupWindow. With this you can use isAboveAnchor and use the ActionBar as the anchor and place it below or use one of the other associated methods. I don't know if either of these will fulfill what you need but they may work for you.
#edwin What you can do is after making
params.gravity = Gravity.TOP | Gravity.RIGHT;
you just provide some margin from top, like this:
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
yourCustomPopOveractivity.setLayoutParams(params);
and i think after doing this you can get your needed position and everything you just did is right.
Please try then tell me
Try Gravity to Center rather than Top Right..

Align an activity as dialog on top left

i know this question was asked for aligning a dialog on the left,
but here i'm using an activity with a
android:theme="#android:style/Theme.Dialog" and the problem is that it's display at the center of the screen and i want to display on the top left corner.
is there any way to do so ?
i have tried using the android:layout_gravity="top|left" on my concerned activity .xml but that doesn't work.
Thank you for your help
Try this on your onCreate of dialog activity
WindowManager.LayoutParams wmlp = getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
Hope it helps.

Categories

Resources