Change an action bar color in Android - android

this question is already ask on stack overflow but didn't work for me.
I want to change my action bar color only like facebook app the top action is in blue color and rest of the thing has white background color.
I tried using the post in stack overflow but, it's change my full activity color to white instead of just only the action bar.
Here is my style.xml and
<style name="CustomActivityTheme" parent="#android:style/Theme.Holo">
<item name="android:actionBarTabStyle">#color/blue_base</item>
<!-- other activity and action bar styles here -->
</style>
manifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/CustomActivityTheme">
Thanks.

You can't use a color directly, you have to use a drawable :
action_bar_bg.xml :
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/blue_base"/>
</shape>
Then this should work :
<item name="android:actionBarTabStyle">#drawable/action_bar_bg</item>

Now I realized where you did wrong. You can't pass color to actionBarTabStyle, you have to pass style. Instead you can define your style and pass it to it like this,
<style name="MyActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#drawable/ab_background</item>
</style>
This example from Android Developers page, if you take a look at there they explain very well.
There is another good article also on Android Developers Blog

Related

Is there any way to change default pressed color for all listviews in android?

The application I'm developing has several lists of items (including one in NavigationDrawer, one in a dialog, etc).
When the user press one item on the list on pre-lollipop smartphones, the background changes to a light blue color (including preferences on PreferenceActivity). On lollipop devices, however, the background changes to a light grey color with a beautiful ripple effect. What I want is to have the same light grey color for all devices (the ripple effect is not needed).
I've seem some posts of people styling ListView items setting backgrounds, but I need an approach to style all of them at once.
ps: my app uses Theme.AppCompat.Light.NoActionBar as base theme.
Create a file res/drawable/listitem_background.xml with the following content :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#color/purple_dark" />
<item android:drawable="#android:color/transparent" />
</selector>
Replace the #color/purple_dark with the color of your choice.
Then, in your theme, add the following line :
<item name="android:activatedBackgroundIndicator">#drawable/listitem_background</item>
EDIT:
To mantain the lollipop effects and colors instead of placing the theme in the values folder, place it in the values-v22
Define a style resource file like this one
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.AppCompat.Light.NoActionBar">
<item name="android:listViewStyle">#style/MyListViewStyle</item>
</style>
<style name="MyListViewStyle" parent="android:Widget.ListView">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<!-- Other stuff you need -->
</style>
</resources>
In your manifest set
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyTheme">
Now all of your ListView's will look like you define in style by default. You can of course change some of them if you want.
You can find a more complex, but very useful sample HERE

Any easy way to change Android Action Bar "selected" color?

I'm able to change a lot of colors through using the Android style documentation from here and looking at the source here and here.
Using what I learned above, I was able to change the ActionBar.TabBar, ActionBar.TabText, and ActionBar.TabView, but I don't know what that little highlight is called. Searching for a holo blue in the android source code from above also didn't help.
I still don't know if there is any way in styles.xml to specify that "blue" highlight color to be changed.
EDIT:
I know that I can use the the action bar style generator, but I'm looking at if I can set the color of it in code (whether it be java or xml). I would not like to have to generate a new drawable for this purpose.
The easiest way You can have is to use Android Action Bar Style Generator to easily theme your action bar and tabs.
You can also refer ActionBar Documents for a customized ActionBar.Something like
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActivityTheme" parent="#android:style/Theme.Holo">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- other activity and action bar styles here -->
</style>
<!-- style for the action bar backgrounds -->
<style name="MyActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#drawable/ab_background</item>
<item name="android:backgroundStacked">#drawable/ab_background</item>
<item name="android:backgroundSplit">#drawable/ab_split_background</item>
</style>
</resources>
Alternatively you can try ActionBarSherlock.There is one sample available in the library named "Style ActionBar".Using this you can change ActionBar tabs underline color.
I also see a useful link in a comment to your question.You are good to go after referring all these links.
Hope this helps.

Unexpected effect of Holo Light Theme of my action bar

I have used XML to define the Theme of the action bar for my Main activity to be as follows:
<style name="AppTheme" parent="#android:style/Theme.Holo.Light" />
<style name="HomeTheme"
parent="#style/AppTheme">
<item name="android:actionBarStyle">#style/HomeActionBarStyle</item>
</style>
<style name="HomeActionBarStyle"
parent="#android:style/Widget.Holo.ActionBar">
<item name="android:displayOptions">showHome|showTitle</item>
</style>
Then in my Manifest file I have set the Theme of my activity with the defined Theme :
<activity
....
android:theme="#style/HomeTheme"
....
</activity>
everything went file as expected except two thing:
1) the border line of the action bar area is still blue
2) the background color of the action bar area is white not grey.
I am not sure what could be my mistake or this is just a bug in my emulator?
I need to solve the previous two points just to have beautiful GUI
Change
parent="#android:style/Widget.Holo.ActionBar"
to
parent="#android:style/Widget.Holo.Light.ActionBar.Solid"
By default the standard ActionBar has the blue line under it instead of the gray. The solid variant is probably what you're looking for.

Is it possible to change android background black colour?

Is it possible to change the colour of background while switching between activities?
I mean the black colour what apear while one of activities disappear and before second appear. Thanks.
Just you need to head over to the activity_home.xml
At the starting lines of code add this background parameter to set the screen black
android:background="#ffffff"
you may change the theme that you are using as follows:
application android:icon="#drawable/icon" android:label="#string/app_name"
android:theme="#android:style/Theme.Light" >
Edit
as mentioned in the documentation:
<application android:theme="#style/CustomTheme">
and then tweak the theme that you like:
<style>
<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>
here is the link to the topic http://developer.android.com/guide/topics/ui/themes.html#ApplyATheme

Android: Custom Title Bar

I have a custom title bar
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activities);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
Which works basically fine. The problem is that until the above code is called the default title bar is shown. I don't want a title bar there at all, in other words before mine shows up no title shall show up.
Adding this to the manifest:
<application android:theme="#android:style/Theme.NoTitleBar">
leads to a force close. My manifest looks like this
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:theme="#style/My_Theme">
Where I need my_Theme since it sets the background color, setting the background color in my customer theme leads to a gray area around my colored backround. So even without the force close I am not sure if the no title will help.
Any idea?
I had the same issue as you.
The issue is with something you have in your style.
Try this out:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="My_Theme">
<item name="android:windowTitleSize">35dp</item>
<item name="android:windowTitleBackgroundStyle">#android:color/black</item>
</style>
</resources>
This one is the only one for me, which prevents the default-title before my custom title is initiated:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomWindowTitleStyle">
<item name="android:textColor">#android:color/transparent</item>
</style>
<style name="CustomTheme" parent="#android:style/Theme.Holo">
<item name="android:windowActionBar">false</item>
<item name="android:windowTitleBackgroundStyle">#android:color/transparent</item>
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleStyle">#style/CustomWindowTitleStyle</item>
</style>
</resources>
First thing why are you using a custom title bar if your app has NoTitleBar? That is silly!
Needless to say, this is your problem and you must remove that flag.
Anyhow, the best way to add custom title bar is in xml only. This avoids your app double loading the title bar; which users will see.
../res/styles.xml
<resources>
<style name="AppTheme parent="#style/android:Theme.Light">
<item name="android:windowNoTitle">false</item>
<item name="android:windowTitleSize">30dp</item
<item name="android:windowTitleStyle">#layout/custom_title</item>
</style>
</resources>
Then you dont need that code about requestAnything.
you should also check whether customTitle is supported by it or not.
Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
}
Your App crashes because in your code you calling Title Bar from window features and in other side you disabling it through manifest. Basically You cant do this, its logically incorrect.
You need to modify your title bar not to remove it.

Categories

Resources