How to use app namespace in Style - android

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.

Related

Overriding an Android View E.g. MyHeading : TextView

Help! Using Xamarin on VS2017, I'm trying to achieve this:
A MyHeading class that I can use anywhere just like a normal TextView class, except that any of the MyHeading instances will inherit the padding/margin/styles/font etc that I only define for MyHeading.
I'm lost and having no luck and can't find anything useful on Google.
I've created MyHeading.cs which inherits from TextView:
public class MyHeading : TextView
{
public MyHeading(Context context) : base(context)
{
}
}
Then I've also created myheading.xml in the Resources/layout folder that contains this XML:
<?xml version="1.0" encoding="utf-8"?>
<com.my.app.MyHeading
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/mygrey"
android:textSize="18dp"
android:textStyle="bold"
android:typeface="sans"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp" />
Please help! How can I achieve such a simple requirement i.e. to have a re-usable TextView with the same style whenever I use it.
With my code above, the project builds successfully, but when I run it, it bombs out with this:
Java.Lang.NoClassDefFoundError: android.support.v7.appcompat.R$drawable
As the comments suggest (and I as well), creating a style is more appropriate and a much more common pattern on Android than creating View subclasses.
Example
Resources/values/styles.xml
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<style name="MyHeadingStyle" parent="android:Widget.TextView">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#color/red</item>
<item name="android:textSize">18dp</item>
<item name="android:textStyle">bold</item>
<item name="android:typeface">sans</item>
<item name="android:gravity">center</item>
<item name="android:layout_marginTop">20dp</item>
<item name="android:layout_marginBottom">10dp</item>
</style>
~~~ other styles ~~~
</resources>
AXML Example:
<TextView
android:text="StackOverFlow"
style="#style/MyHeadingStyle" />
Output:
Re: Android Styles and Themes

Style theme giving an error

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>

How do I find all possibillities for Androids "Theme XML Style"?

The following file defines the base style used by my application:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Snapchat" parent="????" >
<item name="android:colorPrimaryDark">#color/Snapchat_violet</item>
<item name="android:colorPrimary">#color/Snapchat_blue</item>
<item name="android:colorAccent">#color/Snapchat_blue</item>
<item name="android:colorBackground">#color/Snapchat_white</item>
<item name="android:colorForeground">#color/Snapchat_yellow</item>
<item name="android:navigationBarColor">#color/Snapchat_grey</item>
</style>
</resources>
I want to use a standard theme as the parent to inherit from, but I can't find a listing of all standard themes and their corresponding xml names/paths.
I'm sorry if this is a newbish question, but I just can't find it in the android documentation.
check out following links it will help you
Theme.xml
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
Style.xml
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml
You can define multiple styles per file using tag but each style will have its name that uniquely identifies the style. Android style attributes are set using tag as shown below −
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomFontStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:capitalize">characters</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">12pt</item>
<item name="android:textColor">#00FF00</item>/>
The value for the can be a keyword string, a hex color, a reference to another resource type, or other value depending on the style property.
Using Styles
Once your style is defined, you can use it in your XML Layout file using style attribute as follows −
<TextView
android:id="#+id/text_id"
style="#style/CustomFontStyle"
android:text="#string/hello_world" />
</LinearLayout>
To implement a custom theme create or edit MyAndroidApp/res/values/themes.xml and add the following −
<resources>
...
<style name="MyCustomTheme" parent="android:style/Theme">
<item name="android:textColorPrimary">#ffff0000. </item>
</style>
...
</resources>
In your AndroidManifest.xml apply the theme to the activities you want to style −
<activity
android:name="com.myapp.MyActivity"
....
android:theme="#style/MyCustomTheme"
/>
Your new theme will be applied to your activity.
You can also have a look at http://www.tutorialspoint.com/android/android_styles_and_themes.htm
See style, stylable and attr.

Use custom button as parent in custom style

In my project I use special fonts for my buttons. So I've added the PixlUI library so I can set the font in xml.
<com.neopixl.pixlui.components.button.Button
android:id="#+id/btn_login"
style="#style/custom_button_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/login"
pixlui:typeface="AvenirNextCondensed-Regular.ttf" />
<com.neopixl.pixlui.components.button.Button
android:id="#+id/btn_register"
style="#style/custom_button_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/make_new_profile"
pixlui:typeface="AvenirNextCondensed-Regular.ttf" />
<com.neopixl.pixlui.components.button.Button
android:id="#+id/btn_broker_register"
style="#style/custom_button_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/make_new_profile_broker"
pixlui:typeface="AvenirNextCondensed-Regular.ttf" />
These are my buttons, since they all have the same typeface I want to include the typeface in the 'custom_button_style'
This my custom style:
<style name="custom_button_style" parent="#android:style/Widget.Button">
<item name="android:textSize">#dimen/hello_medium_fontsize</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:background">#drawable/custom_btn_background</item>
<item name="android:layout_marginBottom">#dimen/login_button_margin</item>
</style>
How do I include
pixlui:typeface
in this style?
It´s not clear if we can´t see the complete xml layout file, but things that often happens is that the poeple forget to add the xmlns attribute to the parent. You parent layout should look something like this. It´s part of this library and the important thing is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui" <----thats the important attribute
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
You don´t need to add something in Your style.xml. So if this does not fix Your problem, come back.
EDIT
It is not tested yet, but for the style it could be:
<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui">
<style "custom_button_style" parent="#android:style/Widget.Button">
<item name="android:textSize">#dimen/hello_medium_fontsize</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:background">#drawable/custom_btn_background</item>
<item name="android:layout_marginBottom">#dimen/login_button_margin</item>
<item name="pixlui:typeface">AvenirNextCondensed-Regular.ttf</item>
</style>

No style ViewPagerIndicator in combination with SherlockActionBar

I'm trying to implement the ViewPagerIndicator with SherlockActionBar. It works: I can slide the fragments, but the style doesn't work!
It looks like this:
related topic: https://github.com/JakeWharton/Android-ViewPagerIndicator/issues/66
I know what's going wrong:
In the sample of VPI, the style of a page is set in AndroidManifest.xml by (for example)
<activity
android:name=".SampleTabsDefault"
android:theme="#style/Theme.PageIndicatorDefaults">
</activity>
But when I add that android:theme element to my own Manifest.xml I get the following exception:
03-16 11:06:24.400: E/AndroidRuntime(483): Caused by: java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative.
I've also tried to copy all the style-information to styles.xml, but that also doesn't work.
Can you tell me how to set a style to the VPI?
Thank you!
EDIT:
Now I've added
<activity
android:name=".SampleTabsDefault"
android:theme="#style/StyledIndicators">
</activity>
to my Manifest.xml
and the following style in styles.xml
<style name="Theme.BataApp" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.Styled.ActionBar</item>
</style>
<style name="StyledIndicators" parent="Theme.BataApp">
<item name="vpiTitlePageIndicatorStyle">#style/Widget.TitlePageIndicator</item>
<item name="vpiTabPageIndicatorStyle">#style/Widget.TabPageIndicator</item>
<item name="vpiTabTextStyle">#style/Widget.TabPageIndicator.Text</item>
</style>
Result: I don't get any Exception, see the style, but I now I can't see any fragments :(
The code of the activity can be found here: http://pastebin.com/hsNgxFDZ
Screenshot of current page with style:
In order to see the Fragments, you'll have to somehow extend an Android base-theme. In order to see the ViewPagerIndicator, you'll have to somehow include those style-definitions. The answer is to create your own theme that extends the ActionBarSherlock-theme (which extends the Android base-themes), and implements the ViewPagerIndicator-styles.
Example:
<style name="myTheme" parent="#style/Theme.Sherlock">
<!-- If you want to use the default ViewPagerIndicator-styles, use the following: -->
<item name="vpiTitlePageIndicatorStyle">#style/Widget.TitlePageIndicator</item>
<item name="vpiTabPageIndicatorStyle">#style/Widget.TabPageIndicator</item>
<item name="vpiTabTextStyle">#style/Widget.TabPageIndicator.Text</item>
<!-- If you want to implement your custom style of CirclePageIndicator, do it like so: -->
<item name="vpiCirclePageIndicatorStyle">#style/myCirclePageIndicator</item>
<!-- Put whatever other styles you want to define in your custom theme -->
</style>
<style name="myCirclePageIndicator" parent="Widget.CirclePageIndicator">
<item name="fillColor">#color/my_custom_circle_indicator_fill_color</item>
</style>
Then in your Manifest, set android:theme="#style/myTheme" to the <application> or to your <activity>s.
Note that you don't have to include all 4 "vpi..."-styles; you only have to include those that you use. E.g. if you're only using the CirclePageIndicator, you only have to include <item name="vpiCirclePageIndicatorStyle">...</item> and can leave out the other 3 item declarations.
I accidentally removed android:layout_weight="1" when adding styles.
So for other who want to implement VPI with ABS:
layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.viewpagerindicator.TabPageIndicator
android:id="#+id/indicator"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
style="#style/StyledIndicators" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
styles.xml:
<style name="Theme.BataApp" parent="Theme.Sherlock.Light.DarkActionBar">
etc.
</style>
<style name="StyledIndicators" parent="Theme.BataApp">
<item name="vpiTitlePageIndicatorStyle">#style/Widget.TitlePageIndicator</item>
<item name="vpiTabPageIndicatorStyle">#style/Widget.TabPageIndicator</item>
<item name="vpiTabTextStyle">#style/Widget.TabPageIndicator.Text</item>
</style>
Thank you #Reinier! :D

Categories

Resources