cannot resolve symbol AppCompat - java

I am trying to add a Splash Screen to an existing mobile app for Android using Android Studio. From everything I have researched, I need to be using AppCompat to do this, but each time I try to add it to my styles.xml file, Android Studio tells me "cannot resolve symbol "AppCompat"". Is there somewhere else I should be adding this prior to the styles page in order to have code accept it? My code is below.
Styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppCompat">
<item name="android:colorPrimary"></item>
</style>
<style name="SplashTheme" parent="Theme.AppTheme">
<item name="android:windowBackground">#drawable/background</item>
</style>
</resources>
I tried adding it to my AndroidManifest.xml file to no effect, I receive the error "Unknown resource type" in that file.
When I attempted to change my Main file to:
"public class Snake extends AppCompatActivity"
I receive the "Cannot resolve symbol" error again.
I feel like the guides I have read are leaving out some crucial, yet shockingly obvious, step that most people familiar with Android would know quite well. Regretfully, I am still quite new to Android and trying to learn.

So change AppTheme parent to Theme.AppCompat.Light.NoActionBar so styles will be like that:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:colorPrimary"></item>
</style>
<style name="SplashTheme" parent="Theme.AppTheme">
<item name="android:windowBackground">#drawable/background</item>
</style>
You have these error cannot resolve symbol AppCompat because only type AppCompat is not an theme, AppCompat is an symbol inside Themes resouces. So every time you need to set an AppCompat theme you need to type Theme.AppCompat to get the theme AppCompat.

You should have compile "com.android.support:appcompat-v7:25.3.1" in your Gradle dependencies.
You can create a theme with ActionBar and Without ActionBar. If you use a toolbar in your activity probably you should use without action bar theme. Select your theme accordingly.
Create style file like this.
With ActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:colorPrimary"></item>
</style>
<style name="SplashTheme" parent="Theme.AppTheme">
<item name="android:windowBackground">#drawable/background</item>
</style>
Without ActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:colorPrimary"></item>
</style>
<style name="SplashTheme" parent="Theme.AppTheme">
<item name="android:windowBackground">#drawable/background</item>
</style>
Now refer theme in the manifest. Hope it will work:)

Related

Why does appcompat-v7:22.2.0 break ActionMode overlaying ActionBar?

In my project I have a screen that uses action mode via:
mActionMode = getActivity().startActionMode(this);
I need this to "overlay" my action bar rather than push everything down. I achieve this using the following theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowActionModeOverlay">true</item>
</style>
So far this has worked fine when using version 22.1.0 of the appcompat library:
compile 'com.android.support:appcompat-v7:22.1.0'
This is the result - perfect -
Recently I upgraded to version 22.2.0 of the appcompat library:
compile 'com.android.support:appcompat-v7:22.2.0'
Now I have the following:
As you can see, the "overlay" behaviour is completely broken. Am I missing something here?
FYI - I am using a ToolBar in my XML layout and then using:
activity.setSupportActionBar(mToolbar);
You should use the appcompat style attribute, without the framework prefix:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionModeOverlay">true</item>
</style>
I'm not 100% sure, but I believe that for newer API versions it would be good to also keep the framework version, thus:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionModeOverlay">true</item>
<item name="android:windowActionModeOverlay">true</item>
</style>

Trying to create toolbar and getting rendering error

Im trying to create a toolbar but im getting error. I searched and didnt found solution. This is the error:
The following classes could not be found: android.support.v7.widget
my api version is 16.
This is my styles.xml file:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/ColorPrimary</item>
<item name="colorPrimaryDark">#color/ColorPrimaryDark</item>
<!-- Customize your theme here. -->
</style>

Applying AppCompat theme to individual preferences in a PreferenceFragment

I have been wrestling with trying to get my PreferenceFragment to have the same Material-based theme and styling (via AppCompat) as the rest of my application. The PreferenceFragment that I am using to manage all of my application settings is shown below:
As you can see from the screenshot above, I was able to customize the PreferenceFragment by using colorAccent, colorPrimary, and a few other attributes. My theme for the PreferenceFragment is as follows:
<style
name="settingsTheme"
parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/blue_grey_500</item>
<item name="colorAccent">#color/blue_grey_500</item>
<item name="android:textColor">#color/black_text_alt</item>
<item name="android:textColorSecondary">#color/black_secondary_text</item>
</style>
However, despite my best efforts, I am unable to apply themes to the individual elements within my PreferenceFragment, such as ListPreference and EditTextPreference; all of the preferences still retain the standard AppCompat theme:
I found an 2 year old post that discusses this issue, albeit with no real solution: How to apply theme to <PreferenceScreen> elements of a <PreferenceCategory>
I am wondering if anyone has been able to successfully apply themes to the preferences since AppComat V21 has been released. If not, are there any viable workarounds that can be used to apply custom themes to individual preferences?
UPDATE: I made a library to battle the issue (uses AppCompat r22): https://github.com/consp1racy/android-support-preference
The ListPreference extends DialogPreference which uses this piece of code to create the dialog:
mBuilder = new AlertDialog.Builder(context)
.setTitle(mDialogTitle)
.setIcon(mDialogIcon)
.setPositiveButton(mPositiveButtonText, this)
.setNegativeButton(mNegativeButtonText, this);
As you can see the AlertDialog.Builder constructor is not supplied with the second optional int theme parameter. That means the dialog will be themed by whatever your activity's theme has in its android:alertDialogTheme attribute.
Now you have to create a custom theme for your dialog which derives from Theme.AppCompat.Dialog like so:
<style name="Theme.YourApp.Dialog.Alert" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
<item name="colorAccent">#color/accent_yourapp</item>
<item name="colorPrimary">#color/primary_yourapp</item>
<item name="colorPrimaryDark">#color/primary_dark_yourapp</item>
</style>
Problem 1: The above solution will not work for RingtonePreference because it does not extend ListPreference but calls a ringtone chooser intent, so it's always themed according to system. Check out this answer:
RingtonePreference Theme So we can mark this as solved.
Problem 2: The AppCompat dialogs lack title. And so far I haven't found a way to fix this. True that I am not looking hard enough. Let's ignore the title absence as a minor issue.
Problem 3: The radio button drawables are not mutated so the graphics are inconsistent between passive and active (colored) state - all are colored (not just the one you!re pressing) or all are grey. Now this is really annoying
Problems 2 & 3 forced me to take another route - my dialog theme looks like this on API 14+
<style name="Theme.MyApp.Dialog.Alert" parent="android:Theme.DeviceDefault.Light.Dialog.MinWidth">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
</style>
and like this on API 21+
<style name="Theme.YourApp.Dialog.Alert" parent="android:Theme.DeviceDefault.Light.Dialog.MinWidth">
<item name="android:colorPrimary">#color/primary_yourapp</item>
<item name="android:colorPrimaryDark">#color/primary_dark_yourapp</item>
<item name="android:colorAccent">#color/accent_yourapp</item>
</style>
These values have been experimentally gained by crawling through the platform's source files and well tested.
The point is that the only reliable solution seems to be using the device default dialog theme. The only choice before Lollipop is a light or dark variant. On Lollipop this will work as intended and requested.
EDIT: Since appcompat-v7-r21.1.0 you can use AppCompatDialog which is material themed variant of native AlertDialog.
You can use provided AlertDialog.Builder (not to be confused with its native counterpart) for creating its instances.
DialogPreference uses android.app.AlertDialog.Builder directly so it would be impossible to switch the displayed dialog to the material design one for all API levels (which requires using android.support.v7.widget.AlertDialog.Builder).
I believe the only option for now is to extend and override dialog creation logic in DialogPreference or its subclasses (and use reflection to work around private accessors if required). Check out this reddit thread and an example AppCompatListPreference made by the author of that thread.
Taking cues from the answers already given I set in the manifest a custom theme for Settings activity because it is necessary to remove the border background around the Dialog that appear, using a transparent background this only for API <21:
AndroidManifest.xml
<application
android:name=".MyApplication"
...
android:theme="#style/AppTheme">
...
<activity
android:name=".ui.SettingsActivity"
...
android:theme="#style/AppTheme.Settings">
</activity>
</application>
values/style.xml
<!-- Application theme -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:alertDialogTheme">#style/AppTheme.AlertDialog</item>
</style>
<!-- Must be different because of transparent background -->
<style name="AppTheme.Settings" parent="AppTheme.Base">
<item name="android:alertDialogTheme">#style/AppTheme.SettingsAlertDialog</item>
</style>
<style name="AppTheme.AlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
<item name="android:windowContentOverlay">#null</item>
<item name="colorAccent">#color/accent_light</item>
<item name="colorControlActivated">#color/accent_light</item>
<item name="colorControlHighlight">#color/primary_highlight</item>
</style>
<style name="AppTheme.SettingsAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowMinWidthMajor">#android:dimen/dialog_min_width_major</item>
<item name="android:windowMinWidthMinor">#android:dimen/dialog_min_width_minor</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="colorAccent">#color/accent_light</item>
<item name="colorControlActivated">#color/accent_light</item>
<item name="colorControlHighlight">#color/primary_highlight</item>
</style>
finally for values-v21/style.xml the normal dialog, if you don't put this in v21 dialogs will be transparent.
<style name="AppTheme.Settings" parent="AppTheme.Base">
<item name="android:alertDialogTheme">#style/AppTheme.AlertDialog</item>
</style>
Actually I've the problem to set the colors directly from the Theme attributes: ?android:colorAccent instead of my fixed color #color/accent_light.
You should check out this Gist about how different components use the theme color attributes. You might need to add the following attributes to your theme to get the effect you want:
<item name="colorControlNormal">#color/x_color</item>
<item name="colorControlActivated">#color/y_color</item>
<item name="colorControlHighlight">#color/z_color</item>

Android studio cannot resolved symbol

I'm using Android studio and actually I'm styling Action bar.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar"
parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:background">#color/oranzova</item>
<item name="android:icon">#drawable/ic_launcher.png</item>
</style>
</resources>
I'm still getting error :
Error:(12, 35) No resource found that matches the given name (at 'android:icon' with value '#drawable/ic_launcher.png').
but no matters which resource I'm using. If i use #raw/icon.png i get still same error. I tried to Invalidate Caches/restart but no help.
You don't need a file-extension like *.png on resource-files. So just change your line:
<item name="android:icon">#drawable/ic_launcher.png</item>
to
<item name="android:icon">#drawable/ic_launcher</item>

Customizing styles

To customize a standard style, I use the following XML file :
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="windowNoTitle">true</item>
</style>
</resources>
The problem is that Eclipse tells me that the "windowNoTitle" attribute doesnot exist, but I have cut & pasted it from the example shown in the official web site for styles and themes indicated in the Android dev doc.
How can I modify this standard code to have no title bar?
May be you need to add android: prefix before attriibute name, also you need use valid syntax for the link to parent theme ike this:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
You could just add .NoTitleBar at the end so you don't need extra style item.
<style name="AppTheme" parent="android:Theme.Holo.Light.NoTitleBar" />
I used this for fullscreen: <style name="FullscreenTheme" parent="android:Theme.NoTitleBar">
<item name="android:windowContentOverlay">#null</item>
</style>
And it doesn't have Title Bar. Hopefully this helps.

Categories