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.
I'm trying to change colorPrimary and colorAccent programmatically, but I can't found any methods related with them, such as setThemeColorPrimary(int color). The only way I found is change it via Java reflection. But, I can't find colorPrimary and colorAccent fields to be reflected.
So, how can I change colorPrimary and colorAccent programmatically?
Thanks in advance.
As far as i know that's not possible you can't access colorAccent and colorPrimary fields that's not how Android resources compile process works.
There is no such thing as Theme.colorPrimary , to access Theme atributes you need to use obtainStyledAtributtes() or similar technique.
The only way i know to do it programatically is by using setTheme() method or using a ContextThemeWrapper(). Both ways need you to have multiple style declaration in XML.
There's no way to override theme attributes!
1) If you don't want to update every view manually, read on.
2) If having predefined sets of primary and accent colors is OK for you, read on.
Have a couple of predefined theme overlays with primary and accent color specified:
<style "ThemeOverlay.MyApp.Red" parent="">
<item name="colorPrimary">#ff0000</item>
<item name="colorPrimaryDark">#880000</item>
<item name="colorAccent">#00ffff</item>
</style>
<style "ThemeOverlay.MyApp.Blue" parent="">
<item name="colorPrimary">#0000ff</item>
<item name="colorPrimaryDark">#000088</item>
<item name="colorAccent">#ffff00</item>
</style>
<!-- Green, orange, etc. -->
Now you can wrap any context and override just these three attributes by
Context newContext = new ContextThemeWrapper(context, R.style.ThemeOverlay_MyApp_*);
This is good enough for inflating views or creating them manually.
How to make it automatic for all your activities? Create a BaseActivity which all your activities will extend. This activity will update it's theme like so:
#Override
public void onCreate(Bundle icicle) {
final SharedPreferences prefs = ...;
final String themeColor = prefs.getString("themeColor", ""); // Non-null!
final int themeResId;
switch (themeColor) {
"BLUE":
themeResId = R.style.ThemeOverlay_MyApp_Blue;
default:
themeResId = R.style.ThemeOverlay_MyApp_Red;
}
setTheme(themeResId);
super.onCreate(icicle);
// etc.
}
Where themeResId is a resource ID of one of theme overlays defined above. I'm assuming the color theme is a user preference in your app and you store a string like "RED" or "BLUE" which you can translate to the theme resource ID at runtime. DO NOT store the resource ID to preferences, the IDs change across builds.
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>
I'm developing an android application and I cannot remove the title bar permanently. A solution posted here: How to hide the title bar for an Activity in XML with existing custom theme
(the 600 upvoted one, the others didn't work as described below) worked in general, but it maintained the title bar for a brief millisecond when initially launching the app.
I've tried various solutions throughout stackoverflow and other sites modifying the android theme in the manifest xml and style file(s). However, all of these solutions have all crashed the application before it began. The message in LogCat being that I must use an AppCompact theme. My main activity extends the ActionBarActivity class, and I have tried switching it to just Activity while also removing the :
if (savedInstanceState == null) {getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
that is generated. However, when I do so, all of the views on the main activity disappear and it becomes completely white with nothing else. Is there a way to completely remove the actionbar while extending ActionBarActivity? If not, how can I switch to extending Activity or some other class while maintaining no other errors?
on styles.xml you should make that
parent="Theme.AppCompat.NoActionBar"
Do you use custom view for the ActionBar? if yes, then maybe you'll find the approach I use acceptable. I just set text color for Title and Subtitle the same as background color in my app's theme.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="#android:style/Widget.ActionBar">
<item name="android:titleTextStyle">#style/ActionBar.Title</item>
<item name="android:subtitleTextStyle">#style/ActionBar.Subtitle</item>
<item name="android:background">#color/my_color</item>
<item name="android:backgroundStacked">#color/my_color</item>
<item name="android:backgroundSplit">#color/my_color</item>
</style>
<style name="ActionBar.Title">
<item name="android:textColor">#color/my_color</item>
</style>
<style name="ActionBar.Subtitle">
<item name="android:textColor">#color/my_color</item>
</style>
Then I inflate and apply custom ActionBar view in onCreate and set custom title
So I've noticed Android generates a slight fade between activities which really bugs me and I was wondering if there was any way to get rid of it and just have it "snap" to the next screen with no animation at all?
I've looked around but I can't find anything that answers my question really. I was under the assumption that it would be XML based but I saw this guy trying to do it programmatically here:
How do I eliminate the delay before an LayoutTransition animation but doing what he did (applying a "blank" animation) didn't seem to change anything.
Could something point me in the right direction? Ta!
The most elegant control is achieved using styles and themes. This way, you can control this on a per-Application as well as per-Activity basis.
styles.xml (WhateverTheme being the Android theme you implicitly or explicitly chose for your app):
<resources
xmlns:android="http://schemas.android.com/apk/res/android" >
<style
name="MyWhateverTheme"
parent="#android:style/WhateverTheme">
<item name="android:windowAnimationStyle">#style/MyActivityAnim</item>
</style>
<style
name="MyActivityAnim">
<item name="android:windowEnterAnimation">#null</item>
<item name="android:windowExitAnimation">#null</item>
</style>
</resources>
Of course, you can also specify custom #animations this way.
In your Manifest.xml:
<application
android:theme="#style/MyWhateverTheme" >
and to change it for a particular Activity:
<activity
android:theme="#style/SomeOtherTheme" >
Use the FLAG_ACTIVITY_NO_ANIMATION
Intent i = new Intent(this, nextActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);