I'm implementing counter in my app.
At the moment I would like to implement a custom color and style for the counter. To do this I created the following style among my themes:
<style name="CounterTextAppearance" parent="TextAppearance.AppCompat.Small">
<item name="android:fontFamily">#font/karla_bold</item>
<item name="counterTextColor">#color/red</item>
<item name="counterMaxLength">60</item>
<item name="android:textSize">13sp</item>
</style>
This is called within my textView:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/reason_layout"
style="#style/TextInputLayoutStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/general_margin_top"
android:hint="#string/bullettin_reason_field"
app:counterEnabled="true"
app:counterTextAppearance="#style/CounterTextAppearance"
app:error="#{bindingViewModel.reasonValidator.error}"
app:errorEnabled="true"
app:helperText="#string/reason_helper_text"
app:helperTextEnabled="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/code_layout">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/edit_reason"
style="#style/TextInputEditTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapSentences"
android:maxLength="60"
android:text="#={bindingViewModel.reasonLiveData}"/>
</com.google.android.material.textfield.TextInputLayout>
The problem is that no style is applied and I don't understand why. What am I doing wrong?
Edit
Changing parent counter style not change yet
#font/karla_bold
#color/red
13sp
Related
This is the activity_register.xml :
<EditText
android:id="#+id/editTextTextPassword"
android:layout_width="346dp"
android:layout_height="49dp"
android:layout_marginTop="32dp"
android:drawableStart="#drawable/ic_vpn_key"
android:drawableEnd="#drawable/ic_visibility"
android:ems="10"
android:hint="#string/password_hint"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextTextEmailAddress" />
So,
I have password EditText
When you click the drawableEnd, you should see the password and the drawable should change to another one, which then hides the password again.
I found tutorials where you can show the password -> worked
_________________________change the drawable -> didn't work
But, I found no tutorial for a onClickListener inside a drawable for kotlin
Problem short
Code to show the password if the drawable is clicked, and if it clicked another time the password hides again.
What you want is TextInputLayout from the Material Componenets.
Here's how you do it:
Implement Material Library in your build.gradle(app) file as:
implementation 'com.google.android.material:material:1.3.0-alpha01'
Then, change the XML's EditText to
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/passwordLayout"
style="#style/TextInputLayoutAppearanceFilled"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/password"
app:endIconMode="password_toggle" //This is used to set the password toggle behavior
app:startIconDrawable="#drawable/ic_lock_black_24dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textSize="#dimen/_12ssp" />
</com.google.android.material.textfield.TextInputLayout>
Then, Change the Style as you want in styles.xml:
<style name="TextInputLayoutAppearanceFilled" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<item name="hintTextAppearance">#style/HintText</item>
<item name="helperTextTextAppearance">#style/HelperText</item>
<item name="android:textColor">#color/dark_grey</item>
<item name="android:textColorHint">#color/color</item>
<item name="hintTextColor">#color/color</item>
<item name="boxStrokeColor">#color/color</item>
<item name="startIconTint">#color/color</item>
<item name="endIconTint">#color/color</item>
<item name="boxBackgroundColor">#color/white</item>
<item name="boxCornerRadiusBottomEnd">#dimen/_26sdp</item>
<item name="boxCornerRadiusBottomStart">#dimen/_26sdp</item>
<item name="boxCornerRadiusTopEnd">#dimen/_26sdp</item>
<item name="boxCornerRadiusTopStart">#dimen/_26sdp</item>
<item name="boxStrokeWidthFocused">0dp</item>
<!--This destroys the visible layout in layout editor so first
comment this out to design-->
<!--<item name="boxStrokeWidth">0dp</item>-->
<item name="hintEnabled">true</item>
</style>
Here's how the output will look like:
The Password toggle will work exactly how you want it, also, the icon changes to the other one as well. You can further customize it as you want, have a look at the official documentation first - TextInputLayout
Just use the TextInputLayout provided by the Material Components Library with the password toggle end icon:
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
app:startIconDrawable="#drawable/..."
app:endIconMode="password_toggle"
android:hint="Password">
<com.google.android.material.textfield.TextInputLayout
android:inputType="textPassword"
.../>
</com.google.android.material.textfield.TextInputLayout>
Hello all I am new to android themes and styles. According to official documentations
A style is a collection of attributes that specify the appearance for a single View . A style can specify attributes such as font color, font size, background color, and much more. A theme is a type of style that's applied to an entire app, activity, or view hierarchy—not just an individual view
But i can not change some attributes via android:theme attribute, but i can change via style
Here I have this style file:
<style name="Style.InputLayout" parent="Widget.Design.TextInputLayout">
<item name="errorTextAppearance">#style/ErrorTextAppearance</item>
<item name="hintTextAppearance">#style/HintTextAppearance</item>
<item name="errorEnabled">true</item>
</style>
<style name="HintTextAppearance" parent="TextAppearance.Design.Hint">
<!-- Inactive and Active label color-->
<item name="android:textColor">?attr/colorShadow</item>
</style>
<style name="ErrorTextAppearance" parent="TextAppearance.Design.Error">
<!-- Error text color-->
<item name="android:textColor">?attr/colorError</item>
</style>
And I have a TextInputLayout as:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tfUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/Style.InputLayout"
android:hint="Username">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.EditText"/>
</com.google.android.material.textfield.TextInputLayout>
This works as i expected. But when I change style attribute to android:theme in TextInputLayout like below:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tfUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Style.InputLayout"
android:hint="Username">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.EditText"/>
</com.google.android.material.textfield.TextInputLayout>
It does not work. Why is that?
It does not work. Why is that?
<com.google.android.material.textfield.TextInputLayout
android:theme="#style/Style.InputLayout"
It doesn't work because it is wrong.
You can use something like:
<com.google.android.material.textfield.TextInputLayout
android:theme="#style/Style.MyColor"
...>
With:
<style name="MyColor" parent="">
<item name="colorPrimary">#color/.....</item>
</style>
In this way you can modify the theme attributes for that view and any child views, which is useful for overriding theme color palettes in a specific portion of your interface.
You can find more info in the official doc.
Use like this:
Use this code on styles
<style name="MyColor" parent="">
<item name="android:colorControlActivated">#color/colorTextGray</item>
<itemname="android:colorControlHighlight">#color/colorTextGray</item>
<item name="android:colorControlNormal">#color/colorTextGray</item>
</style>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/layoutTextInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:theme="#style/MyColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline131"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline121"
app:layout_constraintTop_toBottomOf="#+id/ed_phone"
app:layout_constraintVertical_bias="0.0"
app:passwordToggleDrawable="#drawable/show_password_selector"
app:passwordToggleEnabled="true"
app:passwordToggleTint="#color/colorWhite">
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/ed_password_mobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:inputType="textPassword"
android:textColor="#color/colorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline131"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="#+id/guideline121"
app:layout_constraintTop_toBottomOf="#+id/ed_phone"
app:layout_constraintVertical_bias="0.0" />
</com.google.android.material.textfield.TextInputLayout>
In Android's widget TextInputLayout the style of active error and hint is identical, like this
But when error is on, I want to see different color of hint (black, not red). How can I achieve this?
P.S. I've already tried to apply style programmatically with method .setHintTextAppearance(R.style.StyleName) but it didn't bring any effect
The code of TextInputLayout
<android.support.design.widget.TextInputLayout
android:id="#+id/editTextClient"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColorHint="#color/colorPrimaryText"
app:errorEnabled="true"
app:hintEnabled="true"
android:hint="#string/login"
app:errorTextAppearance="#style/ErrorAppearence"
app:hintTextAppearance="#style/TextLabel"
app:helperTextTextAppearance="#style/TextAppearance.AppCompat.Body2">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:textSize="14sp"
android:singleLine="true"
android:textColor="#color/colorPrimaryText"/>
</android.support.design.widget.TextInputLayout>
Styles
<style name="TextLabel" parent="TextAppearance.Design.Hint">
<item name="android:textColor">#color/colorPrimaryText</item>
<item name="android:textSize">12sp</item>
</style>
<style name="ErrorAppearence">
<item name="android:textColor">#color/colorAccent</item>
<item name="android:textSize">12sp</item>
</style>
I tried to change TextInputLayout by setting custom style for its edittext as you see below:
layout:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textSize="15sp"
android:theme="#style/MyEditTextTheme" />
</android.support.design.widget.TextInputLayout>
style.xml:
<style name="MyEditTextTheme">
<item name="colorControlNormal">#color/colorPrimary</item>
<item name="colorControlActivated">#color/colorPrimary</item>
<item name="colorControlHighlight">#color/colorPrimary</item>
</style>
I faced 2 problems:
1.It works fine with activities but in fragments it has no change and the colors are as colorAccent.
2.Even in activities, after setting .setErrorEnabled(false); the edittext bottom color changes to colorAccent.
What is wrong? Is it a bug in android?
This is my xml
<android.support.design.widget.TextInputLayout
style="#style/main_input_text"
android:layout_marginTop="#dimen/activity_medium_margin"
app:hintTextAppearance="#style/TextAppearence.App.TextInputLayout">
<android.support.v7.widget.AppCompatEditText
style="#style/main_edit_text"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
And this is style
<style name="main_input_text">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">#dimen/activity_edittext_height</item>
<item name="android:background">#drawable/mc_shape_border_button_transparent</item>
<item name="android:gravity">center_vertical</item>
<item name="android:paddingTop">#dimen/activity_extra_small_margin</item>
<item name="android:paddingBottom">#dimen/activity_extra_small_margin</item>
<item name="android:keyTextSize">8sp</item>
<item name="android:textSize">8sp</item>
</style>
I don't find something like hintSize, textSize and keyTextSize not helping
In your styles.xml
<style name="TextLabel" parent="TextAppearance.Design.Hint">
<item name="android:textSize">16sp</item>
</style>
And then in your layout file:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="12dp"
app:hintTextAppearance="#style/TextLabel"
android:minHeight="30dp">
Change the TextInputLayout's app:errorTextAppearance in XML.
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:errorTextAppearance="#style/CustomTextInputLayoutStyle.ErrorTextStyle">
Where CustomTextInputLayoutStyle.ErrorTextStyle is defined in styles.xml as
<style name="CustomTextInputLayoutStyle.ErrorTextStyle" parent="TextAppearance.Design.Error">
<item name="android:textSize">10sp</item>
</style>
#Santiago Martínez answer only works for hints on non empty Edittext (inside TextInputLayout)
But for empty Edittext, TextInputLayout hint takes property from Edittext textSize.
1) Add hintTextAppearance to your InputLayout with a style
<android.support.design.widget.TextInputLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#style/TextInputLayoutHintText" >
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
</android.support.design.widget.TextInputLayout>
2) Then just add the style to your styles.xml
<style name="TextInputLayoutHintText">
<item name="android:textColor">#color/gray</item>
<item name="android:textSize">12sp</item></style>
For material 1.4.0
implementation 'com.google.android.material:material:1.4.0'
You only need to change the android:textSize attribute for the TextInputEditText for the hint size to change.
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</com.google.android.material.textfield.TextInputLayout>
You can reduce the font size on the EditText - that will reduce the size of the hint as well. i.e. android:textSize="16sp"