I have a AlertDialog box with approximately 10 controls (text and TextView) on it. These controls are in a ScrollView with AlertDialog, plus I got 2 buttons positive and negative. The issue I have is when the soft keyboard pops up the two buttons are hidden behind the keyboard.
I was looking for something like redraw function on my inner View or the dialog box. Below is the screen shot of what I am talking about.
If your dialog was an activity using one of the Dialog themes you could effect this behavior by setting the adjustResize flag for the windowSoftInputMode parameter of the activity.
I'm using:
android:windowSoftInputMode="adjustResize|stateHidden"
I think you can still use this flag with regular dialogs, but I'm not sure how to apply it. You may have to create your AlertDialog with a custom theme that inherits the right parent theme and also sets that flag, or you might have to use ContextThemeWrappers and stuff.
Or maybe you can just use Window#setSoftInputMode.
alertDialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
I've found a best way to handle this. Because this is a dialog, So the code
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
doesn't work very well.
Besides this code, you must set a dialog style for this dialog. The style should like below:
<style name="DialogStyle" parent="#android:style/Theme.Black.NoTitleBar.Fullscreen">
<item name="android:windowFullscreen">false</item>
......
......
</style>
NOTICE that the attribute parent is Theme.Black.NoTitleBar.Fullscreen like an activity's style. and the attribute android:windowFullScreen should be false.
Now, the dialog will be resized when the soft keyboard toggles.
Nothing worked for me except adjustPan
as per the documentation
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
So just simply use it in your onCreate() or onCreateView() method like:
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Or simply put android:windowSoftInputMode="adjustPan" in manifest for the Activiry in which we are playing with dialogs
and use android:windowSoftInputMode="adjustResize|stateHidden" in each edittext which will help the user to navigate to next textbox easily.
Point to remember
Never use MATCH_PARENT to make the dialog full screen as adjustPan will not work here. If anyone wants to make the dialog to fit the screen, just use points till 0.96 (not more than this) for the height, so the keyboard will properly reach to the edittext. I did like below :
#Override
public void onStart()
{
super.onStart();
Dialog dialog = getDialog();
if (dialog != null)
{
//int height = ViewGroup.LayoutParams.MATCH_PARENT;
int width = ViewGroup.LayoutParams.MATCH_PARENT;
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
//int width = (int)(size.x * 0.96);
int h = (int)(size.y * 0.96);
dialog.getWindow().setLayout(width, h);
}
}
Look, If I will use the total height (MATCH_PARENT) then soft_keyboard will squize the dialog. But if I will use points for the height (here 0.96 which is almost near to match_parent), then it will properly work.
Hope it will help someone :)
maybe you don't need to resize Dialog
add android:imeOptions="actionNext" to EditText(all but last) (it will add "Next" button to the keyboard - go to next EditText)
and add android:imeOptions="actionDone" to last EditText ("Done" button - hide keyboard)
now user should be able to click buttons
if you're creating textboxes in code use EditText#setImeOptions function
HTH
Are you forced to have it as a popup? The popup looks so large, that you may just want to have it as a separate activity. In general, popups are used to provide a brief question or statement with a few options, not a full blown data entry form. Since you can't see much behind the large popup, you're not exposing any underlying controls anyways.
to show keyboard immediately and adjust size:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
}
});
To those who are in the same situation as me.
in my case, the problem was activity having these attributes in style
<style name="SomeStyleName">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
If windowTranslucentStatus and windowTranslucentNavigation both are true,
the keyboard came up as it overlay dialog.
So I override those values to false, only for materialAlertDialog. (maybe AlertDialog or Dialog in your case)
<style name="SomeStyleName">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="materialAlertDialogTheme">#style/TranslucentMaterialAlertDialogTheme</item>
</style>
<style name="TranslucentMaterialAlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>
Related
My current method to customize my UI is using the usual android DatePicker then use DatePicketDialog.getDatePicker() to get the inside component out, and customize it.
Now the result is in the image at https://dl.dropboxusercontent.com/u/3286004/Screen%20Shot%202557-08-29%20at%202.52.21%20AM.png
The Question is ... I want to customize the black line above the DONE button to another color.
Could you suggest how I can get that line component out, so I can change it.
Thank you in advance :D
This is absolutely possible, actually you could do whatever you want with it. Really, one of options is to use style and theme which however would not work in 4.x. The more, lets say, proper or easy way is to use views itself like following:
// we need this listener since only here all views are really drawn and accessible
yourDialog.setOnShowListener(new DialogInterface.OnShowListener() {
private boolean areButtonsFixed;
#Override
public void onShow(DialogInterface dialog) {
if (areButtonsFixed)
return;
// both buttons - you could search for only positive button or whatever button your dialog has
final Button btnPositive = getButton(DatePickerDialog.BUTTON_POSITIVE);
final Button btnNegative = getButton(DatePickerDialog.BUTTON_NEGATIVE);
final Button btnNeutral = getButton(DatePickerDialog.BUTTON_NEUTRAL);
// buttons layout parameters, change it into material style (gravity right)
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) btnPositive.getLayoutParams();
lp.weight = 0; // was 1 to fill 50% horizontally
// positive button, set your own label
btnPositive.setText(R.string.dialog_ok_label);
// set text color and size
btnPositive.setTextColor(ResHelper.getColor(R.color.blue_bright));
btnPositive.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResHelper.getDimensPx(R.dimen.text_size_14));
btnPositive.setLayoutParams(lp);
// divider above buttons
((LinearLayout) btnPositive.getParent().getParent()).setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
areButtonsFixed = true;
}
This (prelast line) will remove divider above buttons at all. If you wish to customize it instead do it like following:
((LinearLayout) btnPositive.getParent().getParent()).setDividerDrawable(R.drawable.yer_drawable);
One way would be to use another theme. This theme is Holo i think, so you can't change colors.
I think you can create your dialog with a custom layout.
If you used a custom layout, you can change colors.
Or, you should use another theme, or create your own theme.
EDIT
Yep, at run-time too.
Many things using on your layout are locked, like colors, especially on widgets (searchView for example)
In default dialog it is impossible, this line has system color. You should convert this dialog to activity, then you can change color there.
We're applying the new Android KitKat translucent theme in our apps, and we're getting a weird issue when the keyboard appears. If we don't use the new android:windowTranslucentStatus attribute, all works as usual: The screen is resized, and all remains visible. But when we're using android:windowTranslucentStatus, the screen isn't resized and our EditText becomes hidden by the keyboard.
A sample of the issue:
The only difference between the screens is in the attribute in the style:
First screen:
<item name="android:windowTranslucentStatus">false</item>
Second screen:
<item name="android:windowTranslucentStatus">true</item>
We think this is a bug from Kitkat release, but we want you to be aware of this. We're getting a little mad. Of course, if someone have a solution, it will be amazing.
EDIT: I just added this issue to the Android issue tracker. Probably you'll be interested in starring the issue: https://issuetracker.google.com/issues/36986276
I also ran into this very annoying issue. But eventually I got this working:
<style name="Theme.MyApp">
<item name="android:windowTranslucentStatus">true</item>
</style>
Instead of setting fitSystemWindows="true" in the theme, I set it on the root view of my layout..
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
I also use the great SystemBarTintManager in my activity:
new SystemBarTintManager(this).setStatusBarTintEnabled(true);
This seems to work well, but admittedly seems pretty fickle. Hope it helps someone anyway!
A workaround would be:
Add a Touch or Focus or ClickListener and move up the EditText holding layout up to half of the screen. However this is really not the way it should work, so keep your fingers crossed and hope that Google gets it fixed.
EditText e = new EditText(this);
e.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
v.animate().x(screenHeight/2f).start();
else
v.animate().x(screenHeight).start();
}
});
Just adjust v to your holding layout and make sure the positions you move to look good.
put this below lines after oncreate in you activity
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(mCategoryColors.getColorStatusBar());
}
Work for me :)
This isn't a perfect solution but a workaround I found was to set android:windowSoftInputMode="adjustPan" in AndroidManifest.xml.
Note that this will pan the whole window out of the way of the keyboard, rather than resizing it.
More info here (search for windowSoftInputMode).
set android:windowSoftInputMode="adjustPan" in AndroidManifest.xml is ok. But i just want to say that it not work when activity is FullScreen, for more details see Android How to adjust layout in Full Screen Mode when softkeyboard is visible
Add the following as well
<item name="android:fitsSystemWindows">true</item>
I have a service which displays a floating view on the window manager (using WINDOW_TYPE_ALERT permission). I'm able to display it and perform actions. But, I have two specific questions:
Regarding the implementation of the floating view
How to listen to system back button event so that I can dismiss the view.
Implementation:
In the manifest I added permissions for:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
I have a broadcast receiver which will listen for Alarm events. Upon receiving the event, I'm starting a service to display the floating view. The following is the code I'm using to create the view.
LayoutParams layOutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
Whenever a user performs any action on the view, I'm removing the view from window manager and killing the service.
What I would like to know is - if this is the right way to approach the problem or are there any better ways to do it? And, should I make changes to the LayoutParams or keep them as is?
Secondly, I would also like to dismiss this floating view when there is SYSTEM BACK/HARDWARE BACK button press event. Any pointers on how to do this would be helpful.
Attaching a screenshot of the floating view for better understanding:
In terms of back button detection - I made it to work in a following way (everything happens in service onCreate code):
Wrap your desired view into ViewGroup (LinearLayout, Relative or other)
override dispatchKeyEvent like this in wrapper view:
mView = new RelativeLayout(this) {
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
// < your action >
return true;
}
return super.dispatchKeyEvent(event);
}
};
add wrapper view to the window manager, and be sure that WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE is not set on wrapper layout params.
Have a look at Standout Library , which is good for handling floating windows , it seems it is also not handling back press event , contacting developer might help.
And one more way is you can try opening activity with semi transparent background/theme to get the similar effect used in floating window in Any.do and backpress event can be handled
Regarding the Back button -
You should override the "onBackPressed()" inside your view and do whatever you want
#Override
public boolean onBackPressed() {
// Remove your view from the window...
}
Anyway, I'm using an SDK called Tooleap, to display floating windows in a straight-forward way.
Do you want the HOME button to also dismiss your UI? If you do, it sounds like it is better to have an activity that opens on a transparent background, instead of an alert window.
To do this use the following style as the theme for your activity
<style name="Transparent">
<item name="android:windowFrame">#null</item>
<item name="android:windowBackground">#color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowFullscreen">true</item>
</style>
FOR WORKING "onBackpressed" BUTTON FOLLOW BELOW INSTRUCTION
1.Go to FlotingFolder.java file in example (Not library).
2.Find below method
#Override
public int getFlags(int id) {
if (APP_SELECTOR_ID == id) {
return super.getFlags(id);
} else {
return super.getFlags(id) | StandOutFlags.FLAG_BODY_MOVE_ENABLE
| StandOutFlags.FLAG_WINDOW_EDGE_LIMITS_ENABLE
| StandOutFlags.FLAG_WINDOW_FOCUSABLE_DISABLE;
}
}
Then remove - "StandOutFlags.FLAG_WINDOW_FOCUSABLE_DISABLE" from the above method
Now onBackPressed will work.
I'm optimising our Android application for tablets. For much of the application, we're using fragments but for some settings screens, we just want to launch the screen as a dialog for tablets. To achieve this, we set the theme of the Activity in the Manifest
<activity
android:name="com.company.app.EditTaxTypeSettingsActivity"
android:theme="#style/ActivityTheme">
</activity>
And here's the theme (in res/values-large-v11):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="ActivityTheme" parent="#android:style/Theme.Holo.Light.Dialog">
<item name="android:windowIsFloating">true</item>
</style>
</resources>
The activities using this theme open as a dialog and look fine in landscape orientation. But in portrait mode, the dialogs fill the screen vertically which looks silly.
How can I set the maximum height of these dialogs? I have tried settings the maxHeight attribute of the theme <item name="android:maxHeight">500dip</item> but it seems to refer to the dialog container around the activity content rather than to activity content itself. (ie, if I set the height of the Activity theme to 500dip, the dialog just gets a very large dialog title area)
I also tried setting the size of the activities layout xml (RelativeLayout) but I can't set a maxHeight, I can only set the height but I can't hard code the height there because I have Save and Cancel buttons set to android:layout_alignParentBottom="true" so they can remain above the keyboard when the keyboard pops up. (Hard coding the activities layout height, prevents the save/cancel buttons from remaining on top of the keyboard)
Thanks, does anyone know how to set a maximum dialog height, preferably at the theme level?
Use this method to fix the height of the Window for your activity with Dialog theme.
public static void maxinumDialogWindowHeight(Window window) {
WindowManager.LayoutParams layout = new WindowManager.LayoutParams();
layout.copyFrom(window.getAttributes());
layout.height = WindowManager.LayoutParams.FILL_PARENT;
window.setAttributes(layout);
}
And use it after setContentView() (it will reset the Window layout to wrap_content):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.messages);
if (getWindow().isFloating()) ViewHelper.maxinumDialogWindowHeight(getWindow());
// rest of your code...
}
I have an activity that I would like to occur in a dialog. Is there anyway to do this from code, instead of in the manifest? I tried to do this, but it seemed to have no effect.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_Dialog);
}
Also, the activity contains a webview and when it starts out as a dialog it's got a small amount of content and the dialog is only like 100px tall. When content fills in it scrolls inside a tiny 100px tall window in the dialog. How do I make the dialog take up more vertical space?
You can easily accomplish this via XML. Just use an XML named 'themes.xml', and place it in the values folder.
Here's a basic example, which implements a custom background:
<resources>
<style name="my_theme" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#drawable/custom_background</item>
</style>
</resources>
You'll also need to add the following line to the desired activity section of the manifest:
android:theme="#style/my_theme"
PS: I realize this is an old thread, but hopefully it helps someone nonetheless :)
I'm not aware of a way to set the dialog theme from code.
The dialog is basically as big as it needs to be to contain the content, so if you want it to be bigger you need to make some component in your view larger. Perhaps you can set the hieght of the webview to something larger. Note, use dpi, not px!
that's the solution, you can apply a theme via code, thanks to this guy :)
wasn't aware of this constructor myself
https://stackoverflow.com/a/1975508/371749