Android Styles For API 10 and API 11 - java

I am trying to do something that I feel to be very simple in concept. I would like my app to be supported all the way back to API 10 (Gingerbread). To make this look good, I need to make a slight change to the color of the text on my buttons when the device is running API 10. Thus, I want to create two styles: one of which will be used when the device is using API 10 (I want the text color of the buttons to be black in this case), and another when the device is using API 11 or above (the text color will be the default ICS grayish in this case). To do this, I am using a values and a values-v11 folder. Inside the values folder is a themes.xml file with the following code:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="buttonColorStyle">
<item name="android:textAppearanceButton">#style/buttonTextColor</item>
</style>
<style name="buttonTextColor">
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
However, when I load up my app with target SDK set to 10, the text color of the buttons is unchanged from the default grayish. Also, here is the code for one of my buttons which should use this style:
<Button
style="#style/buttonColorStyle"
android:id="#+id/thirdSectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="sectionButtonClicked"
android:text="Section 3"
android:textSize="11.5sp" />
Anyone have any ideas?

You have to do something like this:
<style name="MyStlye">
<item name="android:textAppearanceButton">#style/BtnText</item>
</style>
<style name="BtnText" parent="android:TextAppearance.Small.Inverse">
<item name="android:textColor">#android:color/black</item> // or whatever color
</style>

why not setting the same nice style for all versions?
you can use this library (also suggested at the android developers blog here) for making all of the devices use the holo theme.
alternatively , just leave the current style , so that the user will feel more "at home" with his current style of the OS (since it can change between companies) .

Related

Is there a way to change the color of an icon inside preference headers?

I've added some icons (as SVGs) to my preference header file and I want to change the color of them with Java (my app is themeable and I can't find any other way to change the icon color according to the theme).
I've already tried changing the color of the icon in a similar way to buttons, etc... I can't change the color with the "app:tint" attribute either and it dosen't change with the theme no matter what I do.
Here is the preference header code. I want to change the color of "ic_round_settings".
<header
android:fragment="com.appname.settings.fragment.GeneralSettingsFragment"
android:icon="#drawable/ic_round_settings"
android:title="#string/settings_general"
android:summary="#string/settings_general_explain" />
To achieve that behaviour use the reference to the color in the XML like so
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?attr/colorAccent"
android:tint="?attr/colorAccent"
android:text="#string/chat_send_text"
android:drawableTint="?attr/colorAccent"
android:drawableRight="#drawable/ic_paper_plane"/>
</FrameLayout>
Also, when working with the activities **make sure you set the theme before using setContentView(R.layout_your_layout_file) ** or else you'll have to call recreate() when you set the theme dynamically.
Example
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(whatever_theme_you_want_to_use)
setContentView(R.layout.activity_cool)
// Further view initialization
}
The drawback is that you would have to explicitly setTheme in all of your activities since Android doesn't give developers an easier way to change the theme app-wide.
OK, I've found one way of doing it. If you add an attr attribute to the preference header like this:
<header
android:fragment="com.appname.settings.fragment.GeneralSettingsFragment"
android:icon="?attr/ic_round_settings"
android:title="#string/settings_general"
android:summary="#string/settings_general_explain" />
and add that attribute to the attr.xml file in the values folder:
<attr name="ic_round_settings" format="reference"/>
And add that into the theme classes in styles.xml with a light and dark version of the icon, the theme will change:
<style name="Theme.BaseLightTheme" parent="Theme.AppCompat">
<item name="ic_round_settings">#drawable/ic_round_settings_dark</item>
</style>
<style name="Theme.BaseDarkTheme" parent="Theme.AppCompat">
<item name="ic_round_settings">#drawable/ic_round_settings_light</item>
</style>
With this in the SVG icon file, changing the color from #000000 to #ffffff in the light SVG icon copy:
<path
android:fillColor="#000000"
android:pathData=""/>
Edit: This won't work on Android 4.4 and below - the icons won't appear at all

Ways to reference an icon in an android app

I know you can reference an icon with
<item android:id="#+id/config"
android:icon="#drawable/ic_config"
android:title="#string/config"
app:showAsAction="ifRoom"/>
So this goes in the drawable folder and looks up the ic_config image.
But I recently saw this:
android:id="#+id/config"
android:icon="?iconConfig"
android:title="#string/config"
app:showAsAction="ifRoom"/>
Now I don't understand how the mapping between my config-image and ?iconConfig is working, I can see that 'iconConfig' appears in the R.java and attr.xml files, but nowhere else.
Can anyone explain?
Please go through the following link:
http://www.linuxtopia.org/online_books/android/devguide/guide/topics/ui/themes.html
Edit
for your convenience i am posting the required part from link provided above:
Just like styles, themes are also declared in XML elements,
and are referenced in the same manner. The difference is that you add
a theme to an entire application or activity, via the
and elements in the Android Manifest — themes cannot be
applied to individual Views.
Here's an example declaration of a theme:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomTheme">
<item name="android:windowNoTitle">true</item>
<item name="windowFrame">#drawable/screen_frame</item>
<item name="windowBackground">#drawable/screen_background_white</item>
<item name="panelForegroundColor">#FF000000</item>
<item name="panelBackgroundColor">#FFFFFFFF</item>
<item name="panelTextColor">?panelForegroundColor</item>
<item name="panelTextSize">14</item>
<item name="menuItemTextColor">?panelTextColor</item>
<item name="menuItemTextSize">?panelTextSize</item> </style> </resources>
Notice the use of the at-symbol (#) and the question-mark (?) to
reference resources. The at-symbol indicates that we're referencing a
resource previously defined elsewhere (which may be from this project
or from the Android framework). The question-mark indicates that we're
referencing a resource value in the currently loaded theme. This is
done by referring to a specific by its name value. (E.g.,
panelTextColor uses the same color assigned to panelForegroundColor,
defined beforehand.) This technique can be used only in XML resources.
'?' is used when you need to refer to the drawable while having several themes. It is made to simplify the logic when switching between themes and let the Android automatically decide which resource to use.

in Android, is there any way of use classes to format a group´of views?

I'm beginning in Android development and I got very familiar with the XML notation, since it's pretty similar to HTML.
By the XML we can set ID's to the views for later managing it with Java, as like we do with HTML and JS.
My point is: Can I define classes or something like that to set similiar attributes to a group of views?
Sorry if I was unclear or anything, and thanks for you help!
You can define styles and use those across widgets.
Here are the official docs for it:
http://developer.android.com/guide/topics/ui/themes.html
You can do this using styles.
in res/values/ create a file named styles.xml [Auto generated in Android Studio]
You can define new style using the style tag :
<style name="styleName"></style>
You can also modify an already present style by using :
<style name="styleName2" parent="alreadyExistingStyle"></style>
Than you use it in xml and as well as Java.
In xml :
style="#style/styleName"
In Java :
R.style.styleName
Example :
Button Style :
<style name="AppButtonLight" parent="#android:style/Widget.Button">
<item name="android:textColor">#FF000000</item>
<item name="android:layout_margin">5dp</item>
<item name="android:textSize">16sp</item>
<item name="android:background">#drawable/hw_btn_default</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
Than use it in xml :
<Button
style="#style/AppButtonLight"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/btnDownload"
android:id="#+id/btnDownload" />
Hope I was able to help you.

I want to hide my title bar in my android application [duplicate]

I am trying to use a custom title to include an image button to the title bar.
I got a lot of help form this post: android: adding button to the title of the app?, but could not get it work for my ListActivity.
In a nutshell, following is what I have:
I hide the titlebar in the AndroidManifest.xml
The specify a relative layout for the custom title (workorder_list_titlebar.xml)
My Activity Class looks like the following:
public class WorkOrderListActivity extends ListActivity {
String[] orders={"WO-12022009", "WO-12302009","WO-02122010", "02152010"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);
setContentView(R.layout.workorder_list);
setListAdapter(new ArrayAdapter(this,R.layout.workorder_list, R.id.label,orders));
}
}
When I ran the app, I got AndroidRuntimeException: You cannot combine custom titles with other title features.
Base on the stack trace, the exception was thrown by com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:183), that was triggered by setlistAdapter call.
Does anyone have the same problem with ListActivity?
Also once I manage to get this work, how do I attach listeners to the image button for it to do something?
Thanks in advance.
I had the same issue and I fix it deleting
<item name="android:windowNoTitle">true</item>
from my theme.xml
Make you create custom style in “values” folder. Make sure you code as below.
<style name="CustomTheme" parent="android:Theme">
Don't modify parent parameter.
This did work for me.
Instead of modifying your theme.xml you may also:
create a new XML style file my_theme.xml in values folder like this:
<style name="MyWindowTitleBackground">
<item name="android:background">#444444</item>
</style>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#style/MyWindowTitleBackground</item>
</style>
You may define other settings as you like in this theme.
Then just use this theme in your manifest within the activity's attributes
android:theme="#style/MyTheme"
Finally set your custom title as always in your activity.java:
final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if (window.getContainer() == null) {
useTitleFeature = window
.requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(R.layout.screen_main);
if (useTitleFeature) {
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.custom_title);
// Set up the custom title
main_title = (TextView) findViewById(R.id.title_left_text);
main_title.setText(R.string.app_name);
main_title = (TextView) findViewById(R.id.title_right_text);
main_title.setText(R.string.Main_titleInfo);
}
Don't forget to define the custom_title.xml file in your layout folder. For example...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical" >
<TextView
android:id="#+id/title_left_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="#+id/title_right_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#fff" />
</RelativeLayout>
I think notenking is right, that this is a problem in activities within tabs. Since some of my activities can either be stand-alone or within a tab, I've found the following helps:
final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if(window.getContainer() == null) {
useTitleFeature = window.requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(layoutId);
if (useTitleFeature) {
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
}
May be you find this problem when use it in tab,for there already have a title and you can not add a custom title again.
you should add this custom title in the activity which you get the Tab
I did exactly as Sunny Dasari did but with one small change I put the # before and android in the parent attribute.
So my code looked like this.
<style name="CustomTheme" parent="#android:Theme">
To avoid crashing, you can simply add
android:theme="#style/android:Theme"
to the <Activity> tag in your AndroidManifest.xml:
<activity
android:name="test.TestActivity"
android:label="#string/app_name"
android:theme="#style/android:Theme">
This is because the styles defined in your default theme conflict with FEATURE_CUSTOM_TITLE (such as the attribute android:windowNoTitle). By using another theme, you can avoid such problems.
However, you might further need to define your own theme to change other attributes, such as android:windowTitleSize, background color, text color and font, etc. In this case, you can derive your theme from an existing theme (e.g., Theme.Light) and modify its attributes:
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#323331</item>
</style>
<style name="CustomTheme" parent="#style/android:Theme.Light">
<item name="android:windowNoTitle">false</item>
<item name="android:windowTitleSize">60dip</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
</style>
</resources>
Try swapping following lines:
setContentView(R.layout.workorder_list);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);
I have run into this issue as well and it looks like it is an issue with what theme is applied to an activity in the AndroidManifest.xml file. If I use a theme like:
android:theme="#android:style/Theme.Holo
Then it will throw the error
android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
However if I use a different theme like:
android:theme="#android:style/Theme.Black"
then it will not throw the error and subsequently will not crash. However I am trying to use a theme like Theme.Holo. I'm not sure if there is a way around this.
Since, I was trying to compile my program in android 4.0, I was facing the same problem. None of these solutions helped.So, I copied my style contents from values > styles.xml and pasted it in values-v11 styles.xml file and values-v14 styles.xml file. Bingo, the trick worked.
As a beginner most of the answers didn't help me for my case. So here is my answer.
Go to res/values folder in your android project and check for strings.xml (this file may vary in your case, something like themes.xml)
Inside the file under resource tag check whether you have style tags. If you don't find it, add the code below as mentioned below as a child to resources tag
something like below
<resources>
<style name="SomeNameHere">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
if you already have style tag, just add the code below to your style tag
<item name="android:windowNoTitle">true</item>

Android Lollipop Material Design Overflow Menu Icon color

I try the new Material Design on a Nexus 7 and have the following strange behaviour. The Overflow Menu Icon has a different color on the first app launch.
I changed the android:textColorPrimary color and read this tutorial.
First App launch:
Second App launch:
As you see the color of the primary text color is not set on the first launch. It is only set if i press the home button and relaunch the app. Here is my styles.xml file:
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
<item name="android:colorPrimary">#FF4444</item>
<item name="android:colorPrimaryDark">#CC0000</item>
<item name="android:textColorPrimary">#000000</item>
</style>
Can someone explain, why that behaviour occurs?
I set android:minSdkVersion="21" and don't want to use support libraries.
I experienced the same problem when running a PreferenceActivity which cannot use appcompat-v7 library on a LOLLIPOP device. When this activity is opened for the first time the overflow icon is always white completely ignoring android:textColorPrimary and android:colorControlNormal. Subsequent runs or orientation changes however result in proper coloring.
I created a gist which will help you mitigate this. The code binds a global layout observer to the toolbar and when it finds an overflow icon it replaces it and unbinds the observer. So don't use it in places where you don't expect an overflow icon because the observer won't get unbound in that case.
Link to gist: https://gist.github.com/consp1racy/4b640679de553fdb3046
Just add the secondary text color for the options menu, i.e.:
<item name="android:textColorSecondary">#color/text_color</item>
In some circumstances the secondary color is set to the primary color. I don't know yet why.
Add these items too:
<item name="actionMenuTextColor">#color/white</item>
<item name="android:actionMenuTextColor">#color/white</item>
If this didn't help, then try this:
<style name="AppBaseTheme" parent="android:Theme.Material.Light">
<item name="android:itemTextAppearance">#style/TextAppearance</item>
</style>
<style name="TextAppearance">
<item name="android:textColor">#android:color/white</item>
</style>
This would works for Holo.Light.DarkActionBar
If you want to use material design in pre-21 devices you need to extend Theme.AppCompat.Light.NoActionBar theme. To do that, you need to add compile com.android.support:appcompat-v7:21.0.0 as a dependency of your project, this way you will be able to use the Toolbar in your layout.
Then define your theme in values/themes.xml::
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">#color/my_awesome_color</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#color/my_awesome_darker_color</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 & colorSwitchThumbNormal. -->
</style>
and your the toolbar in your layout/my_activity.xml:
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
You have optional attrs to define the theme and popupTheme as well:
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
Chris Banes wrote a good article about this that you should read https://chris.banes.me/2014/10/17/appcompat-v21/
I read on the comments of the questions that you are using eclipse, I highly recommend to use android studio + gradle and no eclipse.
Just add android:theme="#style/ThemeOverlay.AppCompat.Dark"to the Toolbar
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/ThemeOverlay.AppCompat.Dark
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
overflow menu icon will be white now :)

Categories

Resources