I am showing a dialog made from Activity. The dialog activity has set no title in onCreate() callback:
public class MyDialogActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set window feature no title
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.my_dialog);
...
}
...
}
I defined a style for the activity:
<style name="Theme.MyDialog" parent="#style/Theme.AppCompat.Light.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="background">#android:color/transparent</item>
<item name="android:windowBackground">#drawable/my_bg</item>
</style>
And in AndroidManifest.xml:
<activity
android:name=".MyDialogActivity"
android:theme="#style/Theme.MyDialog">
</activity>
But when I show the dialog, there is always a title(which shows my app's name) on the dialog.
It is weird not only because it still shows title, but also shows my app's name as title, as you can see, I haven't set any title. It looks like an Android default behaviour, but...how to get rid of the title?
(I am running on Android 5.1.1)
You can try adding the following style to your Activity for imitating a dialog:
<style name="Theme.AppCompat.TranslucentDialog" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">#color/colorTransparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">#android:style/Animation</item>
</style>
where colorTransparent is the following:
<color name="colorTransparent">#64000000</color>
Then use it with:
<activity
android:name=".MyDialogActivity"
android:theme="#style/Theme.AppCompat.TranslucentDialog">
</activity>
Related
I am working on an Android app where I am using DialogFragment. I want to set my own color behind dialog fragment once it opens.
Right now it is showing like light black.
Please see this screenshot enter image description here
I'haven't tried yet, but you could try something like this:
dialogFragment.getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(<your color>));
I haven't tried this myself. But you can try it using styles. Set android:windowIsFloating to false and android:windowBackground to my custom color in the dialog style:
styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#color/orange_transparent</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowIsFloating">false</item>
<item name="android:gravity">center</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowTitleStyle">#null</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
</style>
</resources>
MyDialogFragment
public class MyDialogFragment extends DialogFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
}
}
I open activity with transparent style, it means that it has this theme at the start:
<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
After few moments I change theme
setTheme(R.style.AppThemeWithActionBar);
which looks like this:
<style name="AppThemeWithActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">false</item>
<item name="android:windowNoTitle">false</item>
<item name="android:windowIsTranslucent">false</item>
<item name="colorPrimary">#color/primaryDark</item>
<item name="colorPrimaryDark">#color/primary</item>
<item name="colorAccent">#color/accent</item>
</style>
But unfortunately, when I call getSupportActionBar() after changing theme I get null, what is the reason?
There are two ways to set a style:
Using AndroidManifest.xml
for Entire Application :
<application android:theme="#style/CustomTheme" >
</application>
for Individual Activity :
<application>
<activity android:theme="#style/CustomTheme" >
...
</activity>
</application>
Programmatically :
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.CustomTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
It's not possible to change theme at runtime without reloading whole activity. Because I don't want to reload activity, I will have to stop using ActionBar and start using Toolbar.
Once I switched from ActionBarActivity to AppCompatActivity one of the only changes I did was add this line:
<item name="windowNoTitle">true</item>
Here is my entire styles.xml file:
<style name="gptheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primaryDark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:windowContentOverlay">#null</item>
</style>
<style name="ThemeNoActionBar" parent="gptheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Now, all the new AppCompatDialogs in my code, (which were formerly Dialog d = new Dialog(mContext), all have no titles even though I use setTitle().
Obviously the change I made to the titles specified windowNoTitle but that should only affect the parent activity. I would think, anyway.
How exactly does this new feature work?
// style.xml
<style name="DialogActivity" parent="Theme.AppCompat.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="colorAccent">#fff</item>
</style>
// manifest.xml
<activity
android:name=".MyPopupActivity"
android:theme="#style/DialogActivity"/>
// activity
import android.app.Activity;
public class MyPopupActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_popup);
}
}
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?
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>
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
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
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>
I have an activity defined like this
<activity android:name=".queue.ItemDetailActivity"
android:theme="#android:style/Theme.Dialog"></activity>
This activity implements runnable and shows a progress bar while data is retrieved from the server. I would like to have the dialog invisible until the data is loaded. Is there a way for the activity to start invisible and then later use setVisible(true); to make it appear?
Try this style for the activity.
<style name="Invisible" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>