I have a screenshot below of a random dark/black slightly downwards gradient line appearing above my dialog fragments.
These are build with a dialogfragment class that has been overridden, and an alertdialog builder is being used to construct them (happens with and without the title/buttons) inside the following method
public Dialog onCreateDialog(Bundle savedInstanceState)
Anyone had this happen to them before or have any ideas?
Ive tried to theme them differently, and the same happens with both API14 holo and holoeverywhere library. Ive tried to set the backgrounds to transparent ect... but havent achieved anything except making the dim go away.
You need to add your custom theme for your dialog and provide android:windowContentOverlay parameter.
<style name="MyDialogTheme">
<item name="android:windowContentOverlay">#null</item>
</style>
Then, in your DialogFragment in onCreate call:
setStyle(/* desired style */, R.style.MyDialogTheme);
The Weird Line appears because of the title bar. You just need to hide the title bar and it automatically hides the weird line:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
// make your dialog here
return dialog;
}
Try this,
private Dialog custom_dialog;
private Window window;
custom_dialog = new Dialog(context);
custom_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
custom_dialog.setContentView(R.layout.share_dialog);
custom_dialog.setCancelable(true);
//Below code is used to remove wired black line
window = custom_dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
window.setBackgroundDrawableResource(R.drawable.empty);
Found one way of getting around my problem.
So the deal was that the FrameLayout (with id: android:id/content) that the system makes itself had a foreground drawable on it (the shadow at the top of the frame). I couldnt for the life of me deal with it with styles or anything, nor could i figure out why it was happening on these two custom alert dialogs inside dialogfragments.
note: im using a HoloEverywhere fork thats compatible with v7 AppCompat, but keeping as many of the classes ect... from the actual support libraries.
I could however access the framelayout and remove the foreground drawable with this:
(note: it had to be in the onresume as i dont think there are any calls before that resulted in a "android.util.AndroidRuntimeException: requestFeature() must be called before adding content" error.
public void onResume() {
super.onResume();
((FrameLayout) getDialog().getWindow().getDecorView().findViewById(android.R.id.content)).setForeground(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
If theres a better safer way of doing this, or if anyone knows why this is happening, let me know.
Related
I'm using an AlertDialog to show the result of a game. My problem is that the dialog seems to changes the depiction of my normal activiymain layout while the dialog is open.
That's the layout without the dialog
That's the layout while the dialog is open
As you can see the Textviews that i use as buttons are colored weirdly.
My Dialog is created like this:
AlertDialog.Builder = new AlertDialog.Builder(MainActivity.this);
turnout.setTitle("Dialog");
The mainactivity layout is a relative layout and the textviews are colored in #ffffff (meaning white)
By default Android dims the background (displays the "grey shadow" behind the dialog) whenever a dialog is displayed. Changing this default behavior may decrease the readability of a dialog and confuse a user.
That being said, you can disable the background by setting the parameter android:backgroundDimEnabled to false in your dialog's style.
You add the theme to your styles.xml:
<style name="NoDimAlertDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="android:backgroundDimEnabled">false</item>
</style>
Make sure to inherit the default AlertDialog's style.
Additionally you can control the transparency of the dim, using android:backgroundDimAmount. This parameter takes a value from 0 (no dim) to 1 (background completely black). By default Android seems to use 0.6.
<style name="LessDimAlertDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="android:backgroundDimAmount">0.25</item>
</style>
Next, pass the style name to theAlertDialog.Builder's constructor in your MainActivity.java:
new AlertDialog.Builder(this, R.style.NoDimAlertDialog)
.setTitle("Some title")
.setMessage("Some message")
.show();
You may as well consider using a Fragment to display your AlertDialog. Android comes with handy DialogFragment class for that scenario. A simple DialogFragment in your case may look like that:
class MyDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getContext(), R.style.NoDimDialog)
.setTitle("Some title")
.setMessage("Some message")
.create();
}
}
Then you add your Fragment in MainActivity.java:
new MyDialogFragment().show(getSupportFragmentManager(), "DialogTag");
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.
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;
}
I have a dialog that comes up in my app and I wanted to stray away from using the default dialog, to give something slightly more customized. In my dialog layout, I included the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/My_Custom_Background">
This does basically what I want it to, it changes the background as expected. However, this only applies to the layout of the contents of the dialog box: the dialog also has a title and the title part of the dialog box is still the default Android theme, then everything under it is customized as I wanted. Is there away to extend the custom background to the entire dialog box?
You need to remove the title bar
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog);
Make sure to call requestWindowFeature() before the setContentView() otherwise you get a FATAL EXCEPTION
you can create your dialog as below
Dialog mDialog = new Dialog(mContext);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
mDialog.setContentView(R.layout.your_custom_dialog_layout);
mDialog.setCancelable(false);
mDialog.show();
and inside your custom layout, you can set the custom drawable as a background.
An alternate is to remove your dialog titlebar using this...
yourdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
and design whole dialog with title inside your layout...
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>