How to use support layout_columnWeight in a style? - android

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.

Related

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.

Android style dosen't work

I am trying to add a specific style for a button, the issue is that it's works in themes.xml when I write this :
themes.xml
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorControlHighlight">#color/background</item>
<item name="colorButtonNormal">#color/white</item>
</style>
mylayout.xml
<Button
android:id="#+id/play"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="Play"
android:textColor="#color/guillotine_background"
android:textSize="25dp"
android:textStyle="bold"/>
but I don't get the same result when I use this :
styles.xml
<style name="MyColorButton" parent="AppTheme">
<item name="colorControlHighlight">#color/background</item>
<item name="colorButtonNormal">#color/white</item>
</style>
I added this line to the button tag in mylayout.xml
mylayout.xml
style="#style/MyColorButton"
Isn't that must return the same result ?
colorControlHighlight and colorButtonNormal are theme attributes. They are for styles that are used as themes for an Activity. They are not attributes of a Button. If you want to use it on the Button but not the whole Activity, use android:theme attribute instead:
<Button
android:theme="#style/MyColorButton"
... />
dmk you are doing it right , you just need to change the parent in the style of the button .
just set the parent="Widget.AppCompat.Button" your code will work fine..
<style name="appbutton" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/themeColordark</item>
<item name="android:textColorPrimary">#color/white</item>
</style>
thanks

Apply style only to specific TextViews

I have two themes that I switch back and forth between in my Android application. In some places, I have TextViews that I want to remain the default color. In other places, I want them to be another color (let's say orange). When changing themes, I'd like only the TextViews that were previously orange to become blue.
Is there a way to do this simply in Android? I had something in mind such as a class tag in html/css but can't seem to find anything.
EDIT: To clarify what I was referring to with the html/css equivalent:
<div>This text is black</div>
<div class="redDiv">This text is red</div>
.redDiv {
color:red;
}
Let's say you have two themes in your application: MyFirstTheme and MySecondTheme:
<style name="MyFirstTheme" parent="android:Theme">
<item name="android:textViewStyle">#style/MyFirstTheme.TextView</item>
[...]
</style>
<style name="MySecondTheme" parent="android:Theme">
<item name="android:textViewStyle">#style/MySecondTheme.TextView</item>
[...]
</style>
The textview styles defined in your styles.xml could look like:
<style name="MyFirstTheme.TextView" parent="#android:style/Widget.TextView">
<item name="android:textColor">#android:color/black</item>
[...]
</style>
<style name="MySecondTheme.TextView" parent="#android:style/Widget.TextView">
<item name="android:textColor">#color/orange</item>
[...]
</style>
<style name="PersistentTheme.TextView" parent="#style/MyFirstTheme.TextView">
<item name="android:textColor">#color/red</item>
</style>
So when you have a layout with two TextViews, you can have the one completely follow the active theme by not setting anything extra to it, and you can apply other appearance to your other TextView instance by specifying a style attribute for it:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- This textview will change text color when other style applied -->
<TextView
android:id="#+id/tv_styledependent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- This textview will always have the same (red) text color -->
<TextView
android:id="#+id/tv_persistent"
style="#style/PersistentTheme.TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_styledependent" />
</RelativeLayout>

Android: How do you set default view attributes?

I have multiple buttons that all have the same textSize, layout_width, etc. and I do not want to copy and paste these attributes over and over as it makes for hard to edit code. In other words, I am looking to take many of these:
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/round_button_normal"
android:text="#string/button1"
android:textSize="50sp" />
And instead be able to use a preset button (where the layout_width, layout_height, layout_weight, background, and textSize are all set to the above values, by default):
<PresetButton
android:id="#+id/button1"
android:text="#string/button1" />
Just use styles.
Styles and Themes
Example:
In: res/values/styles.xml
<style name="Numbers">
<item name="android:inputType">number</item>
</style>
Use like this:
<EditText
style="#style/Numbers" />
You can use styles.xml to create a style, then you can apply that styling to each button.
http://developer.android.com/guide/topics/ui/themes.html
in your values -> style.xml file put the xml attribute
<style name="buttonConfirm" parent="#android:style/Widget.Button">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">15dip</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
and in your layout call it by this xml
<Button
android:id="#+id/button1"
style="#style/buttonConfirm"
android:text="Button"
/>

android: set textColor with textAppearance attribute

I have created several styles for font in my app.
How can I add these styles to views? - 1) Using style attribute 2) Using textAppearance.
To use style is not an option because views may have other attributes (margins, paddings, min width, etc - I cant specify 2 styles for a view), so I need to specify text-related attributes separately, but android:textColor doesnt work here:
styles.xml:
<style name="TextAppearance.baseText" parent="#android:style/TextAppearance">
<item name="android:textAppearance">?android:textAppearanceSmall</item>
<item name="android:typeface">sans</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textSize">15sp</item>
</style>
layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="65dp"
android:orientation="horizontal" >
<Button
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sometext"
android:textAppearance="#style/TextAppearance.baseText"/>
</RelativeLayout>
Why doesnt it work? How can I specify textcolor in textappearance?
As Oderik has mentioned in the comments, the Button view has a default value for it's textColor attribute. This textColor value overrides whatever value is set through the textAppearance attribute. Therefore you have two options:
Set the textColor to #null in the style:
<style name="Application.Button">
<item name="android:textAppearance">#style/Application.Text.Medium.White</item>
<item name="android:textColor">#null</item>
</style>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Application.Button" />
Set the textColor to #null in the Button XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Application.Button"
android:textColor="#null" />
It works. Your text color is white - the same as default color. Put there any other color like <item name="android:textColor">#AA6699</item> and you will see difference.
Second thing is that you are trying to set text appearance in text appearance - doen's make sense. If you want to set small letters do this using parent parent="#android:style/TextAppearance.Small and delete line <item name="android:textAppearance">?android:textAppearanceSmall</item>
Third thing is that you want to have small letters and put additional textSize - doesn't make sense.
Try this, works for me:
<style name="BaseText" parent="#android:style/TextAppearance.Small">
<item name="android:typeface">sans</item>
<item name="android:textColor">#AA7755</item>
</style>
If you want to set everything in style:
<style name="BaseText" parent="#android:style/TextAppearance.Large">
<item name="android:typeface">sans</item>
<item name="android:textColor">#AA7755</item>
</style>
<style name="BaseText.Margins">
<item name="android:layout_margin">10dip</item>
</style>
Where BaseText.Margins inherits from BaseText.
Then you can just use:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
style="#style/BaseText.Margins" />
If you want to use android:TextAppearance instead of style: to set your android:textColor, you need to set android:textColor=#null on your TextView.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:textAppearance="#style/TextAppearance.AppCompat.Medium.MySubheader"
android:textColor="#null"
tools:text="Section" />
Then it will inherit correctly from your android:TextAppearance
<style name="TextAppearance.AppCompat.Medium.MySubheader">
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/black_54</item>
</style>

Categories

Resources