FrameLayout not use the styles correctly - android

I am very happy with this website. I'm learning a lot.
Today I doubt has arisen. And I want to put a style to a FrameLayout. and do not use.
The style is as follows:
<style name="textAsk">
<item name="android:textColor">#000000</item>
<item name="android:padding">2dp</item>
<item name="android:minWidth">88dp</item>
<item name="android:minHeight">36dp</item>
<item name="android:textSize" >18dp</item>
<item name="android:layout_marginRight">12dp</item>
<item name="android:layout_marginLeft">12dp</item>
<item name="android:layout_marginTop">5dp</item>
</style>
I show fragments that are changing in a FrameLayout. Each Fragment contains a TextView with a text in it.
There are a lot of fragments, and I would like to set a style, a common one, for all the TextViews, in order to save time and not setup the style in each TextView.
I had tried this code:
<FrameLayout
android:id="#+id/fragmentaskGRP1"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
style="#style/textAsk"/>
But the only thing that works for me is:
<item name="android:layout_marginRight">12dp</item>
<item name="android:layout_marginLeft">12dp</item>
<item name="android:layout_marginTop">10dp</item>
Thank you very much for everything

The FrameLayout doesn't support the textColor or textSize attribute (API). So it's never set for the FrameLayout and ignored. See the style properties section from the guide Styles and Themes for more information.
Quote from the Guide:
However, if you apply a style to a View that does not support all of the style properties, the View will apply only those properties that are supported and simply ignore the others.
Define the text-related styles in a separate style definition and use it for this one for the matching views like TextView

The thing is that child views don't inherit styles from their enclosing ViewGroup. Styles can have parents, but in your case TextViews are not going to get these attributes from the FrameLayout.

The other styles will not be applied to fragments. You have to create another logic to apply styles to all fragments (Most probably you will have to apply styles individually to each fragment)
FrameLayout have nothing to do with text so textColor, textSize will have no effect.
Where as minWidth, minHeight are properties of View it think they should work.

Related

Styles for Children of ConstraintLayout

I wanna set general style for my inner elements of my ConstraintLayout. For example, I have multiple TextViews with following attributes:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
I created this style for it:
<style name="PageTitleStyle">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginRight">8dp</item>
<item name="android:layout_marginLeft">8dp</item>
</style>
but how can I set these attributes to the defined style?
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
You can define your attributes in styles as follows:
<style name="MyStyle">
<item name="layout_constraintLeft_toLeftOf">parent</item>
<item name="layout_constraintRight_toRightOf">parent</item>
</style>
You can then specify style="#style/MyStyle" on each TextView.
Setting the style on the ConstraintLayout will not set the style on the children of the ConstraintLayout unless you set the style as a theme. See "Apply a style as a theme". (Emphasis is mine.)
Beginning with Android 5.0 (API level 21) and Android Support Library v22.1, you can also specify the android:theme attribute to a view in your layout file. This modifies the theme for that view and any child views, which is useful for altering theme color palettes in a specific portion of your interface.
So you would add android:theme="#style/MyStyle" to the ConstraintLayout. This will replace the existing theme, so you may want to set your AppTheme as the parent of MyStyle.
There is one odd-looking side effect that I have noticed in doing this: The constraints named in the style effect the display of the layout in the studio designer (correctly) but the constraints themselves do not display. The layout editor will also not pick up that the constraints are defined in the style and will give "constraint missing" errors. (Android Studio 3.3 RC3)
Relation constraints can not be used in "style". Try to use it in layout

xamarin android alignParentRight style

can you tell me how can I add alignParentRight into my xml style?
<style name="My_Style">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">75dp</item>
<item name="android:background">#color/Gray_75</item>
**<item name="android:alignParentRight">true</item>**
</style>
yes but I need to add it programatically for dynamic and I will add only style and thats all.
Generally you can't do that programmatically. What we can do is:
Create two different styles, one with <item name="android:alignParentRight">true</item> and the other one not, apply them to the control when it is needed. Since in which scenario of your styles will be used is not clear in your question, maybe you can look into State List.
I personally think it is more straight if we directly set the Layout Parameters in code behind, for example:
var parameters = btn.LayoutParameters as RelativeLayout.LayoutParams;
parameters.AddRule(LayoutRules.AlignParentRight);
The btn in this code refer to a Button control.

Why are some of my styles not working with lower APIs?

I have a quick question!
In my styles.xml file, I have
<style name="TextViewStyle" parent="android:Widget.TextView">
<item name="android:padding">20px</item>
<item name="android:background">#9cd0e8</item>
<item name="android:textColor">#254b7c</item>
<item name="android:textSize">18sp</item>
<item name="android:textStyle">bold</item>
</style>
And in my activity_main.xml, I have
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:theme="#+styles/TextViewStyle"
android:text="Sample Text"/>
What I am trying to do is, in my Android application, on a certain activity I plan to place many TextViews with similar properties. Instead of writing these 'properties' every time with each TextView instance, I grouped them together in a style in styles.xml file and set theme of each of my TextViews to that style.
It works fine and does what I want it to do, but only with APIs above 21! My application's supposed to support devices from API level 15 up. Why is my approach not working with lower APIs?
Please help soon. I need to finish this soon.
EDIT
By 'working', I meant that the attributes I set in my style (padding, color, etc.) appear on the TextViews as they should. In lower APIs however, the TextViews appear as if I had not applied any attribute on them. Plain text appears instead of a styled one.
remove parent from your style
remove android:theme from textView, (why there is + sign?)
instead of theme put this into your textView
style="#style/TextViewStyle"
btw, use dp instead of px ;)

Issues changing the color of views

So I am trying to change my app color to blue and some of most the views I have are not willing to cooperate with me.
Here is the image:
Here I want to change the color of the green parts on the spinner, edittext and checkbox views (which are green) to black or blue.
I've looked all over Stack Overflow and I can't find the solution!
Thank you very much, If possible I would like to have a XML solution but I wouldn't mind a programmatic solution!
Add these to your base theme in styles.xml
<item name="colorControlNormal">#color/YOUR_COLOR</item>
<item name="colorControlActivated">#color/YOUR_COLOR</item>
<item name="colorControlHighlight">#color/YOUR_COLOR</item>
Note: The above change will affect the EditTexts and other views probably throughout the application.
If not, and if you are using the AppCompat v22 support library, you can specify the theme in the EditText like: android:theme="#style/Theme.App.Base.
This will ensure the style won't also affect other views in your layouts that you don't want to change
Also if you want to change the above solution, just add another Theme specific to EditTexts and Spinners and apply it to all Spinners if you want
<style name="MyWidgetTheme">
<item name="colorControlNormal">#color/YOUR_COLOR</item>
<item name="colorControlActivated">#color/YOUR_COLOR</item>
<item name="colorControlHighlight">#color/YOUR_COLOR</item>
</style>
and in your EditText, Spinner or any other View, just assign this theme:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Demo"
android:lines="1"
android:theme="#style/MyWidgetTheme"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/MyWidgetTheme"></Spinner>
Take a look to this resource generator.
Choose your color, the widgets you want to generate and voila! You copy them to your project and reference them in your xmls.

Different formats for different Textview defined by Custom style

I have a problem figuring out how to do this:
I am currently coding an app that comes with different themes (User can select the complete appereance of the app out of a list of different styles).
Then the list item is selected I want to call setTheme(R.style.Own_App_Style0); to change the complete appearance.
The problem is best explained by an example:
Lets say we have 2 TextView.
Theme1
1. TextView: TextColor should be green and TextSize 15sp.
2. TextView: TextColor should be red and TextSize 10sp.
Theme2
1. TextView: TextColor should be blue and TextSize 10sp.
2. TextView: TextColor should be yellow and TextSize 10sp.
Of course I know that by setting <item name="textViewStyle">#android:style/Widget.TextView</item> I can change the default appearance of TextViews.
But how can it be done to have lets say two (ore more) different types of TextView with different applied styles (and by xml)?
Found a solution (basically in this answer setTextAppearance through code referencing custom attribute). In case anyone else has this problem I shortly explain:
Declare in style.xml a attribute and in the actual style definition asign a value (reference) to this attribute:
<declare-styleable name="CustomTextView">
<attr name="mainTextView" format="reference"/>
</declare-styleable>
<style name="appstyle0" parent="android:style/Theme.Holo.Light">
<item name="#attr/mainTextView">#style/CustomTextViewAppearance1</item>
<item name="android:textViewStyle">#style/CustomTextViewAppearance2</item>
</style>
<style name="appstyle1" parent="android:style/Theme.Holo.Light">
<item name="#attr/mainTextView">#style/CustomTextViewAppearance2</item>
<item name="android:textViewStyle">#style/CustomTextViewAppearance1</item>
</style>
<style name="CustomTextViewAppearance1">
<item name="android:textSize">10dip</item>
</style>
<style name="CustomTextViewAppearance2">
<item name="android:textSize">30dip</item>
</style>
Now in the layout all textViews are like CustomTextViewAppearance2 (because this is set as standard in this style. And the textViews that should use the other style write into the definition:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blablabla"
style="?mainButtonTextView"/>
When you now call .setTheme (after restart the activity) the appearance of the textviews switch. Like this method you can define as many different types of View styles and switch between them only by calling .setTheme.
Unfortunately, styles are static once they are defined. To have an entire cascade of styles modified programmatically, you would have to change the definition of the style itself. Instead, all you can do is change the style that is assigned to a TextView (or whatever style-able object), as outlined in the question I linked to in my comment above.

Categories

Resources