android:style not applying in xml layout - android

I need to apply a custom style to a Spinner.
These are my styles in styles.xml:
<style name="spinnerItemStyle">
<item name="android:textColor">#color/font</item>
<item name="android:textSize">#dimen/small_text</item>
</style>
<style name="spinnerDropDownItemStyle">
<item name="android:padding">10dp</item>
<item name="android:textColor">#color/font</item>
<item name="android:textSize">#dimen/small_text</item>
<item name="android:background">#color/unpressed2</item>
</style>
And I'm doing this in the layout:
android:spinnerItemStyle="#style/spinnerItemStyle"
android:spinnerDropDownItemStyle="#style/spinnerDropDownItemStyle"
<Spinner
android:id="#+id/songSpinner"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:textSize="#dimen/small_text"
android:entries="#array/songs_array"
android:spinnerItemStyle="#style/spinnerItemStyle"
android:spinnerDropDownItemStyle="#style/spinnerDropDownItemStyle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.18"
app:layout_constraintTop_toBottomOf="#+id/musicSeekBar"
app:layout_constraintWidth_percent="0.5" />
The problem is that Spinner is ignoring the styles applied.
And also I disscovered that it works perfectly if I do this in my custom app theme inside styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="colorButtonNormal">#color/unpressed2</item>
<item name="android:buttonStyle">#style/ButtonColor</item>
<item name="android:textColor">#color/font</item>
<!-- spinner styles -->
<item name="android:spinnerItemStyle">
#style/spinnerItemStyle
</item>
<item name="android:spinnerDropDownItemStyle">
#style/spinnerDropDownItemStyle
</item>
</style>
Why works with my second approach and not with the first?

styles.xml File
Add below xml code in your styles.xml File.
<style name="CustomSpinner" parent="AppTheme">
<item name="android:spinnerItemStyle">#style/spinnerItemStyle</item>
<item name="android:spinnerDropDownItemStyle">#style/spinnerDropDownItemStyle</item>
</style>
yourlayout.xml
In Spinner use this styles
For E.g:-
<Spinner
android:id="#+id/songSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/CustomSpinner">
</Spinner>

Related

Change Version Of Material 1.1.0 to 1.3.0

And my Second Question :
I use a TextInputLayout :
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColorHint="#color/LightBlue"
android:theme="#style/ThemeOverlay.AppTheme.TextInputEditText.Outlined"
app:boxStrokeColor="#color/selector"
app:boxStrokeWidth="3dp"
app:startIconTint="#color/white">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/numberOfPlayer_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="numberDecimal"
android:textColor="#color/gold" />
</com.google.android.material.textfield.TextInputLayout>
The Styles file
<style name="AppTheme"
parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowBackground">#color/Thistle</item>
<!-- use in get player information -->
<item name="CustomTextStyle">#style/Widget.App. TextInputLayout.
OutlinedBox</item>
</style>
<style name="ThemeOverlay.AppTheme.TextInputEditText.Outlined"
parent="">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="boxCornerRadiusBottomEnd">20dp</item>
<item name="boxCornerRadiusBottomStart">20dp</item>
<item name="boxCornerRadiusTopEnd">20dp</item>
<item name="boxCornerRadiusTopStart">20dp</item>
<item name="android:hint">Number of player</item>
<item name="hintTextColor">#color/card1</item>
<item name="startIconDrawable">#drawable/ic_people</item>
</style>
<style name="Widget.App.TextInputLayout.OutlinedBox"
parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">#color/player_information</item>
<item name="boxCornerRadiusBottomEnd">15dp</item>
<item name="boxCornerRadiusBottomStart">15dp</item>
<item name="boxCornerRadiusTopEnd">15dp</item>
<item name="boxCornerRadiusTopStart">15dp</item>
<item name="android:textColorHint">#color/white</item>
<item name="startIconDrawable">#drawable/player</item>
<item name="startIconTint">#color/gold</item>
<item name="elevation">5dp</item>
<item name="boxStrokeWidth">2dp</item>
<itemname="materialThemeOverlay">#style/ThemeOverlay.App.
TextInputEditText.OutlinedBox</item>
<item name="hintTextColor">#color/AntiqueWhite</item>
<!-- .... -->
</style>
<style name="ThemeOverlay.App.TextInputEditText.OutlinedBox"
parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
<!-- to change the cursor color -->
<item name="colorControlActivated">#color/white</item>
</style>
That is my selector file
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/DarkSeaGreen"
android:state_focused="true"/>
<item android:color="#color/white"/>
</selector>
and Attrs
<resources>
<attr name="customTextInputStyle" format="reference" />
<attr name="CustomTextStyle" formet ="reference"/>
</resources>
When I change the version of the material 1.1.0 to 1.3.0 ,The shape of The text input layout1 I created before is distorted. It became wider than it was before. I could not find the reason for this change.As you can see in the picture, the text inputlayout is very wide and the hinttext still looks black on the back.
The main issue is in the ThemeOverlay. You have to add the parent ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox.
<style name="ThemeOverlay.App.TextInputEditText.OutlinedBox" parent="#style/ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
<item name="colorPrimary">#color/...</item>
</style>
It is needed because the default style Widget.MaterialComponents.TextInputLayout.OutlinedBox which you are extending has defined a own materialThemeOverlay and
without the parent dependency you are losing the default behaviour.
Also in your xml layout remove android:theme="#style/ThemeOverlay.AppTheme.TextInputEditText.Outlined". You don't need it because you are already using materialThemeOverlay in the style and the theme defined in the xml layout overrides it and this theme overlay is wrong.
Also:
use a min width because you are using a big corner radius 15dp
<com.google.android.material.textfield.TextInputLayout
android:minWidth="150dp"
android:layout_width="wrap_content"
Finally the color used by the hint:
<item name="android:textColorHint">#color/red600Dark</item>
<item name="hintTextColor">#color/teal_700</item>
Final layout:
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.App.TextInputLayout.OutlinedBox"
android:minWidth="150dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:startIconTint="#color/white">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/numberOfPlayer_txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="numberDecimal"
android:textColor="#color/gold" />
</com.google.android.material.textfield.TextInputLayout>
with:
<style name="Widget.App.TextInputLayout.OutlinedBox"
parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">#color/text_input_stroke_selector</item>
<item name="boxCornerRadiusBottomEnd">15dp</item>
<item name="boxCornerRadiusBottomStart">15dp</item>
<item name="boxCornerRadiusTopEnd">15dp</item>
<item name="boxCornerRadiusTopStart">15dp</item>
<item name="startIconDrawable">#drawable/ic_...</item>
<item name="startIconTint">#color/...</item>
<item name="elevation">5dp</item>
<item name="boxStrokeWidth">2dp</item>
<item name="materialThemeOverlay">#style/ThemeOverlay.App.TextInputEditText.OutlinedBox</item>
<item name="android:textColorHint">#color/....</item>
<item name="hintTextColor">#color/....</item>
</style>
<style name="ThemeOverlay.App.TextInputEditText.OutlinedBox" parent="#style/ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
<item name="colorPrimary">#color/....</item>
</style>

Android Material Edit Text endIcon

I've added an EditText view from Google's material library and set the endIcon mode to clear_text. However, the endIcon is too big (it fits the text view entirely), whereas, from looking at the documentation, I believe it should be a little smaller. I tried to play around with the layout XML but can't seem to find out why this is. I've also looked at the endIcon attributes in the Material repository and there seems to be no endIconSize attribute. I'd appreciate it if someone could point out where I made the mistake.
TextView XML
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/descriptionTextField"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:boxBackgroundColor="#android:color/white"
app:boxStrokeColor="#color/colorSecondaryLight"
app:boxStrokeWidth="2dp"
app:endIconMode="clear_text"
app:errorEnabled="false"
app:layout_constraintBottom_toBottomOf="#+id/taskConfirmedBtn"
app:layout_constraintEnd_toStartOf="#+id/taskConfirmedBtn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/taskConfirmedBtn">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/descriptionInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:inputType="textCapSentences"
android:singleLine="true"
android:textColor="#color/colorOnSurface" />
Styles xml
<resources>
<!-- Base application theme -->
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="android:colorBackground">#color/background</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryVariant">#color/colorPrimaryLight</item>
<item name="colorOnPrimary">#color/colorOnPrimary</item>
<item name="colorSecondary">#color/colorSecondary</item>
<item name="colorSecondaryVariant">#color/colorSecondaryLight</item>
<item name="colorOnSecondary">#color/colorOnSecondary</item>
<item name="toolbarStyle">#style/Widget.Taskeroo.Toolbar</item>
<item name="actionBarTheme">#style/Widget.Taskeroo.ActionBar</item>
<item name="floatingActionButtonStyle">#style/Widget.Taskeroo.FloatingActionButton</item>
<item name="imageButtonStyle">#style/Widget.Taskeroo.ImageButton</item>
<item name="materialCardViewStyle">#style/Widget.Taskeroo.CardView</item>
<item name="bottomSheetDialogTheme">#style/CustomBottomSheetDialog</item>
<item name="android:textViewStyle">#style/SanSerifLightTextView</item>
<item name="android:buttonStyle">#style/SansSerifLightButton</item>
<item name="android:radioButtonStyle">#style/SansSerifLightButton</item>
</style>
<!-- Themes -->
<style name="SplashScreenTheme" parent="AppTheme.NoActionBar">
<item name="android:windowBackground">#drawable/splash_screen_bitmap</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="AppTheme.NoActionBar.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.NoActionBar.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<!-- Widgets -->
<style name="Widget.Taskeroo.ActionBar" parent="Widget.MaterialComponents.ActionBar.Primary">
<item name="android:colorControlNormal">#color/colorOnPrimary</item>
<item name="android:iconTint">#color/colorOnPrimary</item>
</style>
<style name="Widget.Taskeroo.Toolbar" parent="Widget.MaterialComponents.Toolbar.Primary">
<item name="titleTextColor">#color/colorOnPrimary</item>
</style>
<style name="Widget.Taskeroo.FloatingActionButton" parent="Widget.MaterialComponents.FloatingActionButton">
<item name="android:backgroundTint">#color/colorSecondaryLight</item>
<item name="tint">#color/colorOnSecondary</item>
</style>
<style name="Widget.Taskeroo.CardView" parent="Widget.MaterialComponents.CardView">
<item name="cardBackgroundColor">#color/colorSurface</item>
<item name="cardElevation">10dp</item>
<item name="android:elevation">8dp</item>
</style>
<style name="DividerStyle">
<item name="android:background">#color/colorSecondary</item>
</style>
<style name="Widget.Taskeroo.ImageButton" parent="Widget.AppCompat.Button.Borderless.Colored">
<item name="tint">#color/colorSecondary</item>
</style>
<!-- Components -->
<style name="CustomBottomSheetDialog" parent="#style/ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="bottomSheetStyle">#style/CustomBottomSheet</item>
</style>
<style name="CustomBottomSheet" parent="Widget.MaterialComponents.BottomSheet">
<item name="shapeAppearanceOverlay">#style/CustomShapeAppearanceBottomSheetDialog</item>
<item name="backgroundTint">#color/colorSurface</item>
</style>
<style name="CustomShapeAppearanceBottomSheetDialog" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">16dp</item>
<item name="cornerSizeTopLeft">16dp</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
<!-- Fonts -->
<style name="SanSerifLightTextView" parent="android:Widget.TextView">
<item name="android:fontFamily">sans-serif-light</item>
</style>
<style name="SansSerifLightButton" parent="android:Widget.Holo.Button">
<item name="android:fontFamily">sans-serif-light</item>
</style>
<style name="SansSerifLightRadioButton" parent="android:Widget.Holo.CompoundButton.RadioButton">
<item name="android:fontFamily">sans-serif-light</item>
</style>
<style name="AppTheme.NoActionBar.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
Your issue is here:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="imageButtonStyle">#style/Widget.Taskeroo.ImageButton</item>
</style>
<style name="Widget.Taskeroo.ImageButton" parent="Widget.AppCompat.Button.Borderless.Colored">
<item name="tint">#color/colorSecondary</item>
</style>
You are using Widget.AppCompat.Button.* instead of Widget.AppCompat.ImageButton.
The style Widget.AppCompat.Button.Borderless.Colored has a custom dimens.
Change the code like this :
<com.google.android.material.textfield.TextInputLayout
app:endIconDrawable="#drawable/customDrawable">
...
</com.google.android.material.textfield.TextInputLayout>
where custom drawable, is a vector/png image of any shape and size you will define.
If this still doesn't solve your problem, try downgrading to :
implementation 'com.google.android.material:material:1.0.0'
Most probably it will solve your problem.
try this-->
Add dependency:
Gradle:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependency:
implementation 'com.github.HITGIF:TextFieldBoxes:1.4.3'
Use app:endIcon in xml or setEndIcon(Int resourceID) to set the icon of the ImageButton that is shown at the end of the field if you want there to be one.
<studio.carbonylgroup.textfieldboxes.TextFieldBoxes
...
app:endIcon="#drawable/ic_mic_black_24dp"
>
in java:
final TextFieldBoxes textFieldBoxes = findViewById(R.id.text_field_boxes);
textFieldBoxes.getEndIconImageButton().setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View view) {
// What you want to do when it's clicked
}
});
Try this solution in your_view.xml
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/til_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true"
app:hintEnabled="false"
app:passwordToggleDrawable="#drawable/show_password_selector" <-- add this line
app:endIconMode="password_toggle" <-- add this line
app:passwordToggleEnabled="true"> <-- add this line
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_password"
android:inputType="textPassword"/>
</com.google.android.material.textfield.TextInputLayout>
You can also use this solution for the start icon
I hope help

Why is my EditText style not being applied?

I try like this:
styles.xml:
<style name="EditTextStyle" parent="Widget.AppCompat.EditText">
<item name="android:layout_marginBottom">12dp</item>
<item name="android:backgroundTint">#color/editTextUnderline</item>
<item name="android:drawableLeft">#drawable/ic_sample_action</item>
<item name="android:drawablePadding">8dp</item>
<item name="android:paddingTop">15dp</item>
<item name="android:paddingBottom">25dp</item>
<item name="android:textColor">#color/colorTextPrimary</item>
<item name="android:textColorHint">#color/editTextHintColor</item>
<item name="android:textSize">16sp</item>
<item name="android:fontFamily">#font/viga</item>
<item name="android:inputType">text</item>
</style>
And then I use it in layout file:
<EditText
style="#style/EditTextStyle"
android:id="#+id/input_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Title"
android:text="#={viewModel.title}" />
However I do not see any changes. Why could that be?
I chanded style parent to #style/Widget.AppCompat.EditText and it worked.

How to Change CardView background color from a theme

In an app that has multiple themes, how would you change the background color of the CardView from the styles file?
styles.xml
<style name="AppTheme1" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimaryTheme1</item>
<item name="colorPrimaryDark">#color/colorPrimaryDarkTheme1</item>
<item name="colorAccent">#color/colorAccentTheme1</item>
</style>
<style name="AppTheme2" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimaryTheme2</item>
<item name="colorPrimaryDark">#color/colorPrimaryDarkTheme2</item>
<item name="colorAccent">#color/colorAccentTheme2</item>
</style>
<style name="AppTheme3" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimaryTheme3</item>
<item name="colorPrimaryDark">#color/colorPrimaryDarkTheme3</item>
<item name="colorAccent">#color/colorAccentTheme3</item>
</style>
CardView
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:elevation="4dp"
app:cardCornerRadius="15dp">
I want to change the background color for each theme and I'm hoping to accomplish this in the styles.xml file.
Try the below line of code
Write the below line of code in style.xml
<style name="CustomCardview1" parent="CardView">
<item name="cardBackgroundColor">#F3921F</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">10dp</item>
</style>
<style name="CustomCardview2" parent="CardView">
<item name="cardBackgroundColor">#F3931F</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">10dp</item>
</style>
Write the below line of code in layout where you need the cardview
<android.support.v7.widget.CardView
style="#style/CustomCardview">
hope it will help for you

How to use parent style in android xml

How to use parent style in android xml. Code is here. its not working.
And i have added layout.xml and style.xml.
Please advise for this.
layout.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginRight="25dp"
android:orientation="horizontal">
<TextView
style="#style/textbold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Catering: "/>
<TextView
style="#style/textwhiteitalic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Veg, 20-50, Lunch, Wedding, 15 Oct, Breakfast ..."/>
</LinearLayout>
style.xml
<resources>
<style name="textbold">
<item name="android:textSize">16sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#fff</item>
</style>
<style name="textwhite">
<item name="android:textColor">#fff</item>
<item name="android:textSize">16sp</item>
</style>
<style name="textwhiteitalic" parent="#style/textwhite">
<item name="android:textStyle">italic</item>
</style>
try this you directly add parent like parent="#style/textwhite"
<style name="textwhiteitalic" parent="#style/textwhite">
<item name="android:textStyle">italic</item>
</style>
For more details go to http://developer.android.com/guide/topics/ui/themes.html
Update: try this way
<style name="txt_style_row" parent="android:Widget.TextView">
<item name="android:textColor">#fff</item>
<item name="android:textSize">16sp</item>
<item name="android:textStyle">italic</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">italic</item>
</style>
and set style to your TextView like
style="#style/txt_style_row"
For this what you need to do is ::
<style name="textwhite">
<item name="android:textColor">#fff</item>
<item name="android:textSize">16sp</item>
</style>
<style name="textwhite.textwhiteitalic" >
<item name="android:textStyle">italic</item>
</style>
--
<TextView
style="#style/textwhite.textwhiteitalic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Veg, 20-50, Lunch, Wedding, 15 Oct, Breakfast ..."/>

Categories

Resources