How to set maximum height for activity (Dialog themed) - android

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

Related

Setting backgroundcolor in theme gives graphical buggy dialog

In my activity theme in the themes.xml I have set a background color in order to move away from the default (transparent/white?) background color to my own.
<item name="android:background">#color/red</item>
Unfortunately, when the I am showing my loading dialog the color shines halfway through that dialog now. Was this to be expected?
I have tried to use different themes, also defined by own dialog theme subclassing from Holo Light setting the background color explicitly to white, but the problem persists, only the currently still white areas are changed in this case.
What can I do? The only alternative is currently to use the Tradiotional Dialog Theme.
Try to set android:windowBackground instead. android:background attribute is applied to all nested views. Here is the discussion: What's the difference between windowBackground and background for activities style?
It looks like there's some padding or margins to the left and right of the title. If you're using the built-in ProgressDialog I'd suggest creating your own Dialog instead, that way you can change anything you want about it. Just create your own xml layout and create the dialog like this:
protected static Dialog getProgressDialog(Activity activity) {
Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View progressDialogView = inflater.inflate(R.layout.custom_progress_dialog, null);
dialog.setContentView(progressDialogView);
dialog.setCancelable(false);
return dialog;
}

How to style Theme.Holo.DialogWhenLarge to custom size, position, margins, etc.?

I am working on my first Android app (only worked on iOS and WP before) and trying to show an activity in fullscreen on phones and and as dialog/popup on tablets. I found Theme.Holo.DialogWhenLarge and it seems that this is what I was looking for.
The activity is displayed as dialog on tablets but I did not found out how to change the size and position of the activity. It is simply shown at screen center and has about the same size and dimensions as on the phone. How can this be changed? Is this done by the Activity directly or do I have to create a sub-style?
I had a look on Theme.Holo.DialogWhenLarge. This Theme simply applies PreferencePanel.Dialog to preferencePanelStyle. I do not understand how this influences the activity to be shown as Dialog. What is this PreferencePanel?
How can I find out in general what kind of properties of an object (view/activity) can be changed by a style?
I have the same problem. I didn't find any way to override the width and height styles on the xml.
The only way I found is to change the window on the onStart, here is the code:
#Override
public void onStart() {
super.onStart();
// In order to not be too narrow, set the window size based on the screen resolution:
final int new_window_width = 505;
final int new_window_height = 502;
WindowManager.LayoutParams layout = getDialog().getWindow().getAttributes();
layout.width = new_window_width;
layout.height = new_window_height;
getDialog().getWindow().setAttributes(layout);
}
Hope it helps!

android: set background color for the entire screen dynamically, or how to set actionbar to overlay layout, but not overlap content

My app uses the Theme.Wallpaper family, which means the current wallpaper is used as the app's background.
This may cause readability issues (depending on the user's favorite wallpaper) so I want to allow the user to choose an image as a background.
The way I am trying to implement it is:
#Override
protected void onResume() {
// Get wallpaper preferences to check if user selected a wallpaper and
// display it
SharedPreferences wallpaperPref = getSharedPreferences(WallpaperActivity.WALLPAPER_PREFERENCES, MODE_PRIVATE);
int selectedWallpaper = wallpaperPref.getInt(WallpaperActivity.SELECTED_WALLPAPER, 0);
if (selectedWallpaper != 0) {
findViewById(R.id.pager).setBackgroundResource(selectedWallpaper);
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(selectedWallpaper));
} else {
findViewById(R.id.pager).setBackgroundColor(Color.TRANSPARENT);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
super.onResume();
}
The shared preference contains the resource id of the selected wallpaper.
My problem is the titlebar and actionbar do not respond to this. Is there a way to make them also receive the new background?
Make the background of the title bar and action bar transparent.
Set your Window ActionBar Overlay to true.
Pad your main view by the height of the ActionBar so the ActionBar doesn't cover up any of the content.
Your ActionBar will now sit over the background view, instead of above it. It will be transparent, so as to allow the background to show through, and it will not be covered up by the ActionBar.
Here's what I ended up doing, thanks to #Sky for the help:
On my app's style I added this item: <item name="windowActionBarOverlay">true</item> which makes the actionbar overlapp my layouts. This is the solution to the original problem, but of course it is not useable as long as the layout contents are overlapped, so here's how to overcome this:
On the dimen.xml file I added these two dimentions to be used as paddingTop values
dimen name="actionBarSize" 56.0dip dimen,
dimen name="actionBarWithTabsSize" 112.0dip dimen.
(add the '<' and '/> in place)
These are necessary to API 10 and below, for API-11 and above there is ?android:attr/actionBarSize that can be set as the paddingTop value.
On each top-level layout - that is the top level element on every layout file that is being inflated by an activity, I added a paddingTop value like this: android:paddingTop="#dimen/actionBarSize" except on the layout in which I have tabbs implemented, there I used android:paddingTop="#dimen/actionBarWithTabsSize".
On the preference.xml file in the xml folder, I added this line to the top-level PreferenceScreen element: android:layout="#layout/activity_settings".
On the layout folder I created a new layout file named "activity_settings.xml". It contains a FrameLayout as the top-level element, with this property: android:paddingTop="#dimen/actionBarSize", and within the FrameLayout there is one ListView element with id android:id="#android:id/list".
On my PreferenceActivity sub-class, right after the call to addPreferencesFromResource(R.xml.preferences); I added setContentView(R.layout.activity_settings);. This tells the preference activity to use the activity_settings.xml layout file as it's layout, and the ListView with id of "#android:id/list" as it's preference container. This way you get control of the top-level layout file in order to set it's padding.

How to resize AlertDialog on the Keyboard display

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>

Apply the Android "Dialog" theme and sizing it properly

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

Categories

Resources