styling action bar use a Theme.AppCompat - android

I am following the android training , in styling the Action bar.
I am facing error because of android:theme=#style/MyActionBar.
I want to know how to style the action bar.
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyActionBar" >
IllegalStateException: You need to use a Theme.AppCompat theme with
this activity
I thought the error from :
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
so I modify it to
<style name="CustomActionBarTheme"
parent="#android:style/Theme.AppCompat.Light ">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
I had the following error :
error: Error retrieving parent for item: No resource found that
matches the given name '#android:style/Theme.AppCompat.Light '.
I created a new file as the training explained
res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#aaaaaa</item>
</style>
</resources>
And here is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyActionBar" >
<activity
android:name="com.example.helloworld.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.helloworld.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.Helloworld.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.Helloworld.MyActivity" />
</activity>
</application>
</manifest>

edit your style as follows:
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_background</item>
</style>
for support library you have to use
name="background">#drawable/actionbar_background</item>
instead of
name="android:background">#drawable/actionbar_background</item>
and then use this theme in your manifest...
before use don't forget to add support:appcompact dependencies in build.gradle if you are using Android Studio...
thanks...

appcompat v7 libary has two kinds of files. One is jar, the other is reses like string.xml styles.xml. You question implies that you didn't import reses of appcompat library. So, import the project of .../Android-SDK/sdk/extras/android/support/v7/appcompat into your Eclipse, and then add it as a libary project of your main project.

I think you have not added a reference of appcompat lib in your project. Add the reference of it to your project and check
https://developer.android.com/tools/support-library/setup.html

Related

change theme in manifest not working

i define two theme in styles.xml
AppTheme1 and AppTheme2:
<resources>
<!-- Base application theme. -->
<style name="AppTheme1" 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>
<style name="AppTheme2" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#00ff00</item>
<item name="colorPrimaryDark">#ff00ff</item>
<item name="colorAccent">#0000ff</item>
</style>
</resources>
when i change theme in application tag inside AndroidManifest.xml
my app theme doesn't any change and the app only use colors.xml and not styles.xml.
for example my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.maneshtsoft.blackdictionary">
<application
android:allowBackup="true"
android:icon="#mipmap/favicondark"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme2">
<!-- OR AppTheme1 does not matter -->
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
just say that i use toolbar and AppCompactActivity maybe help to answer.
thanks.
The problem may be the cache of InstantRun.
Did you try "Clean & Rerun"? If you use AndroidStudio 2.1 or higher, Select Menu -> Run -> Clean & Rerun.

How to remove title bar in support library?

I want to port my app to android support library v7 revision 23.1.
For this purpose I have extended my theme from Theme.AppCompat.NoActionBar as instructed in multitude of guides.
<style name="MyTheme" parent=" Theme.AppCompat.NoActionBar ">
The problem is that the activity shows old android title bar with the name of my application and I cannot remove this title bar. Regarding this the support library is a big step backwards in comparison to holoeverywhere. Not to mention load of other bugs.
I have tried to remove title bar using following adition to my theme:
<style name="MyTheme" parent=" Theme.AppCompat.NoActionBar ">
...
<item name="windowNoTitle">true</item>
<item name="android:windowNoTitle">true</item>
...
</style>
But the title bar persists.
The device runs with android 2.3.5.
How to remove title bar?
You must set
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
also using toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/activity_main_page_toolbar"
android:layout_height="#dimen/actionbar_height"
android:layout_width="match_parent"
android:background="#drawable/actionbar_background"
android:textColorPrimary="#android:color/white"
app:theme="#style/Theme.CustomAppCompat">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_logo"
android:background="#drawable/menu_item_drawable"
android:padding="5dp"
android:clickable="true"
android:id="#+id/toolbar_icon" />
<include layout="#layout/notif_layout" />
</android.support.v7.widget.Toolbar>
Try to create such a style.xml.Its for full screen app.So just delete fullscreen for NoTitleBar.And windowFullScreen to false.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="#style/Theme.AppCompat.Light">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
</resources>
And such a Mansifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="by.test.roma.usermetrics" >
<application
android:name=".ParseApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

how to set image to background n text color of action bar?

I want so set backgroung image to actionbar of main activity and all activities too and also want to chnage text color of text of action bar
i tried this code but it giving me error
plz help me
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppThe" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="ActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#drawable/actionbar_background</item>
</style>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
</resources>`
manifest code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hp.navigationd"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="OLXA"
android:theme="#style/AppThe" >
<activity
android:name="com.example.hp.navigationd.MainActivity"
android:label="OLXA" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.hp.navigationd.post_ad"
android:label="#string/Ad_Details"
android:parentActivityName="com.example.hp.navigationd.MainActivity" />
<activity android:name="com.example.hp.navigationd.My_account" />
<activity android:name="com.example.hp.navigationd.My_fav" />
<activity android:name="com.example.hp.navigationd.My_ads" />
<activity android:name="com.example.hp.navigationd.My_chats" />
<activity android:name="com.example.hp.navigationd.Rules" />
<activity android:name="com.example.hp.navigationd.Help" />
<activity android:name="com.example.hp.navigationd.About_us" />
</application>
</manifest>
Try this,
Insted of using,
<style name="AppThe" parent="Theme.AppCompat.Light.DarkActionBar">
use this,
<style name="AppThe" parent="#android:style/Theme.Holo.Light.DarkActionBar">
Check out the below style.
<style name="AppThe" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:actionBarStyle">#style/ActionBar</item>
<item name="actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
<item name="background">#drawable/actionbar_background</item>
</style>
Here you need to use both you both android:actionBarStyle and actionBarStyle as well as android:background and background items. The former is for newer versions of android that support ActionBar natively. The latter is for older android versions.
About the error:
You can not use "#android:style/Theme.Holo.Light" with AppCompatActivity

Android: Custom Action Bar Theme Error - (No Resource Found that Matches . . .)

I am working with Android in Eclipse.
So I'm trying to implement a custom theme, and I keep getting this error from my manifest file:
error: Error: No resource found that matches the given name (at 'theme' with value
'#android:style/CustomActionBarTheme').
Ideas? Thanks!
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/CustomActionBarTheme" >
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
Themes.xml (Custom theme)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo">
<item name="android:windowActionBarOverlay">true</item>
<item name="android:actionBarTabTextStyle">#style/MyActionBarTabText</item>
<item name="android:actionMenuTextColor">#color/actionbar_text</item>
</style>
<!-- ActionBar Styles -->
<style name="MyActionBar"
parent="android:style/Widget.Holo.ActionBar">
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
</style>
<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#color/actionbar_text</item>
</style>
<!-- ActionBar tabs text styles -->
<style name="MyActionBarTabText"
parent="#android:style/Widget.Holo.ActionBar.TabText">
<item name="android:textColor">#color/actionbar_text</item>
</style>
</resources>
Styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="#android:Theme.Holo.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here.
-->
</style>
</resources>
You're referencing the system's resources when you use #android:.... Instead use #style/CustomActionBarTheme.
You need to use
android:theme="#style/CustomActionBarTheme
You are referring to the style in your themes.xml.
There is not resource with the name #android:style/CustomActionBarTheme

Why my Android app don't show icon and text in ActionBar?

I'm a beginner in Android development and I trying to build my theme but I have a problem. I tried searching answers on google, d.android but to no avail.
ActionBar is changing color (this is OK), but text and icon of app doesn't show (only if I use my theme - if I use default theme everything is OK). Why?
res/values/styles.xml:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="AppTheme">
<item name="android:background">HEX COLOR</item>
<item name="android:titleTextStyle">#style/MyTitleTextStyle</item>
</style>
<style name="MyTitleTextStyle" parent="AppTheme">
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
Here is my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appname"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.appname.Intro"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
"You should add following code in onCreate() method.
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher); "
I see the problem that you use AppTheme as parent for MyActionBar style and it causes circular dependency. MyActionBar -> AppTheme -> MyActionBar ... You should use android:Widget.ActionBar or similar as parent of your action bar theme.
Also there should be android:Theme.Holo.Light instead of android:Theme.Light as parent of AppBaseTheme.
Here is how your res/values/styles.xml file should look like:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Holo.Light"/>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="android:Widget.ActionBar">
<item name="android:background">HEX COLOR</item>
<item name="android:titleTextStyle">#style/MyTitleTextStyle</item>
</style>
<style name="MyTitleTextStyle" parent="AppTheme">
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>

Categories

Resources