In HTML/CSS you can override all paragraphs by defining p in CSS
I.e.
p { color: blue;
}
Can you do mimc this in Android? I'd like to define my set of styles for all form elements in styles.xml and then every layout I write would use these properties.
At the moment I have to specify the style in the layout: i.e
<EditText android:id="#+id/address1" style="#style/EditText" />
Which seems a bit redundant (and error prone).
Thanks,
John
You have to create an xml file which defines the style that you want, and store it in res/values folder.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
This is an example of an style, once you have your style. you can applay it to every view by doing something like this:
<TextView
style="#style/CodeFont"
android:text="#string/hello" />
This example is from:
http://developer.android.com/guide/topics/ui/themes.html
Related
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.
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
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.
I have a simple problem. I'd like to provide multiple themes for my UI that a user can switch between
The problem is that I can use a theme that styles primitive UI classes, or I can use a style directly on an XML layout element, but I can't define a style that will then change based on the theme I have applied. Not all of the styling I want to do is based on the primitive type.
I would say that what I want to do is similar to using a CSS selector to style a class or ID, but themes only allow you to style an element name.
The best I can do right now is have a base style that inherits from EITHER of the actual styles I've built, as below:
My res/values/style.xml
// All styles have to be subclassed here to be made generic, and only one
// can be displayed at a time. Essentially, I'm doing the same thing as setting
// a theme does, but for styles. If I have 50 styles, I have to have 50 of
// these for each theme, and that's not DRY at all!
<style name="MyHeaderStyle" parent="MyHeaderStyle.Default"/>
<!--
<style name="MyHeaderStyle" parent="MyHeaderStyle.Textures"/>
-->
<style name="MyHeaderStyle.Default">
<item name="android:background">#android:color/background_dark</item>
</style>
<style name="MyHeaderStyle.Textures">
<item name="android:background">#drawable/header_texture</item>
</style>
Usage in my layout.xml:
<!-- Note that I can't use a theme here, because I only want to style a
SPECIFIC element -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/MyHeaderStyle"
android:gravity="center">
</LinearLayout>
Try this method:
1) Define your own res/values/attrs.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AppTheme">
<attr name="myLinearLayoutStyle" format="reference" />
</declare-styleable>
</resources>
2) Assign the above attr to you LinearLayout
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?attr/myLinearLayoutStyle"
android:gravity="center">
</LinearLayout>
3) Assign a concrete style to this attr:
<style name="AppLightTheme" parent="android:Theme.Holo.Light">
<item name="#attr/myLinearLayoutStyle">#style/lightBackground</item>
</style>
<style name="AppTheme" parent="android:Theme.Holo">
<item name="#attr/myLinearLayoutStyle">#style/darkBackground</item>
</style>
<style name="lightBackground">
<item name="android:background">#D1CBF5</item>
</style>
<style name="darkBackground">
<item name="android:background">#351BE0</item>
</style>
Hope I understood correctly your question.
I have a login screen that is branded differently for different builds of my application. I need the background image to be different in the layout file for this screen, so I want to point to a different style for the top level container. I'm a bit at a loss at how to do this.
I have declared a styleable something like:
<resources>
<declare-styleable name="ThemeBase">
<attr name="loginPageContainerStyle" format="reference" />
</declare-styleable>
</resources>
I have several different themes for the application, as such:
<resources>
<style name="ThemeBase" parent="android:style/Theme.Light" />
<style name="ThemeOne" parent="ThemeBase">
<item name="loginPageContainerStyle">#style/loginPageContainerThemeOne</item>
</style>
<style name="ThemeTwo" parent="ThemeBase">
<item name="loginPageContainerStyle">#style/loginPageContainerThemeTwo</item>
</style>
</resources>
And I have defined the following styles:
<resources>
<style name="loginPageContainerThemeOne">
<item name="android:background">#drawable/background_theme_one</item>
</style>
<style name="loginPageContainerThemeTwo">
<item name="android:background">#drawable/background_theme_two</item>
</style>
</resources>
And finally a login.xml file something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/loginRoot"
style= [ ? WHAT GOES HERE ? ]
android:gravity="center_horizontal"
android:orientation="horizontal">
[ LAYOUT STUFF ... ]
</LinearLayout>
Am I doing anything wrong? Can this be done this way?
Ok I figured it out, the style reference should be:
style="?attr/loginPageContainerStyle"
Figured I would share.