TextInputLayout change color on button click - java

I am having trouble changing the color of my TextInputLayout underline. Does anyone know how to change it 'On Click? it goes like this like..
onClick
if ( is true ) {
change textinputlayout underline color into red
} else {
non
}

Try to create a new style like this in your styles.xml TextAppearence.App.TextInputLayout, you then need to add colorControlActivated and colorControlHighlight.
<style name="TextAppearence.App.TextInputLayout"
parent="#android:style/TextAppearance">
<item name="android:textColor">#color/black</item>
<item name="android:textSize">#dimen/_13sdp</item>
<item name="colorControlNormal">#color/black</item>
//added attributes
<item name="colorControlActivated">#color/colorPrimary</item>
<item name="colorControlHighlight">#color/colorPrimary</item>
</style>
and change your color accordingly and use it here in styles.

You can do like this.
android:textColorHint="#FF0000" was EditText's hint color.
You can change android:textColor="#FF0000" in the EditText as text color.
You can add app:errorTextAppearance="#style/text_in_layout_error_hint_Style" to change the error text color.
And like this .
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColorHint="#FF0000"
app:errorTextAppearance="#style/text_in_layout_error_hint_Style"
app:hintTextAppearance="#style/text_in_layout_hint_Style"
app:errorEnabled="true">
<EditText
android:id="#+id/editEmail"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="-4dp"
android:digits="abcdefghijklmnopqrstuvwxyz.#0123456789"
android:hint="email / snipe id"
android:textAllCaps="false"
android:textColor="#FF0000"
android:textColorHint="#color/primary"
android:textSize="12sp"/>
</android.support.design.widget.TextInputLayout>
The LEFT-TOP hint color depended on app:hintTextAppearance="#style/text_in_layout_hint_Style".
And the style code:
<style name="text_in_layout_hint_Style">
<item name="android:textColor">#FF0000</item>
<item name="android:textSize">12sp</item>
</style>
Add error text style
<style name="text_in_layout_error_hint_Style">
<item name="android:textColor">#00ff00</item>
<item name="android:textSize">12sp</item>
</style>
like this:

Related

While using TextInputLayout , how can the hint color be same while floating or while getting error ? Not same as the error color

XML
<com.google.android.material.textfield.TextInputLayout
style="#style/TextInputLayout"
android:id="#+id/abc_TIL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/abc_TV"
style="#style/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789"
android:hint="abc*"
android:maxLength="11"
android:inputType="number"
android:maxLines="1"
android:textSize="#dimen/_12sdp" />
</com.google.android.material.textfield.TextInputLayout>
style
<style name="EditText" parent="Widget.TextView">
<item name="android:textSize">#dimen/_13sdp</item>
<item name="android:textColor">#color/Black</item>
<item name="hintTextColor">#color/dark_grey</item>
<item name="colorControlNormal">#color/dark_grey</item>
<item name="colorControlActivated">#color/dark_grey</item>
<item name="colorControlHighlight">#color/dark_grey</item>
<item name="android:background">#drawable/background_grey</item>
</style>
<style name="TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<item name="android:layout_marginLeft">#dimen/_10sdp</item>
<item name="android:layout_marginRight">#dimen/_10sdp</item>
<item name="android:layout_marginTop">#dimen/_6sdp</item>
<item name="hintTextColor">#color/dark_grey</item>
<item name="android:textColorHint">#color/dark_grey</item>
<item name="colorControlNormal">#color/dark_grey</item>
<item name="colorControlActivated">#color/dark_grey</item>
<item name="colorControlHighlight">#color/dark_grey</item>
<item name="errorIconDrawable">#null</item>
**Also tried app:hintTextApperance , app:errorTextApperance but not getting what i am expecting . i want hint color & error color to be different during error .Hint color should not be red when error occurs , it should be gray & error text should be red **
Edit: Set the hint on the object, which is inside your TextInputLayout (EditText, TextView, etc.) to hold different colors for the hint and the error.
Visit link.

Apply theme to view's background color

So I have an ImageButton and I could just change its background colour to white, but then it would be also white while using the dark theme. I tried to use the android:theme and set it to mine, but it made the button's background colour set back to the default, or changed the whole app's background to that colour.
ImageButton:
<ImageButton
android:id="#+id/backbut"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_margin="5dp"
android:layout_weight="0"
android:theme="#style/Theme.MusicPlayer"
android:contentDescription="#string/backbut_desc"
android:elevation="4dp"
android:padding="8dp"
android:scaleType="fitCenter"
app:srcCompat="#drawable/ic_arrow_left" />
themes.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.MusicPlayer" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">#color/blue_1</item>
<item name="colorPrimaryVariant">#color/blue_1</item>
<item name="colorOnPrimary">#color/white</item>
<item name="colorPrimaryDark">#color/blue_2</item>
<item name="colorAccent">#color/blue_1</item>
<item name="android:textColorPrimary">#color/black</item>
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryDark</item>
<item name="android:navigationBarColor">#color/white</item>
</style>
Any tips on what I should do?
Here is how I handle such cases:
Create an attribute in attrs.xml (in values folder, if there isn't one, create it yourself) as:
<resources>
<attr name="myBackgroundColor" format="color" />
</resources>
There are 2 themes.xml if you are implementing the dark mode with the help of day-night theme. So in the themes.xml file which is located in values folder:
<item name="myBackgroundColor">#FFFFFF</item>
In the other themes.xml file which is located at values-night folder:
<item name="myBackgroundColor">#000000</item>
When you want the view to change it's background color accordingly with the phone theme:
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?myBackgroundColor"/>
That's it, android will handle the rest and pick the corresponding color you provided from night themes xml if the dark mode is enabled by the user.

Java Android, Default textview color is grey, looks like hint text color, how to correct it?

Text color is grey as shown below, i have not set any default color in sytles.xml.
As you can see there is a difference between the spinner text color and the textview color.
color difference between Spinner and textview text
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView color"
android:textAlignment="gravity"
android:textAllCaps="false"
android:textSize="16dp"
android:textStyle="bold"
android:gravity="left|center"
android:layout_marginLeft="20dp"
android:padding="12dp" />
<Spinner
android:layout_width="192dp"
android:layout_height="49dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:entries="#array/spinner_items"
android:padding="10dp"
android:spinnerMode="dropdown"
/>
I ran the ColorStateList oldColors = textView.getTextColors(); i got the below result.
ColorStateList{mThemeAttrs=nullmChangingConfigurations=0mStateSpecs=[[-16842910], []]mColors=[603979776, -1979711488]mDefaultColor=-1979711488}
Is the text color is due to this style, my Styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
All i want is regular black for texts. And i don't want to add change anything.
Add color property in your TextView
<TextView
          android:color="#000000"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="TextView color"
          android:textAlignment="gravity"
          android:textAllCaps="false"
          android:textSize="16dp"
          android:textStyle="bold"
          android:gravity="left|center"
          android:layout_marginLeft="20dp"
          android:padding="12dp" />
Looks like that is the default color of text. So i have to force it to use regular black in Styles.xml
<item name="android:textColor">#color/textColorPrimaryLight</item>
The reason i was looking for is i want to implement Dark mode. Below article helped my issue. Basically i have to use 2 styles.xml to implement dark mode.
How to officially support Dark Mode in your apps?

Can't change cursor and bubble color for Material TextInputEditText

I want to change Material TextInputEditText's bubble and cursor color. I tried colorAccent, android:textCursorDrawable these are not working correctly.
Files in here
The material attribute color colorControlActivated do the magic. You have to create a style for your TextInputLayout.
e.g:
<style name="TextInputLayoutAppearance" parent="Widget.Design.TextInputLayout">
<item name="colorControlNormal">#color/white</item>
<item name="colorControlActivated">#color/red</item>
<item name="colorControlHighlight">#color/blue</item>
</style>
Then you have to apply this style in the theme attribute of control:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/lblObservaciones"
android:theme="#style/TextInputLayoutAppearance"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:boxStrokeColor="#color/blue"
app:hintTextAppearance="#style/TextAppearance.AppCompat.Medium"
app:hintTextColor="#color/blue">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/comments"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="top"
android:inputType="textMultiLine"
android:maxLength="200" />
</com.google.android.material.textfield.TextInputLayout>
Just apply colorControlActivated in app theme(day/night) and all Material TextInputEditField will have the changed cursor color
<item name="android:colorControlActivated">#color/green</item>
You have to use following attributes:
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<item name="colorPrimary">#212121</item>
<item name="colorPrimaryVariant">#000000</item>
<item name="colorOnPrimary">#FFFFFF</item>
<item name="colorSecondary">#2962FF</item>
<item name="colorSecondaryVariant">#0039CB</item>
<item name="colorOnSecondary">#FFFFFF</item>
<item name="colorError">#F44336</item>
<item name="colorOnError">#FFFFFF</item>
<item name="colorSurface">#FFFFFF</item>
<item name="colorOnSurface">#212121</item>
<item name="android:colorBackground">#color/background</item>
<item name="colorOnBackground">#212121</item>
</style>
<color name="background">#FAFAFA</color>
To know more about : Setting up a Material Components theme for Android

Making TextView bold and adding Text Designs

Please have a look at the following code
<RelativeLayout 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"
android:descendantFocusability="afterDescendants"
android:background="#drawable/background1"
tools:context=".Form" >
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/dateTxt"
android:textSize="32sp"
android:textColor="#FFFFFF"
android:layout_alignParentLeft="true"
/>
<DatePicker
android:id="#+id/datePick"
android:layout_toRightOf="#+id/txt1"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
/>
</RelativeLayout>
In here, I need to make the text of TextView Bold, and if possible, add a text design something like "spread", "spoiled", "blur".
How can I do this? Making the text bold is the main question, if you can please answer to the second question as well.
Thanks
TextView t1=(TextView)findViewById(R.layout.TextView01);
t1.setTypeface(null,Typeface.BOLD);
For design get some idea from it..
http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-basic-font-sizes/
To do it via XML I recommend you to create a style. If you don't have any, add a XML file to your 'values' folder named "styles.xml" and add this style to it (I used shadows to try to make some blurry text, but it may not be very effective), anyway you have the bold text as you wanted:
<resources>
<style name="Text"> </style>
<style name="Text.Strong">
<item name="#android:textSize">32sp</item>
<item name="#android:paddingLeft">4dp</item>
<item name="#android:paddingBottom">4dp</item>
<item name="#android:textColor">#FFFFFFFF</item>
<item name="#android:textStyle">bold</item>
</style>
<style name="Text.Strong.Blurry">
<item name="#android:shadowColor">#BBBBBB</item>
<item name="#android:shadowDx">1</item>
<item name="#android:shadowDy">1</item>
<item name="#android:shadowRadius">10</item>
</style>
</resources>
Then just apply the style to your TextView:
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/dateTxt"
android:layout_alignParentLeft="true"
style="#style/Text.Strong.Blurry" />
Modify Your dateTxt String. Use bold tag.
<string name="dateTxt"><b> Copyright </b></string>
And for blurry or spoil text , you can make style and then apply on your textView. For doing text blur use this
<style name="Theme.BlurText" parent="android:style/Theme.Translucent">
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:background">#android:color/transparent</item>
</style>
And in your textview use
android:style="#style/Theme.BlurText"

Categories

Resources