Activity should be transparent, but has black background - android

My use case is writing an overlay controller activity for a landscape camera preview. I followed the instructions from a couple of tutorials for writing a transparent theme.
So my res/values/style.xml looks like this:
<resources>
<style name="Theme" parent="android:Theme" />
<style name="Theme.Transparent">
<item name="android:windowBackground">#drawable/transparent_background</item>
</style>
<drawable name="transparent_background">#00000000</drawable>
</resources>
The activity snippet:
<activity android:name=".CameraPreview"
android:label="Camera"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Controlls"
android:label="Controlls"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Translucent">
</activity>
When I start this activity from my root activity, the layout gets drawn correctly, but the background stays black. I tried to use #android:style/Theme.Translucent instead, but this Theme inherits the orientation from the calling activity (landscape) and thats not what I want.
Edit:
The application holding the camera preview is set to landscape view as it does not display the preview correctly in portrait orientation. (see old google bug report)
What I wanted to do was to put an independent activity for user interaction interface in front of the camera surface holder (this activity should be set to 'portrait', or even better to 'sensor')

Here's a somewhat related answer for anyone else that has a similar problem.
tl;dr
Adding a new style where the name is a suffix of an existing style can cause problems (like making transparent activities have a black screen).
Problem:
Our transparent activity background was black after doing a large refactor. (The activity was already using a transparent theme.)
After several hours of going through commits I found what seemed to be the cause of the problem. In our app we use styles like CSS. There was an existing style like this that we applied to a TextView.
<style name="HeadLine.SM.White.MyFontBold">
<item name="android:fontFamily">#font/some_bold_font</item>
</style>
The non-bold variant of the style was added as a style before the bold variant as below.
//This style was added
<style name="HeadLine.SM.White.MyFont">
<item name="android:fontFamily">#font/some_font</item>
</style>
<style name="HeadLine.SM.White.MyFontBold">
<item name="android:fontFamily">#font/some_bold_font</item>
</style>
For some reason, after the non-bold style variant was added all transparent activities had a black screen. When we changed the non-bold variant style name to something else it fixed the problem for us.
Now our styles look like this (I know there are better ways to handle font styles - these styles are a few years old).
<style name="HeadLine.SM.White.MyFontRegular">
<item name="android:fontFamily">#font/some_font</item>
</style>
<style name="HeadLine.SM.White.MyFontBold">
<item name="android:fontFamily">#font/some_bold_font</item>
</style>
Conclusion
It seemed that adding a new style where the name is a suffix of an existing style caused problems. If you're adding a new style make sure the name is not a suffix of an existing style.
We did try cleaning the build, rebuilding, and invalidating Android Studio caches. None of these things solved our problem.

Try the built-in #android:style/Theme.Translucent instead of your custom one, according to this blog post. I haven't tried to do this myself, so I don't know if the technique written about there works or not.

I found out, that another important child element for the style description above is <item name="android:windowIsTranslucent">true</item> was lacking.
Problem:
That child element also causes synchronizing the activity's orientation with the calling one. (same effect as #android:style/Theme.Translucent)

I bumped into the same problem as you describe (regarding translucent background and screen orientation), but in the end I reconciled with the fact that this is just how it works. In fact it actually makes sense that it works this way. I can't think of any screen based system that supports mixing portrait and landscape views, so why should Android?
I guess the general rule is that all visible activities must have the same orientation, regardless of the attributes in the manifest-file. If all are set to "sensor" then they will all change, if one is fixed to portrait or landscape, then the others must follow (and whoever sets it last "wins").
I guess this was just so obvious to the developers that it didn't occur to them to document it :)

Remove and all its done
#Override
public void onAttachedToWindow() {}

Style name is having some effect on the transparent background to be black. I used below one and works fine.
<style name="Theme.AppCompat.Translucent">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
</style>
<!-- AndroidManifest.xml -->
<activity android:name=".TranslucentThemeActivity"
android:theme="#style/Theme.AppCompat.Translucent"/>

Related

Disable Google Sign in Transition for Single Screen

After signing in with the googleSignIn() method in flutter, there is an undesired transition appearing on the screen (Example : https://i.stack.imgur.com/CWIhL.gif).
This is not a glitch for the Android emulator, it also happens on my physical device.
The only solution I found was to add the following line to the styles.xml to disable the Transition :
<item name="android:windowAnimationStyle">#null</item>
However, I do not want to disable the animations for the whole app. How can I only disable it for the Login Screen ? I do not know what side effects may occur disabling all windowAnimationStyle for the whole app ?
You might be able to create a style for an individual view or a theme for a particular activity or view hierarchy. An example of this is given in the following link: https://gist.github.com/cesarferreira/38bf26a68317ac15f1dc
<!-- Developer should create a style -->
<style name="noAnimTheme" parent="android:Theme">
<item name="android:windowAnimationStyle">#null</item>
</style>
<!-- Then in manifest set it as theme for activity or whole application. -->
<activity android:name=".ui.ArticlesActivity" android:theme="#style/noAnimTheme">
</activity>
More information on Styles and Themes are given here: https://developer.android.com/guide/topics/ui/look-and-feel/themes#Styles

Unity android "freezes" with splash image

I have added a custom theme with background image to serve as "splash" image to make the app look nicer while unity player is loading.
That's the theme definition I have
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Test" parent="#android:style/Theme.Light.NoTitleBar.Fullscreen">
<item name="android:windowBackground">#drawable/launchimage</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
Now, when I refer to this theme in AndroidManifest.xml like:
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:theme="#style/Theme.Test" ... ></activity>
the game just "freezes" – it's hard to tell in fact, because it just displays the splash image and ... that's it. Logcat doesn't show any warnings/errors either.
Removing the theme OR changing android:windowBackground property to a solid color make the game work, but window background appears black until the game is launched.
What is the correct way to set activity background to make Unity happy?
When your image is too large this issue happen. change your image with smaller and set centerCrop attribute may fixed this issue. (usually this show skip frame in your log)
Also if you use Android studio 2.0 there is newly issue with that you can find it here.

AppCompat Dialog Theme with Mis-colored titlebar - Bug?

I have a LoginActivity where I use an AppCompat theme like this:
<activity
android:name=".LoginActivity"
android:theme="#style/Theme.AppCompat.Light.Dialog"
android:label="Login" />
I am aware that as of this post Google has not yet added Material Themes in AppCompat library for DIALOGS, so I assumed it will fall back on Holo. Instead, this is what I get:
Keep in mind, I am not using the AppCompat toolBar. In the Activity, I am not even making a reference to the ActionBar. What you see above is default behavior, yet I cannot figure out where it is coming from. Is this a bug perhaps?
(Also, the EditText fields are not being colored with the Primary color for the app.)
Note: see my final edit for possibly the best solution
For what it's worth, I do think this is a bug. However, a valid workaround that I discovered is to use #style/Base.Theme.AppCompat.Light.Dialog.FixedSize. Based on your screenshot I think this will work for you as well. However, I have not tested palette coloring yet.
From what I can tell in my testing, this extends the gray border while still allowing you to use AppCompat and v21.
Edit: one side-effect is it now appears that all dialog activities are the same size, which may not work for you. Also, I haven't figured out how to remove the title - requestWindowFeature and supportRequestWindowFeature with Window.FEATURE_NO_TITLE seems to be causing
java.lang.RuntimeException: Unable to start activity ComponentInfo{myclass}:
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
even though I've tried it before and after super.onCreate and definitely before setContentView
Edit#2: Removing the title via XML theming works, and since you have no title there is no bizarre gray box to worry about, which means you can drop the FixedSize setting and the dialog will wrap it's content like it did in earlier versions.
<style name="MyActivityDialogTheme" parent="Base.Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
Edit #3: You can also just simply remove the gray background - this may be the best solution because it does not require the Base. prefix:
<style name="MyTitledActivityDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">false</item>
<item name="android:windowTitleBackgroundStyle">#android:color/transparent</item>
<item name="windowActionBar">false</item>
</style>

Themes in android

A create a simple Theme as
<style name='one'>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
However on viewing in the emulator the screen goes black.when i do not apply theme the screen has a white background .
what really happens here.i am just starting with android.
In addition ,if a apply a theme to my activity then the attributes of the theme applies to all components of my activity say button,textfields and edittexts .
why would i then write
android:textSize=?android:textSize
to reference value from the theme for any button in my layout when the same value would already be applying.
is the syntax above the correct way to reference an attribute from my theme to assign to attribute for any view in my layout.
thanks
tejinder
Yeah, so you need to do a little more reading.
Let's start with the basics,
You need to understand the differente betweent an Attribute, a Style, and a Theme.
An Attribute is something that can be styled. For instance: android:textSize is an attribute that can have any value.
A Style is a set of specific attributes that will be applied to a Widget. They are defined
in your /values/styles.xml
For instance:
<style name="normalTextThin" parent="android:Widget.Holo.Light.TextView">
<item name="android:gravity">left|center_vertical</item>
<item name="android:padding">8dp</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">16sp</item>
</style>
The styles can be applied either as part of a theme or directly as theme-independent.
Theme-indepentent styling of a widget is like this:
<TextView
android:id="#+id/text"
style="#style/normalTextThin"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
You are then theming only that one TextView.
A Theme is a collection of Styles that can be applied to a part of your UI, such a a whole Activity, or your whole Application.
For instance:
<style name="AppTheme" parent="android:Theme.Light">
<item name="android:editTextStyle">#style/EditTextAppTheme</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
<item name="android:imageButtonStyle">#style/ImageButtonAppTheme</item>
</style>
Here, we are declaring that all EditText in your application will use the style named EditTextAppTheme, and so forth and on. When done like this, in order to actually have the theme be active, you declare it in the manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
That means that you are not required to declare the style on each widget you create.
<EditText
android:id="#+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_search">
<requestFocus />
</EditText>
That widget right there would already be styled using EditTextAppTheme without the need of you explicitely declaring so.
I recommend you try to read on what attributes can be styled, how to style them, and so forth and on.
If you don't want to though, it's fine, you can still get a lot done with the following tools for styling:
ActionBarStyleGenerator to help you create styles for the ActionBar.
Android Holo Colors to help you style standard widgets.
Hope that helps.
Additional Info
Let me clarify on the whole ?attr/attributeName
The ? means that the system will choose the specific attributeName value for the current Configuration (not specific to different themes). This should be used only when you want the value to be different on different configurations. For example:
?android:attr/actionBarSize
This line is a dimension, and it will be different not based on the current theme, but on the current device screen size and orientation (values, values-land, values-sw600dp).
It's important to know that specifying ?android: means you are accessing preset Android values, not yours. If you have or want to create and use your own attribute values for specific configurations, you must do the following:
Create a file named attrs.xml on your /values/ folder.
Declare the desired custom attribute:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<attr name="my_custom_attr" format="reference" />
</resources>
Declare a value for the custom attribute, let's say on your own theme.
<style name="AppTheme" parent="android:Theme.Light">
<item name="my_custom_attr">#resource_type/resource_name</item>
<item name="android:editTextStyle">#style/EditTextAppTheme</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
<item name="android:imageButtonStyle">#style/ImageButtonAppTheme</item>
</style>
And then you can use it on the Widget you'd like:
Hope that clears things out.
EDIT 2
For a better answer to your question, please update your question. And like I said, read more on how to properly create styles.
The Theme named 'one', what do you want to apply it to? An activity, a Widget, the whole Application?
How are you applying the theme? Show the lines of code where you specify the usage of theme 'one'.
Your theme as you specified is simply not a properly constructed theme/style.
<style name='one'>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
This says absolutely nothing, and it is definitely not suitable for an Activity-level theme. The reason you specify a parent is so your theme can inherit all of the attributes from the parent, and then you specifiy which ones to change.
For instance, if you want to use your theme and have a light background, do this:
<style name='one' parent="android:Theme.Holo.Light>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
But even here, despite the fact that it will apply, you don't want to have the same text color and size for the whole application do you? That'd be nonsense, different text color and sizes account for a big part of the user experience, so rather than setting those values from what we can refer to as the main style, we can create substyles and apply them to certain widgets.
I can't really go any more detailed that what I already have, the above explains how to accomplish Widget-specific styling, and activity/application level theming.
For a complete start-up guide, read the Android Developer Site, try the test styles declared there, see how they work, and until then try to create your own, don't try to create something out of nowhere if no reading has been made.

How to set the background screen as Wallpaper all the time?

Currently, I'm using this to show my application background as phone wallpaper.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER,
WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
But for some reason when I start my application by pressing the icon. It just shows the activity screen with the icons on the home screen. I didn't use dialog but it looks like a dialog because layout is just set that way. So I just want to show the wallpaper whenever this activity is running. But it only shows the wallpaper only after the next event occurs such as switching to different activity. I already put that code on onCreate() and whenever I do setContentView()..... Is there way to do such thing or there is just no way?
For users of AppCompat, just use the following in your styles.xml, no need for code:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowShowWallpaper">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
After long search and trial and error. I've found the solution to what I wanted. It was just creating separate themes.xml file and just tweak the Theme.Dialog which is already defined in default android themes.xml. All I did was change the Animation part. Originally in android themes.xml the line looks like this.
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
but since modifying in android themes.xml doesn't take the effect. I just created my own themes.xml as I said above and just set parent as android:Theme.Dialog. And added a line like this.
<item name="android:windowAnimationStyle">#android:style/Animation</item>
Thanks for the help and I hope this solution helps others.
Use following code -
rl = (RelativeLayout)findViewById(R.id.someid);
//relative layout is my root node in main.xml (yours may be linearlayout)
WallpaperManager wm = WallpaperManager.getInstance(this);
Drawable d = wm.peekDrawable();
rl.setBackgroundDrawable(d);// You can also use rl.setBackgroundDrawable(getWallpaper);

Categories

Resources