Android activity Background affects alertDialog - android

I have a style I created like this:
<style name="myStyle" parent="#android:style/Theme.Dialog">
<item name="android:textColor">#FFFFFF</item>
<item name="android:background">#000099</item>
</style>
Then I created an xml and an activity, and in the manifest declared this
<activity android:name=".Try"
android:theme="#style/myStyle" >
</activity>
Then when I start this activity I have an xml with a background color (blue);
THe problem is , when I create an Alert Dialog
AlertDialog.Builder dialog = new AlertDialog.Builder....
It is also affected by this background (It looks more like a blue rectangle behind it that is bigger then the dialog and is coming out from all 4 sides).
I dont want it to be, I want to use another style for the alert dialog.
How can I disable this?

Are you saying that the background of your activity is too prominent when the dialog pops up? If so, you can blur what's in the background with the following code:
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Or are you saying that the background of your activity is being applied to the background of your dialog...?

I managed to fix that, well not entirley but to pass around it.
Instead of using:
#000099
I used
IMAGE_NAME
and I added a green rectangle image at the "drawable" folder.
This makes the background to become from an image instead of painting it, and the image does not affect the Dailog.
It will look a bit different (for example it will cover the white border as well) but it is better then before.

Related

Possible overdraw: Root element paints background in Theme.Holo

I have read similar questions for this problem but i couldn't find my answer anywhere. In order to solve the overdraw problem i use the following:
<style name="MyTheme" parent="#android:style/Theme.Holo">
<item name="android:windowBackground">#null</item>
</style>
When i use this in my activity which has an ActionBar, the background color of the action bar also changes to null. How can i fix this? If i set the color of the action bar separate the blue divider is being disappeared. How can i set to null the background of the frame below to action bar divider?
I'd suggest the following:
Create 2 separate themes: one for the activity with the action bar, the other one for the one without it.
For the activity without the action bar, use the theme you provided in your post, i.e.:
<style name="MyTheme" parent="#android:style/Theme.Holo">
<item name="android:windowBackground">#null</item>
</style>
For the activity with the action bar, simply create a theme which doesn't have the android:windowBackground item but instead sets a specific background colour that you want to have in that activity. Then, in the layout file that you inflate for the activity, delete the android:background attribute (because it'll already be set by the theme).
To make it a bit more visual, the theme would look like this maybe:
<style name="MyThemeForActivityWithoutActionBar" parent="#android:style/Theme.Holo">
<item name="android:background">#android:color/black</item>
</style>
And the activity's layout would maybe look like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black" // remove this
android:orientation="vertical" >
...
</LinearLayout>
I indicated which line I'd remove in the snippet above.
Does this help? :)
Solve your overdraw problem in a different way. Set the background you need in android:windowBackground and instead remove backgrounds from your layouts. Then you'll get action bar background back.
Using null window background is a bad solution anyway, exactly because it causes unintended side-effects, like rendering artifacts (or in your case missing action bar background). Chet Haase explains it in detail in Developing for Android VIII
The Rules: User Interface:
Avoid Null Window Backgrounds
One technique to avoid overdraw is to eliminate the window background
in situations where the views in the hierarchy all have opaque
backgrounds. That is, the user will not see the background of the
window if the view hierarchy completely covers that background with
one or more opaque views.
Eliminating the window background can be a valid technique, but it
tends to be a complicated way to solve overdraw issues, and can often
result in rendering artifacts in different situations. While it is
possible to set a null window background in the application manifest,
this can result in graphics artifacts due to the system not being able
to draw the starting window correctly. A more correct way to do this
is to leave the starting window background in place in the manifest,
but set it to null in the activity’s onCreate() method, by calling
getWindow().setBackground(null). But even this approach can cause
artifacts. For example, if the keyboard/IME is set to adjustResize and
is then animated in for an activity that has a null background, there
may be artifacts behind the keyboard as it animates in because the
window manager has nothing to draw for that background. Also,
fullscreen ListViews may have artifacts with overscroll bounce gaps
(which can be worked around with
ListView.setOverscrollFooter/Header()).
The correct way to address overdraw issues for this situation is to
actually use the starting window. Instead of having containers with
their own opaque background color between the window background and
views, put the background drawable you want on the window itself with
the windowBackground theme attribute and let those intervening
containers keep their default transparent backgrounds.

Custom dialog background is acting very strange

I am creating a small game for Android. At the moment I'm just creating the UI for the menu screen.
As I'm doing a wooden theme, I also want to use a custom dialog for showing highscores etc so it follows the theme.
I have found some good guides, but I have this very strange problem with the background of the dialog. The dialog is almost transparent.
What I have done:
- created a dialog_theme.xml with:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Dialog" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
created custom_dialog.xml with the elements I need (TextView for title and content, and button to close)
created a CustomDialog class which extends Dialog, and lets me build these custom dialogs rather easy with the content and title I want
using the CustomDialog in the activity to create the dialog
(the main guide I used for this blog.androgames.net/10/custom-android-dialog/ )
The problem is that the transparent background isn't always transparent (showing the activity ui in the background). I have 4 custom buttons in this menu. Problem is that instead of just showing the dialog transparent and showing the whole ui in the background, then one of the images for a button is stretched and fills the whole dialog background. If I just use a standard background for this one button then the dialog background is transparent and shows the activity ui in the background as it should.
As I might have been bad at explaining I will show pictures of what I mean:
- Code for the button that causes the problem:
<Button
android:id="#+id/id_about_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/id_achievements_button"
android:layout_marginTop="15dp"
android:background="#drawable/selector_about" />
Gives this result: (sorry but I can't use pictures directly in the post yet)
http://dl.dropbox.com/u/2980431/wrong.png
Modifying the button code to:
<Button
android:id="#+id/id_about_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/id_achievements_button"
android:layout_marginTop="15dp"/>
Gives this result:
http://dl.dropbox.com/u/2980431/correct.png
Hope someone got an idea about why this is happening, and a solution to fix it - to be honest I am totally lost.
Still not sure what happened. In another project I came across the same thing - custom semi transparent dialog background, got another drawable added to the background. Renaming the wrong drawable showing in the background, and then clean the project fixed this for me.
Strange.

Theme.Holo.Dialog text is unreadable

I have an Activity containing a single EditText and a Button that uses a custom style, derived from Theme.Holo.Dialog. The Activity is defined as:
<activity
android:name=".activity.MyActivity"
android:label="#string/activity_myactivity"
android:theme="#style/Dialog">
The style Dialog is defined as:
<style name="Dialog" parent="#android:style/Theme.Holo.Dialog">
</style>
However, when the activity is launched, the text in the EditText is black on a black background, making it unreadable. I can change this in the above style definition by adding <item name="android:textColor">#color/white</item>, but I feel like I must be doing something wrong, as the pre 3.x Theme.Dialog seemed to have the text color, etc already defined. (I would use that but am unhappy with the inconsistency with the dialog with the rest of my app that is using the Holo theme)
Update : I tried your code on ICS seems to work fine..
Set android:setInverseBackgroundForced="true" on your activity

Text color doesn't change in an AlertDialog when applying an app wide theme

(Note: build minimum and target API 7)
Ok, here is a real stumper for this newbie between the chair and the keyboard:
I am applying a them to my app, and using AlertDialog for some key information at a few key places (i.e. a EULA pop up on first app run). My problem is this, everything is fine until I apply a theme (or style to the Activity). My text everywhere but the pop ups formats correctly. The problem is that I am changing from the default white text on black back ground to black text on white background. The background changes on the pop ups but not the text, so the net effect is that I have a white pop up with the text there (scroll bars show for the long winded EULA) but the text is unreadable because it is the exact same color as the background.
Here is the my_style.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="main">
<item name="android:background">#FFFFFF</item>
<item name="android:textColor">#000000</item>
<item name="android:typeface">sans</item>
</style>
</resources>
I know I am implementing the call correctly because everything else in the app formats correctly, what am I missing? The app works just fine when the android:theme="#style/main" is removed from the <application> tag in the manifest file (formating removed from the entire app and the dialogs are readable). Thanks for getting a newbie set straight.
Have you passed the theme to AlertDialog's or AlertDialog.Builder's constructor when creating the dialog?
See here.

How to create a completely custom Dialogue/Popup in Android (change overlay colour and dialogue window layout)

I would like to completely re-skin the default dialogue component in Android. Specifically I would like to do this:
Change the semi-transparent overlay background from the default black to a semi-transparent white.
Change the Dialogue window by
removing the default windowed frame border,
and replacing it with a layout
defined in XML (it's just going to be
a borderless graphic with floating
buttons. no actual frame.)
I have seen tutorials about creating a custom layout for within the dialogue box (e.g. http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application), but I haven't seen anything regarding changing the colour of the overlay and/or completely customizing the dialogue window that pops up and turning it more into an overlay with no "window".
I've solved this problem and created my own custom popup overlay with a custom coloured semi-transparent overlay background using the following steps:
1 - Create a new xml file in your res/values/ folder and name it styles.xml
2 - Here is where you will define your dialog properties. Here is what mine looks like. If you want to replace the default semi-transparent black overlay that shows over the screen, you have to set windowIsFloating to false, and modify the background of your layout to be whatever colour you want. Here is my file below that I've used:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomDialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#color/transparent_white</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
3 - Back in your java code, when creating the dialog object, use the constructor that passes both the context AND the theme. Eg. myDialog = new Dialog(this, R.style.CustomDialogTheme); (CustomDialogTheme is the name attribute I specified in the styles.xml from step 2)
4 - Simply set your dialog objects content view to whatever layout you want your dialog to look like. Eg. myDialog.setContentView(R.layout.my_custom_overlay);
If you want your dialog to appear at the center of the screen, set its root element's android:layout_gravity to center
This worked great for me, but is missing how to close the dialog. if you have a button in your custom layout to close it, here is how to add the listener and close the dialog window.
final Dialog d = new Dialog(this,R.style.CustomDialogTheme);
d.setContentView(R.layout.custom_dialog);
d.show();
Button close_btn = (Button) d.findViewById(R.id.close_btn);
close_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
d.dismiss();
}
});

Categories

Resources