Setting background to a dialog - android

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

Related

Using an Alertdialog changes the textviews behind

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");

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

Weird black line above DialogFragment AlertDialog

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.

Setting the backgroung of a Dialog box as transparent

I am working on an android application that allows user to add an edit text dynamically. I have been able to add the edit text using an alert dialog. But I need to make the dialog box transparent .I need to be able to see the imageview in the background. Color.Transparent wont work because my background is an image
Try this:
Dialog mDialog = new Dialog(mContext, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
or
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
You can try this
<EditText
android:id="#+id/purpose_textfield"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"/>
Instead of Using alert Dialog Box, i prefer you to use Dialog box:
And use this code:
Dialog dialog = new Dialog(getBaseContext, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

Dialogs and Popups in Android

The Android design documentation in http://developer.android.com/design/building-blocks/dialogs.html makes a clear differentiation between Dialogs, Alerts, Popups and Toasts. It also recommends the implementation of Dialogs by means of the DialogFragment class and Toasts by means of the Toast class. However it's not clear to me whether Popups should be implemented with PopupWindow or with DialogFragment.
I know that DialogFragments usually come with Ok/Cancel buttons and that the location of PopupWindows can be defined, but:
Are these slight differences the only arguments to use one or the other?
Is DialogFragment the successor of PopupWindow that will be deprecated at some point?
According to the answer in https://stackoverflow.com/a/15165554/2482894, PopupWindow is "Limited to a few templates", but I can't find any reference to a limited amount of templates in the class documentation.
So, finally, how would you implement Popups like these http://developer.android.com/design/media/dialogs_popups_example.png and why?
If you want dialog as shown in the link, just make them by making custom dialog as mentioned below:
Make a dialog object:
Dialog dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);
Set custom view to this dialog:
show_dialog(){
dialog.setContentView(R.layout.custom_dialog);//your custom dialog layout.
}
Your custom layout should be like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/custom_dialog_first_rl"
android:background="#android:color/black">
<!-- write code for rest of your UI here -->
</RelativeLayout>
Now set alpha for your first relative layout in show_dialog() like this:
show_dialog(){
dialog.setContentView(R.layout.custom_dialog);//your custom dialog layout.
RelativeLayout custom_dialog_first_rl=(RelativeLayout)dialog.findViewById(R.id.custom_dialog_first_rl);
custom_dialog_first_rl.getBackground().setAlpha(170);
}
Call show_dialog() where you wanna show this dialog

Categories

Resources