Style theme giving an error - android

I have a style that is supposed to apply to all CardViews known as CardStyle. When I apply the style to one of the cardView's theme it gives an error:
Error:(84, 28) No resource found that matches the given name (at 'theme' with value '#styles/CardStyle').
I haven't had much experience with styles before so it may be a simple fix.
CardView in main.xml:
<android.support.v7.widget.CardView
android:theme="#styles/CardStyle"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<!-- content -->
</android.support.v7.widget.CardView>
costum style in styles.xml
<style name="CardStyle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:padding">20dp</item>
<item name="android:layout_marginTop">16dp</item>
<item name="android:layout_marginRight">5dp</item>
<item name="android:layout_marginLeft">5dp</item>
</style>
What am I doing wrong or is there a different better way to do this?
EDIT:
I changed the name to #style/CardStyle. Right when I think I solved it, I get slammed with another error.
Render problem: Couldn't resolve resource #style/CardStyle
I don't know why this is...

You may correct the typo error, replace android:theme="#styles/CardStyle" with android:theme="#style/CardStyle"
<android.support.v7.widget.CardView
android:theme="#style/CardStyle"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</android.support.v7.widget.CardView>

Related

How to use support layout_columnWeight in a style?

I think this snippet from values/styles.xml says it all:
<!-- Style for the label above a button -->
<style name="baseLabel">
<item name="android:gravity">center</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_columnWeight">1</item>
<item name="android:layout_gravity">center_horizontal|fill_horizontal</item>
</style>
And a corresponding entry from layout/main.xml might look like:
<TextView
style="#style/baseLabel" android:text="#string/lr12"
app:layout_row="0"
app:layout_column="1" />
There are no build errors, but the layout_gravity attribute has had no effect. (It works fine if I set it in the layout file: app:layout_gravity="center_horizontal|fill_horizontal)
You're setting android:layout_gravity in your style, but app:layout_gravity in what you say you're setting in the layout file. Those attributes need to match if you want them to do the same thing, so you should be using layout_gravity and layout_columnWeight in your style.

One or more layout are missing the layout height and layout width error when Loading these property from style

I am writing an app in android studio that all property of TextViews are same except id and text. For this reason I declare a style in style.xml as below:
<style name="thirdTextView" parent="TextAppearance.AppCompat">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#color/black</item>
<item name="android:layout_gravity">center_vertical</item>
</style>
In my activity_main.xml I used this style:
<TextView
style="#style/thirdTextView"
android:text="#string/questionWeight"/>
But I can not see the preview of layout and I got this error:
One or more layouts are missing the layout_width and layout_height attributes
How to solve this?
Change your TextViews to this;
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/thirdTextView"
android:text="#string/questionWeight"/>
Every widget in a layout file needs height and width attributes. Hope, this helps.

How to use app namespace in Style

I was doing something like this in layout file
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:textColorHint="#color/colorPrimary"
app:hintTextAppearance="#color/colorPrimary">
But i want to use this style at multiple place so i tried to move it in style.xml file
<style name="FindInstituteInputTextViewStyle" >
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginTop">5dp</item>
<item name="android:layout_marginBottom">5dp</item>
<item name="android:textColorHint">#color/colorPrimary</item>
<item name="app:hintTextAppearence">colorPrimary</item>
</style>
But now this give me following exceptions.
Error:(2376, 21) No resource found that matches the given name: attr 'app:hintTextAppearence'.
I also tried placing app namespace attr in < resource >
tag inside style.xml
<resources 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"
>
But again it didn't work for me. Please help me and sorry if i am doing some silly mistake
<style name="FindInstituteInputTextViewStyle" >
... ...
<item name="hintTextAppearence">#color/colorPrimary</item>
</style>
You did not create own tag, so trying to use such will cause error as you see it. Own styles you created are used by reference with existing tags, no namespace needed. i.e.:
<TextView
style="#style/FindInstituteInputTextViewStyle" /
See docs: https://developer.android.com/guide/topics/ui/themes.html
Also this line:
<item name="app:hintTextAppearence">colorPrimary</item>
does not seem to refer to color properly, so it should not even compile.

Color of the text is not changing when style including textColor is applied to textAppearance of textView

I want to reduce my xml code repetition. So I made some standard styles for text in textView. We can apply styles under 'style' attribute as well as 'android:textAppearance' attribute in textView.
Below are some styles I made for text appearance-
<style name="Grey">
<item name="android:textColor"> #333333 </item>
</style>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor"> #00FF00 </item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">20sp</item>
</style>
When I apply these styles under 'textAppearance' attribute the color of the text is not changing in none of the above styles. It's working under 'style' attribute of textView.
//textColor not working
<TextView
android:id="#+id/profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textAppearance="#style/CodeFont"/>
//textColor working
<TextView
android:id="#+id/profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Name"
style="#style/CodeFont"/>
I want them to work under 'textAppearance' attribute so that I can apply some other style under 'style' attribute. And according to android documentation we can apply textColor styles under 'textAppearance' attribute.
Please suggest some solution to this.
Thanks
Try setting the text color in your widget as null like this:
<TextView
android:id="#+id/profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textColor="#null" //add this line
android:textAppearance="#style/CodeFont"/>
Also, I think you should try to Invalidate cache and Restart Android Studio. Import and linking issues can be solved like this sometimes.
This snippet works for me
<style name="fonts" parent="TextAppearance.AppCompat">
<item name="android:textColor">#245546</item>
<item name="android:textSize">30dp</item>
</style>
and textview is
<TextView
android:id="#+id/sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to stackoverflow"
android:textAppearance="#style/fonts"/>
Sometimes if you don't give android:textColor in styles you can have this issue that text color won't appear in your TextView, So the solution is to just give completely <item name="android:textColor">#color/yourColor</item> in styles rather than this <item name="textColor">#color/yourColor</item>.. your problem would be resolved :)

change in activity style deforms button

I want to change the style of my action bar (change the color and text font) and so set up my manifest and style XML as follows:
Manifest:(application)
android:theme="#style/ArbuckleAppTheme"
Style sheet:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="ArbuckleAppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#color/handler_noorder_startcolor</item>
<item name="android:textColor">#color/handle_noorder_textcolor</item>
</style>
</resources>
The thing is one of my buttons (inside a sliding drawer) becomes much bigger. I tried changing layout values, etc. but to now avail.
Here is the xml surrounding the button:
<Button
android:id="#+id/bCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:text="Cancel"
android:background="#drawable/cancel_singleitem_background"
android:textSize="12dp"
android:padding="5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"/>
There is an identical button in the main part of my app - which doesn't change shape.
I want to figure out why this one button's shape is getting completely screwed up. Happy to add more code if needed - thought I would start with immediate code first.
I figured it out!
You can remove the parent attribute from the application theme, and this removes the imposition of holo.light formatting across the entire app.
So the code is now:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="ArbuckleAppTheme" >
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#color/handler_noorder_startcolor</item>
<item name="android:textColor">#color/handle_noorder_textcolor</item>
</style>
</resources>
By the way, the imposition I'm talking about (button becoming bigger) is because Google is trying to impose minimum standards on app development - and that includes buttons that are min. 48px.
I learned a lot from this - hope this is news to some of you so I can give back.

Categories

Resources