Style AlertDialog with SeekBar to mimic native volume controls - android

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.

Related

Android CustomDialog minWidth

I have a CustomDialog (extending Dialog) which is currently set to wrap_content for both width and heigh. Problem is if I set the main text as something really short (say "hello") the dialog gets very small in width. I would like the dialog to always take all the space available, but still have the default margin from the edges of the screen.
I already tried various methods:
setting:
android:layout_width="match_parent"
on the dialog layout, doesn't have any effect.
doing:
int width = (int)(getContext().getResources().getDisplayMetrics().widthPixels*0.90);
getWindow().setLayout(width, LinearLayout.LayoutParams.WRAP_CONTENT);
causes the dialog to go at the start of the screen and doesn't change anything regarding the width
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.horizontalMargin = 10;
params.width = LinearLayout.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
makes the dialog take the whole screen but without any horizonthal margin
putting
<item name="windowMinWidthMajor">100%</item>
<item name="windowMinWidthMinor">65%</item>
in the dialog style has no effect.
how do I accomplish that? Mind that the dialog is used all around the app, so putting any code outside of the dialog class itself is not an option.

blur window when showing AlertDialog in android

I want make background window blur when showing alert dialog.
I have tried -
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount = 0.0f;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
but it is not working in, also it seems "FLAG_BLUR_BEHIND" is deprecated.
is there any other way to achieve this without using third party libraries?

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

Anchor Android Dialog to a View

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().

Align Android activity

Is there any possibility in Android to align a dialog Activity (activity with a dialog theme) at the bottom? By default, it is displayed in the middle of the screen. I haven't found any information on that... Thanks.
This is not exactly what you are doing, but I had a similar problem where I needed to display a dialog from an activity - at the bottom of the screen. The trick was to use WindowManager.LayoutParams. Here's what my onPrepareDialog looks like:
public void onPrepareDialog(int dialog, Dialog dlg) {
Window window = dlg.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
switch (dialog) {
case DIALOGUE_LOADING_PLEASE_WAIT:
wlp.gravity = Gravity.BOTTOM;
window.setAttributes(wlp);
break;
default:
break;
}
}
I didn't tried but searching on google found this...does this help you getWindow().setAttributes() , I hope this will help you.
content of the link (if it doesn't work) :-
You can call getWindow().getAttributes() to retrieve the
WindowManager.LayoutParams for the window. This has the following
fields involving window placement:
http://code.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#width
http://code.google.com/android/reference/android/view/ViewGroup.LayoutParams.html#height
http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#gravity
http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#x
http://code.google.com/android/reference/android/view/WindowManager.LayoutParams.html#y
After making your desired changes, use getWindow().setAttributes() to
install the new values.
Note that, though you can force a specific size through the width and
height fields, in general the correct way to do this is let the window
do its normal layout and determine the window size automatically that
way. If you have a single view in the dialog that wants a fixed size
such as 300x200, implement View.onMeasure() to call
setMeasuredDimension(300, 200). Then when the view hierarchy layout
happens, the dialog window will be given a size that ensures your view
is that dimension (probably making the actual window larger to take
into account the dialog frame and decoration).
http://code.google.com/android/reference/android/view/View.html#onMeasure(int,%20int)

Categories

Resources