I was able to create a floating activity after following this tutorial http://cases.azoft.com/android-tutorial-floating-activity/
However, to do so, I had to add this line in styles.xml
:
<item name="android:windowIsTranslucent">true</item>
Is it possible to have the same effect using only Android/Java code ? (e.g. in Activity.onAttachedToWindow()
or so...)
Thanks in advance for your help.
[EDIT 01] styles.xml
must not be changed (and I'm not supposed to know what's in it...). But for testing purposes, I'm using the default one:
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
</resources>
[EDIT 02] Resources.Theme.applyStyle() seems to do what I want (according to the API description: "Place new attribute values into the theme" ).
So I created the following custom_style.xml
:
<resources>
<style name="MyCustomStyle" >
<item name="android:windowIsTranslucent">true</item>
</style>
</resources>
Then, in onAttachedToWindow()
, I called:
getTheme().applyStyle(R.style.MyCustomStyle, true);
But it didn't have any effect...
Is it possible to have the same effect using only Android/Java code ?
I am afraid, No. You will have to do it in styles.xml
only. AFAIK value of android:windowIsTranslucent
alone can't be changed programatically.
When we call super.onCreate(savedInstanceState);
the Activity class provides empty graphical window on which we set our content ie.views. And a theme is applied to this window and then the content are loaded on this view.
Hence the sequence will be,
super.onCreate()
eg.
styles.xml
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowBackground">@drawable/background</item>
<item name="android:windowNoTitle">true</item>
</style>
<!-- Application theme.without title bar -->
<style name="AppTheme.NoTitleBar" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowBackground">@drawable/background</item>
<item name="android:windowNoTitle">true</item>
</style>
<!--Floating activity theme -->
<style name="Theme_Translucent" parent="android:style/Theme.NoTitleBar.Fullscreen">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowFullscreen">true</item>
</style>
Then set your theme for Floating activity
as follows:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTheme(R.style.Theme_Translucent); // Set here
setContentView(...)
}