Adding a style for ConstraintLayout - android

I want to make a style for ConstraintLayout but it seem nothing happened.
This is my style :
<style name="ConstraintLayoutFragment">
<item name="android:layout_marginStart">#dimen/margin_border_screen</item>
<item name="android:layout_marginEnd">#dimen/margin_border_screen</item>
</style>
This is how I applied the style :
<androidx.constraintlayout.widget.ConstraintLayout
style="ConstraintLayoutFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
Maybe I need to put a parent in the style but I don't found anything for ConstraintLayout.

Oh! I just forget to put a #style/ :
style="#style/ConstraintLayoutFragment"

Related

Constraints are not applied when moving all four of them to themes.XML

I have a ConstraintLayout in which I have placed a ProgressBar right in the center.
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progress_bar"
style="#style/ProgressBarStyle"/>
</androidx.constraintlayout.widget.ConstraintLayout>
All four constraints are located themes.XML file in ProgressBarStyle:
<style name="ProgressBarStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="layout_constraintBottom_toBottomOf">parent</item>
<item name="layout_constraintEnd_toEndOf">parent</item>
<item name="layout_constraintStart_toStartOf">parent</item>
<item name="layout_constraintTop_toTopOf">parent</item>
</style>
Even if the ProgressBar is displayed in the center in Android Studio, see the image below:
I get the following warning:
This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints
So even if the constraints are present in the themes.XML file, I still get this warning. How to solve this?
Because you can not set constraints for view within style.constraints should be under constraint layout.when you write xml code, layout try to find all constraints to place the view in preview window.they won't look at style for constraints.that's why you got warning.to avoid warning move all four constraints from style to layout.
I have come across this in the past - just looks like an case that is not handled when checking the constraints. You are not doing anything wrong.
tools:ignore="MissingConstraints" will get rid of the error. You can apply this to each view that displays the error or in the ConstraintLayout XML so it will apply to the entire layout.

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

FrameLayout not use the styles correctly

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.

How to layout partial overlay ActionBar

I have a problem to implement this situation, Seen here that natively has let the entire layout as belowlayout below.
But I would like to do this, this rectangle Should be a single and clickable item
How to do this ?
If you need to display something over the main content aPopupWindow will be the best choice.
First, you need to create a custom theme that extends an existing action bar theme and set the android:windowActionBarOverlay property to true.
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo">
<item name="android:windowActionBarOverlay">true</item>
</style>
</resources>
And add the following in your layout:
android:paddingTop="?android:attr/actionBarSize"
Example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize"> // <==== add this one
...
</RelativeLayout>
Read more about this.

ListView divider not applied when specified in style declaration

This is more "Why?" than "How?" question, since I know a simple workaround for the problem. But I'd like to know what logic is behind behavior I'm going to describe.
I've got a style declaraion and some ListViews. I want to change the way divider looks. I used following declaration:
<style name="Theme.MyApp.Dark" parent="#style/Theme.Sherlock">
<item name="android:dividerHeight">4px</item>
<item name="android:divider">#123456</item>
</style>
To my amusement, only the height of the divider was applied. I've tried specifying color with alpha value (#12345678) and gradient drawable, nothing works. ListViews are declared like this:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
So, as you can see there's nothing that could override style declaration. I can change the divider only when I specify it directly in ListView declaration:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:divider="#123456"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Using my newly-acquired skill of specifying colors depending on theme selected, I can work around this very easily - I will just have to declare android:divider with custom color attribute for every list.
But why I can't change android:divider in the style, just like the android:dividerHeight? Is there some reason for that? I couldn't find an explanation, bug report or something similar that would let me understand this behavior.
EDIT: The theme is applied globally, in the Manifest file:
<application android:icon="#drawable/icon" android:label="#string/app_name" android:name="org.my.app" android:theme="#style/Theme.MyApp.Dark">
It can be set from a theme but with a little trick:
<style name="MyTheme" parent="AppTheme">
<item name="android:listViewStyle">#style/MyListViewStyle</item>
</style>
<style name="MyListViewStyle" parent="#android:style/Widget.ListView">
<item name="android:divider">#F00</item>
<item name="android:dividerHeight">1px</item>
</style>
Rgrds ;)
You have to mention the style in the Lisview like this
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
style = "#style/Theme.MyApp.Dark"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Otherwise this wont work..

Categories

Resources