I'm new to android development and wanted to make a tabbed application, so I started with google's file form the documentation and I would like to remove the most above part of the action bar (where the logo and the app name stands). I've tried styling it like this:
<resources>
<style name="ThemeHoloWithActionBar" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarTabStyle">#style/ActionBarTabStyle</item>
<item name="android:actionBarSize">50dip</item>
</style>
<style name="ActionBarTabStyle" parent="#android:style/Widget.Holo.ActionBar.TabView">
<item name="android:minHeight">50dip</item>
</style>
hoping that it would push the size of the other bar to 0 but it didn't work.
Edit:
when using:
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
I Can't get the error formatted correctly so I uploaded an image :
http://i.imgur.com/ED7InpD.jpg
If you don’t need the action bar, you can remove it from your entire app or from individual activities. This is appropriate for apps that never used the options menu or for apps in which the action bar doesn’t meet design needs (such as games). You can remove the action bar using a theme such as Theme.Holo.NoActionBar or Theme.DeviceDefault.NoActionBar.
in style.xml add this:
<resources>
<style name="NoActionBar" parent="#android:style/Theme.Holo.NoActionBar">
<!-- Your style here -->
</style>
</resources>
in AndroidManifest.xml add this:
<!-- [...] -->
<application android:name="#style/NoActionBar"
<!-- [...] -->
or try with this:
<!-- [...] -->
<application android:theme="#style/NoActionBar">
<!-- [...] -->
To remove actionbar, you need add:
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
to your AppTheme style file, after remove, then you can not use default actionbar to add tabs.
If you still want to use tabs, then you have to use SlidingTabLayout to make a custom ViewPager title strip. There is Android sample project SlidingTabsBasic, which show detail how to make custom tabs.
Related
Long story short, I have an app in which I applied style attributes directly on activities and fragment's XML files. And now I am refactoring that into a styles.xml file. I barely dare to mess with modifying the theme itself directly, because I already tried something similar before and inheritance became chaos.
So, I have several styles like this in my styles.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<style name="My_DarkMode" parent="Theme.MyApp">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/purple_500</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
</style>
<style name="My_DarkMode.MaterialToolbar">
<item name="android:background">#color/purple_500</item>
</style>
<style name="My_DarkMode.TextView">
<item name="android:textColor">#color/white</item>
</style>
<style name="My_DarkMode.HighlightedText">
<item name="android:textColor">#color/yellow_4text</item>
</style>
<style name="My_DarkMode.TabLayout">
<item name="android:background">#color/tab_grey</item>
<item name="tabIndicatorColor">#color/tab_indicator</item>
<item name="tabSelectedTextColor">#color/tab_selected_text</item>
<item name="tabTextColor">#color/tab_text</item>
</style>
<style name="My_DarkMode.FragmentBg">
<item name="android:background">#color/black</item>
</style>
<style name="My_DarkMode.MainBg">
<item name="android:background">#color/activity_grey</item>
</style>
<style name="My_DarkMode.SearchBar">
<item name="android:background">#color/white</item>
<item name="android:textColor">#color/black</item>
</style>
<!-- etcetera... -->
So, I replaced the hardcoded atributes by one of these styles in the XML objects within activities and fragments. This works fine. (please notice that different XML "view objects" might use one or another of all these styles).
And here comes the question:
So, let's imagine that I take all those styles and make a copy of every single one of them, but with different name: instead of naming them My_DarkMode or My_DarkMode.*, I name them My_LightMode, and change completely the color palette for these "LightMode styles" (so, using them like some kind of theme whatsoever).
So, the idea would be switching programatically this:
<!-- It's a little pseudo-code, for summarizing the thing -->
<TextView
style="#style/My_DarkMode.TextView"
android:id="[...]" />
<TextView
style="#style/My_DarkMode.HighligthedText"
android:id="[...]" />
<!-- [...] -->
...into this (but for much more XML elements than this):
<TextView
style="#style/My_LightMode.TextView"
android:id="[...]" />
<TextView
style="#style/My_LightMode.HighlightedText"
android:id="[...]" />
<!-- [...] -->
What would be the proper way to, by Java (y'know, by clicking some button or something like that: I'm not concerned about how to trigger it for this question, but rather which instructions should I use), switching the styles to be applied? Can't I somehow group all those styles into a single theme or something? It looks like I have no choice but to do something similar to this, because if I apply some of this from the theme and let inheritance do its stuff, then the SearchView inherits white letters (with white background; so you can see what I mean by chaos)... but also, my knowledge about Android styles and themes is, indeed, limited.
For the record
I am aware that this approach of mine might not be a good one at all. Among other things: because of my ignorance about how Styles, Themes and style inheritance hierarchy works in Android. (I mean, maybe my XML code would be too WET if I keep this approach? I wonder...) If so, a more clean approach is welcome.
If this was HTML and CSS instead, I would be able to figure this out much more easier for some reason, or so I think...
EDIT:
For real: no matter how much I read docs like Themes or how the theme main color attribute work... the more I read, the more confused I get. Trial and error is not helping much either... recently I tried to remove all styles from XML layouts and start the theme from scratch; and trying what it is inside this link i find out that if I do this:
<item name="colorBackground">#color/purple_500</item>
...colorBackground just doesn't exist! One more of countless times that something didn't work as in the docs... sigh...
So here goes: the "theme from scratch". Forget all style= that I set up earlier: I removed them all.
My themes.xml:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- all these ones were before, I started tweaking
them and see what happens -->
<item name="colorPrimary">#color/purple_500</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/black</item>
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/white</item>
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- <item name="toolbarStyle">#style/My_DarkMode.MaterialToolbar</item> -->
<item name="toolbarStyle">#style/Widget.MaterialComponents.Toolbar.PrimarySurface</item>
<item name="drawerArrowStyle">#style/Theme.MyApp.DrawerArrowStyle</item>
<!-- and here I add new ones I din't knew before
and start playing with them like crazy -->
<item name="colorSurface">#color/activity_grey</item>
<item name="colorOnSurface">#color/white</item>
<item name="colorOnPrimarySurface">#color/yellow_4text</item>
<item name="colorContainer">#color/activity_grey</item>
<item name="colorOnBackground">#color/yellow_4text</item>
<!-- It doesn't exist... sigh... vVv -->
<!-- <item name="colorBackground">#color/black</item> -->
</style>
I am having a really hard time trying to imagine, discover or reach which general theme attributes to tweak for trivialities like having all TextViews showing text with the same color, or why colorSurface or colorContainer paint grey the <NavigationView>, but not the <ConstraintLayout>'s backgrounds?? I am trying to keep everything as similar as it was before, but with a centralized theme that I can switch from a single place programatically.
I read lots of links in "Android Developers" about styles and themes and how they are supposed to work together... but they don't clearly show howto! It would be great if there was a canonical way to join several styles in a single theme or something like that... so, you could apply different styles to different views by just switching the theme, just like it would work, more or less, with CSS classes.
Old:
(before wanting to extract a theme)
New mess:
(removed styles from Layouts and started modifying Theme from scratch)
How I can remove Action Bar on specific activity not for all application, for example I have Sign Up activity and for this activity I want to remove Action Bar
In your AndroidManifest.xml use NoActionBar theme for your Activity like this:
<activity android:name=".SignUpActivity"
android:theme="#style/AppTheme.NoActionBar"/>
and in your styles.xml
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
from within your activity:
getActionBar().hide();
or
getSupportActionBar().hide();
I would suggest to migrate to ToolBar, new Android API for the same purpose. The main benefit of ToolBar is that you can treat it like other views, it guarantees to you a more flexible approach.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
If you have an ActionBar, you can use getActionBar().hide();
If you have a SupportActionBar, you can use getSupportActionBar().hide();
Remove Action Bar on specific Activity go to Activity java file here your Activity class extends with AppCompatActivity remove the AppCompatActivity and extends with Activity and go to xml file and select Theme NoTitleBar or go to Manifests file select your Activity and add Theme on your Activity android:theme="#android:style/Theme.NoTitleBar"/>
For kotlin you can use supportActionBar?.hide()
As much as I learn, I still have a long way to go ...
I'm trying to use Facebook SDK to provide details for a sign up.
I am using a blue, yellow and white theme for my app; the text being white. My style setup is:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#android:color/holo_orange_light</item>
<item name="android:colorBackground">#color/colorMainBackground</item>
<!--<item name="android:actionBarStyle">#style/ThemeActionBar</item>-->
<!--<item name="android:windowActionBarOverlay">true</item>-->
<item name="android:textColor">#android:color/white</item>
<item name="android:textColorLink">#android:color/holo_orange_light</item>
<!-- Support library compatibility -->
<!--<item name="actionBarStyle">#style/ThemeActionBar</item>-->
<!--<item name="windowActionBarOverlay">true</item>-->
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:titleTextColor">#color/yellow</item>
<item name="android:textColorPrimaryInverse">#android:color/black</item>
<item name="android:navigationBarColor">?android:attr/statusBarColor</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
However, when Facebook shows a dialog box, all I get is a white square:
It would appear that Facebook are using my textcolor and hence the white box. Is there a way I can override this? Or have Facebook use the default colour scheme (if there is one).
This is somewhat frustrating me now.
My Manifest entry for Facebook is:
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/app_name" />
Thanks in advance
Facing the same issue. It seems FacebookActivity picks up the color from the style you declared in the application tag.
<application
...
android:theme="#style/AppTheme'>
You see it white because in styles.xml you have the accent color to white. for your theme.
<item name="colorAccent">#FFF</item>
Since in my application I need the colorAccent to be another color, I chose to have two AppThemes. One for my activities with whatever colorAccent I need and one for the application that will get used by Facebook.
I tried to add a theme to com.facebook.FacebookActivity but it gets ignored. Something to do with ManifestMerging.
<style name="Theme_Base_App" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
I simply copied the above code to my styles.xml file and I copied
android:theme="#style/Theme_Base_App"
into my manifest file. How do I now change the color of this action bar?
Also, can someone explain me what exactly I am doing by using these lines of code? What is the manifest file for? And what am I doing in the Styles.xml file?
To change your color just set the colorPrimary in your colors.xml file.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary">your color</color>
</resources>
Using this style you are defining your custom theme inheriting from AppCompat theme.
<!-- inherit from the material theme -->
<style name="Theme_Base_App" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">#color/colorPrimary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
</style>
You can refer this link for more info.
The Android Manifest file presents essential information about your app to the Android system. With
android:theme
you are defining a reference to a style resource defining a default theme for all activities in the application. Individual activities can override the default by setting their own theme attributes. For more information, see the Styles and Themes developer guide.
Here you can find more info about Manifest and application element in the Manifest.
I have different Activities in my App and in all of them I do not want the Action Bar. I cannot find how to disable it. I have tried to find an attribute to apply it to the main_activity.xml but so far I did not find anything. Can somebody help me please?
Haha, I have been stuck at that point a while ago as well, so I am glad I can help you out with a solution, that worked for me at least :)
What you want to do is define a new style within values/styles.xml so it looks like this
<resources>
<style name = "AppTheme" parent = "android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name = "NoActionBar" parent = "#android:style/Theme.Holo.Light">
<item name = "android:windowActionBar">false</item>
<item name = "android:windowNoTitle">true</item>
</style>
</resources>
Only the NoActionBar style is intresting for you. At last you have to set is as your application's theme in the AndroidManifest.xml so it looks like this
<application
android:allowBackup = "true"
android:icon = "#drawable/ic_launcher"
android:label = "#string/app_name"
android:theme = "#style/NoActionBar" <!--This is the important line-->
>
<activity
[...]
There are multiple ways of doing this.
1) Change the theme in Android Manifest
Below the Application element, you will find theme tag. Replace it with this one -
android:theme="#android:style/Theme.NoTitleBar"
If you are using AppCompat, use #android:style/Theme.AppCompat.NoActionBar.
This will set the theme for all activities. If you don't need the action bar in a specific acitivity, then set the theme in the activity container, ie
<activity android:theme="#android:style/Theme.NoTitleBar" ...>
You may also set android:theme="#android:style/Theme.NoTitleBar" theme in the styles and use it later on.
2) Create a Custom Theme
Add this in res/values/styles.xml
<style name="NoActionBar" parent="#android:style/Theme.Holo.Light">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
and then set it as your activity's theme in Manifest:
<activity android:theme="#style/NoActionBar" ... />
3) Dynamically Remove the Action Bar
Put this code in the onCreate method before setting content view -
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
If you are using the support library use getSupportActionBar().
If you're using AppCompat, declare your activity in AndroidManifest.xml as follows:
<activity
android:name=".SomeActivity"
...
android:theme="#style/Theme.AppCompat.Light.NoActionBar" />
Considering everyone is posting older ways of hiding the ActionBar here is the proper way of implementing it within styles for AppCompat support library. Which I highly suggest moving toward if you haven't already.
Simply removing the ActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
If you are using the appcompat support libraries, this is the easiest and recommended way of hiding the ActionBar to make full screen or start implementing to toolbar within your layouts.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Your Toolbar Color-->
<item name="colorPrimary">#color/primary</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#color/primary_dark</item>
<!-- colorAccent is used as the default value for colorControlActivated,
which is used to tint widgets -->
<item name="colorAccent">#color/accent</item>
<!-- You can also set colorControlNormal, colorControlActivated
colorControlHighlight, and colorSwitchThumbNormal. -->
</style>
Then to change your toolbar color
<android.support.v7.widget.Toolbar
android:id=”#+id/my_awesome_toolbar”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:minHeight=”?attr/actionBarSize”
android:background=”?attr/primary” />
Last note: Your Activity class should extend AppCompatActivity
public class MainActivity extends AppCompatActivity {
<!--Activity Items -->
}
Default style you getting in res/value/style.xml like
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
And just the below like in your activity tag,
android:theme="#style/AppTheme.NoActionBar"
If you want most of your activities to have an action bar you would probably inherit your base theme from the default one (this is automatically generated by Android Studio per default):
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
And then add a special theme for your bar-less activity:
<style name="AppTheme.NoTitle" parent="AppTheme">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="actionBarTheme">#null</item>
</style>
For me, setting actionBarTheme to #null was the solution.
Finally setup the activity in your manifest file:
<activity ... android:theme="#style/AppTheme.NoTitle" ... >
You may also change inheritance from ActionBarActivity to Activity as written here.
UPDATE
A good solution is here:
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
}
open Manifest and add attribute theme = "#style/Theme.AppCompat.Light.NoActionBar" to activity you want it without actionbar :
<application
...
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
...
</activity>
</application>
I will try to show it as simple as i can:
DECLARE FULL SCREEN ACTIVITY IN ANDROID MAINIFEST file:
<activity
android:name=".GameActivity"
android:label="#string/title_activity_game"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
Now in Android studio (if you using it) open your Activity xml file ( in my example activity_game.xml) in designer and select theme (above mobile phone in designer click button with small circle) and then select Mainfest Themes on the left side and then NoTitleBar.Fullscreen.
Hope it will help!
Please find the default theme in styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
And change parent this way
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
android:theme="#style/Theme.MaterialComponents.Light.NoActionBar"
Using this theme worked for me
Put this line in your Activity in the Manifest:
<activity android:name=".MainActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
...
</activity>
and make sure you didn't put Toolbar in your layout
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
/>
Create an new Style with any name, in my case "Theme.Alert.NoActionBar"
<style name="Theme.Alert.NoActionBar" parent="Theme.AppCompat.Dialog">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
set its parent as "Theme.AppCompat.Dialog" like in the above code
open AndoridManifest.xml and customize the activity you want to show as Alert Dialog like this
<activity
android:excludeFromRecents="true"
android:theme="#style/Theme.Alert.NoActionBar"
android:name=".activities.UserLoginActivity" />
in my app i want show my user login activity as Alert Dialog, these are the codes i used. hope this works for you!!!
It's Really Simple Just go to your styles.xml change the parent Theme to either
Theme.AppCompat.Light.NoActionBar or Theme.AppCompat.NoActionbar and you are done.. :)