I have an app that was originally styled for Android 2.3. Now I'm adding an ActionBar with ActionBarSherlock, and everything is fine, except on my Nexus 7 running 4.2, it's now using the Holo theme for buttons and for EditTexts. Before my theme was inheriting from Theme.Light and now it's inheriting from Theme.Sherlock. I want it to use the old theme even on newer devices. I suspected that this was caused by the following line in the values-v14 folder:
<style name="Sherlock.__Theme" parent="android:Theme.Holo">
So I tried removing that folder and the v11 folder just to see if it would fix the problem, but then the ActionBar went away completely.
Then I tried overriding the style for the EditText like this:
<style name="AppTheme" parent="Theme.Sherlock">
<item name="#android:attr/editTextStyle">#android:style/Widget.EditText</item>
</style>
That should have made the widget not use the Widget.Holo theme and use the regular theme, but it didn't work.
How do I make Android 4.0+ devices use the original theme instead of the Holo theme while using ABS?
Below I've posted some code for a sample application using ABS that is currently showing a Holo themed EditText but should be showing a regular EditText:
MainActivity.java:
import android.os.Bundle;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
public class MainActivity extends SherlockActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<EditText
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</RelativeLayout>
styles.xml:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="Theme.Sherlock">
<item name="#android:attr/editTextStyle">#android:style/Widget.EditText</item>
</style>
</resources>
Also, the theme in the manifest is AppTheme and ABS is added as a library project.
This wouldn't be caused by ActionBarSherlock, the EditText and Button themes are defined by the platform you are running, in this case 4.2. EditTexts and Buttons may also look different based on the UI skin, (TouchWiz, Sense, MotoBlur, etc).
I would recommend keeping the Holo theme on devices that support it because users will be familiar with it and having the 2.x theme on 3.x+ will look out of place.
Also, one of the Sherlock themes is required in order to use ActionBarSherlock according to the docs:
In order for the custom action bar implementation to function your application must use Theme.Sherlock, Theme.Sherlock.Light, or Theme.Sherlock.Light.DarkActionBar, or your custom theme must use one of the aforementioned as its parent.
If you really want the 2.x look for Buttons and other Widgets I believe you would need to copy all of the styles/drawables from 2.x and then override them in one of the Sherlock derived themes. You could take a look at what Chris Versieux has done with HoloEverywhere and try to do the reverse making everything look like 2.x, I would strongly recommend against it though.
The reason my fix wasn't working was because the background attribute of Widget.EditText was using the ?attr syntax, which means it was using the background of the current theme, which was Holo. So I essentially looked at the Android source and changed the two attributes that used this syntax back to what I believed should be pre-Holo theme styles (i.e. setting the background to white).
Related
I recently started Android programming, all is good and dandy, but I've came across a problem that I couldn't find an answer to, and I really diged hard for 4 days so far.
My app uses support action bar, and to be specific "android.support.v7.app.ActionBarActivity". Long story short, I couldn't handle most of the stuff applicable to an Action Bar to this support one.
My app uses this support action bar by default due to setting up my mini sdk version to 14.
I want to be able to build an action bar from scratch, and customize it, since the default action bar is not responsive to my customization in styles.xml, etc.
I don't mind using Holo theme library instead of AppCompat.
So the question here, how can use Action Bar instead of Support Action Bar?
How can I extend my java class to use that instead of the support one?
Because none of the online customizing solutions are applicable to the support action bar.
A bit foggy description so I apologize for that.
Create a new project, and select the minimum API level as 15. When you do this, the appcompat-v7 library will not be required for this project as it is for projects with minSdkversion < 15. In this project, the classes android.app.ActionBarActivity and android.app.ActionBar will be used by default, i.e. the native AOSP classes and not the ones from the support library.
The following will let you have an ActionBar with custom background color as you want it, on API level 8 and above.
STEP 1. In your res/values folder, define an XML file theme.xml and add the following to it:
<resources
xmlns:tools="http://schemas.android.com/tools">
<style name="DefaultActionBarTheme" parent="#style/Theme.AppCompat.Light">
<item name="android:actionBarStyle" tools:targetApi="11">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarSize" tools:targetApi="11">#dimen/action_bar_wrap_content</item>
<item name="actionBarSize">#dimen/action_bar_wrap_content</item>
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="android:background" tools:targetApi="11">#color/actionbarbgcolor</item>
<item name="background">#color/actionbarbgcolor</item>
<item name="android:height" tools:targetApi="11">#dimen/action_bar_wrap_content</item>
<item name="height">#dimen/action_bar_wrap_content</item>
</style>
</resources>
In the same folder make another XML file colors.xml and add the following to it:
<resources>
<color
name="actionbarbgcolor">#00FF00
</color>
</resources>
and to the existing file dimens.xml, add the last line:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<!-- Optional, in case you wish to increase the default width of the Action Bar. -->
<dimen name="action_bar_wrap_content">55dp</dimen>
</resources>
In place of #00FF00 above, use the hex color code for the background color you wish to use in your ActionBar.
NOTE: The above will work assuming you are using the appcompat-v7 library. If not, then you'll have to use one of the Holo.Light themes instead of AppCompat.Light, and there will be other changes as well.
STEP 2. In your manifest file, you must add:
android:theme="#style/DefaultActionBarTheme"
to every <activity declaration if that Activity has the ActionBar.
Try this. It will work.
Zygotelnit answer works but you have to omit the ["tools:targetApi="11"] from item declaration otherwise it will give you an error for some reason.
On the other hand, I've found a much shorter and easier but not so optimized solution.While searching through the actionBar class and playing around here is the answer:
In your activity.java, go down to
protected void onCreate(Bundle savedInstanceState)
Anywhere appropriate in that method, write the following code:
ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new
ColorDrawable(Color.parseColor("#D62D20")));
You replace the color code by any color code of your choosing. It's obviously a hex color code.
I had my app looking pretty nice using the new Lollipop tools. I decided backwards compatibility is important, so I switched all my Fragments,actionBar imports to the support library. Now (understandably) I can't use my lollipop theme.
Is there a way to use different action bars for different themes? I tried to cast the support ActionBar to a new one but it doesn't seem this is allowed.
My problem lies with the following (from v21 docs)
All of your Activities must extend from ActionBarActivity, which
extends from FragmentActivity from the v4 support library, so you can
continue to use fragments. All of your themes (that want an Action
Bar/Toolbar) must inherit from Theme.AppCompat. There are variants
available, including Light and NoActionBar. When inflating anything to
be displayed on the action bar (such as a SpinnerAdapter for list
navigation in the toolbar), make sure you use the action bar’s themed
context, retrieved via getSupportActionBar().getThemedContext(). You
must use the static methods in MenuItemCompat for any action-related
calls on a MenuItem.
so by calling getsupportActionBar I can't use my Holo theme:
<resources>
<!-- Base application theme. -->
<style name="appTheme" parent="android:Theme.Holo.Light.DarkActionBar">
</style>
<style name="MyActionBar"
parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:background">#color/blue_semi_transparent</item>
</style>
</resources>
Also for some reason the action bar loses the button that was on it and it goes into the dropdown menu and the app icon no longer appears in the action bar.
I really am no expert on this stuff having only started developing on lollipop so would really appreciate advice.
AppCompat (i.e., ActionBarActivity) uses the Material color palette which defines default coloring throughout your app. In your case, you need to use colorPrimary for your action bar color:
<style name="appTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/blue_semi_transparent</item>
</style>
Note that you should also provide a colorPrimaryDark (a darker version of the same color) for coloring the status bar.
Per the partially outdated Action Bar training, AppCompat also uses app namespaced attributes (as things like showAsAction didn't exist before API 11) for your menu items:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom" />
...
</menu>
Per the Toolbar documentation (which is default behavior on Material themes and in AppCompat):
In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.
Therefore as you noted the app icon not appearing on the Action Bar is expected behavior.
I switched from ABS to AppCompat and Material theme(for api 21 only)
<!--manifest: -->
<application
android:theme="#style/AppStyle"
<-- values folder -->
<style name="AppStyle" parent="#style/AudioRecTheme">
<style name="AudioRecTheme" parent="#style/Theme.AppCompat.Light">
<!-- values-v21 folder-->
<style name="AudioRecTheme" parent="#android:style/Theme.Material.Light">
My activity:
public class AudioRecActivity extends FragmentActivity
The action bar is showing only in Android 5.0, but missing otherwise.
First, either use appcompat-v7 or use built-in themes, not both for the same activity. Here, you are trying to use Theme.AppCompat.Light in some cases and Theme.Material.Light in others, which is not only unnecessary but AFAIK will not work. If you are going to use Theme.AppCompat.Light, do so for all API levels.
Second, if you are going to use appcompat-v7 and Theme.AppCompat.Light, you need to inherit from ActionBarActivity.
All I want to do is change the accent color of my android app, but I'm having a tough time figuring out how to do that. The default for android is blue now, but I want to make it orange.
By accent color, I mean the accent of navigation tabs, the color that highlights when you hit lists, the accent color in pop up dialogs, etc.
I'm using actionbarsherlock if that matters.
Here is an image. I'd like to change the color of that blue accent throughout the app:
It's been some time since you asked this question but now that google has released a new AppCompat version you can do what you want to achieve quite simply. The answer I'm giving you is inspired from android developer blog support library 2.2.1.
Add the support library to your project (I am assuming you are using Android Studio).
For that add these lines to the app.graddle file (assuming your module is named app).
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
}
Set the theme of your application
These lines are to be added to your styles.xml file. As you can see there are a few items in this style. If you want to know what element they correspond to go check customize android status bar with material.
colorAccent is the color you want to change in the first place.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primaryDark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/textColorPrimary</item>
<item name="android:windowBackground">#color/windowBackground</item>
<item name="android:navigationBarColor">#color/navigationBarColor</item>
</style>
You will also need to set your application theme in the Android manifest
<application
android:theme="#style/AppTheme" >
...
</application>
Change From Activity / ActionBarActivity to AppCompatActivity in your classes.
public class MainActivity extends AppCompatActivity
{
....
}
You will probably need to change some methods due to the AppCompatActivity. Look at the video in the first link to better understand that :)
Change your widgets to the AppCompat ones
<LineareLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatTextView
android:id="#+id/text"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_start" />
</RelativeLayout>
Et voilà ! That's it you're all set :) You can now change the Accent color easily.
You're going to want to use state layout lists.
http://developer.android.com/reference/android/content/res/ColorStateList.html
You might need to make one of these for each of the widgets that is going to have a new default selected color.
I am building a sample application based on latest version of Android OS..As per instructions given here I am trying to give holo:light theme to only one button in my application. How do i do it ? I have created a myStyles.xml in res/values folder but I am not able to find property to apply holo-light theme.
This is what i have in myStyles.xml file
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="mybutton" parent="android:Theme.Holo.Light">
<item name="android:background">#android:color/holo_orange_light</item>
<item name="android:textColor">#android:color/holo_blue_dark</item>
</style>
</resources>
this is i have in my activity.
<Button
android:id="#+id/button1"
style="#style/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lol" />
I can confirm that backgroundcolor and textcolor are getting applied...(done this for testing purpose).
Currently, If i add android:theme="#android:style/Theme.Holo.Light" to AndroidManifest.xml, holo light theme is applied to entire application. However, I want this theme to be applied for only few controls ( in this case a single button control).
A theme is a style applied to an entire Activity or application, rather than an individual View.
Thus, what you are asking for is not possible. That being said, it seems odd that you would want to do this anyway. I suggest applying the holo theme to your entire app, and then customizing your Buttons only if you really need to. This is probably what the user will expect from your application.