Android how to get AppCompat.Translucent type theme with support actionbar?
Asked Answered
R

4

70

I would like to add the support actionbar to one of my activities, I previously had been using the theme.translucent with this activity but in order to make the support actionbar work I needed to inherit the Theme.AppCompat, I need to maintain a translucent theme in this activity but unfortunately there isnt a Theme.AppCompat.translucent that i can see by default, is there any way that this can be done?

Resnick answered 31/12, 2013 at 19:16 Comment(0)
T
168

You can create a new set of styles to use which have the same properties as Theme.Translucent from themes.xml.

Add the following to your styles.xml file:

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

You can change the prefix Theme.AppCompat to something else if you want to inherit other things from the theme such as dialog styles etc. For example, a name like Theme.AppCompat.Light.Translucent would have the properties of the Light theme.

To use the new style, set the theme property to @style/Theme.AppCompat.Translucent

<activity
    android:name=".TranslucentActivity"
    android:theme="@style/Theme.AppCompat.Translucent" >
</activity>
Trina answered 1/2, 2014 at 0:30 Comment(5)
Wanted to add a Alertbox to an activity, but wanted to have a translucent ctivity, this is a perfect solution. Added another suggestion as an UPDATE to existing answer.Preindicate
Have one issue with defined Theme. When I apply above theme to <activity> orientation is not working for it, but if I remove above theme orientation is working properly.Rimple
But I am getting Black ToolBar in the transparent activity. Please help me to fix my bug.Engrail
it's not good, the right way to add needed attributes to (e.g.) <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">... I've added <item name="android:windowIsTranslucent">true</item> to my AppCompat theme so there is no white background when app startsKaiulani
This is still showing the action bar. To eliminate the action bar just add a parent like: <style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">Insincerity
G
39

Parama ,

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
        <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>

This should be the style header if you want the toolbar to disappear.you can use any parent theme which has NoActionBar for other effects.

Hope this helps

Gorham answered 17/2, 2016 at 5:5 Comment(0)
S
10

If we use Translucent for transparent activity. It raises other issues - the color of Msgbox (now white previously black), Default dialog color, the spinners do drop down but do not show the underline and drop-down arrow. The spinners are color black text black; drop-down white drop-down text black and etc. To overcome this problem, you can just use below code

In style

<style name="Theme.AppCompat.Transparent.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
 <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

In manifest file

<activity
        android:name=".activity.YourActivityName"
        android:theme="@style/Theme.AppCompat.Transparent.NoActionBar" />

I hope it will help Thanks

Spire answered 9/7, 2018 at 11:14 Comment(3)
Thanks for the helpRinglet
I used https://mcmap.net/q/46672/-android-how-to-get-appcompat-translucent-type-theme-with-support-actionbar to create transparent activity, but I had color different issues. But this answer helped me to over come from color different issues as @Spire mentioned on answer. ThanksCuenca
uggh, none of these solutions seem to work anymore, I'm continuously getting the black screen.Mattern
E
4

Cameron's answer is a nice hack , but it produced a floating action bar and tinted my status bar, which i didn't wanted . So i added more xml attributes for making status bar transparent(for sdk >=19) and used java code for making action bar invisible.

mainActivity.java :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
     ...
    }
     ...
}

styles.xml

<style name="AppTheme.TranslucentBG">
    <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>

    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>

</style>

manifest.xml

<application
    android:icon="@mipmap/ic_launcher"
    ...
    android:theme="@style/AppTheme"
    ...
    >


    <activity android:name=".MainActivity"
        android:theme="@style/AppTheme.TranslucentBG"
        ...
        >
        ...
    </activity>
</application>
Electrolysis answered 6/9, 2018 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.