Setting spinner popup background color - android

I can set the background of the popup menu of a spinner using:
<Spinner
android:popupBackground="#color/darkThemeBackgroundColor"/>
But how do I set the background text color of it?

This solution is more simpler, you only need add this code to styles.xml
<style name="spinnerTheme">
<item name="android:background">#color/colorDark</item>
<item name="android:textColor">#android:color/white</item>
<!-- this is for api 19 text color -->
<item name="android:color">#android:color/white</item>
</style>
And this is how use in your layout file
<Spinner
android:id="#+id/spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:padding="8dp"
android:entries="#array/aves"
android:theme="#style/spinnerTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
That is the result:
I hope this solution helps you, greetings.

Related

How to change CounterTextAppearance

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

How to use default android themes?

Hy
I created an android project, and I saw thet, tehe are two themes.xml file. In emulator, when I switch day and night mode, my application color schema is changes too.
I thought it is greate, because with thease themes it will be easy to create a day/night app, but I don't know how it works.
How can I define a new color item inside themes, and use this schema for layout background?
For example: There is an item in both themes
<item name="colorPrimary">#303F9F</item>
And I would like to create a new item for both themes.xml, and use it to set up fragment background color, but the intellisense doesn't find this element directly.
<item name="backgroundColor">#303F9F</item>
You can change color easily from where you define your theme in 'styles.xml'.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- 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="AppThemeDark" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorSecondary</item>
<item name="colorPrimaryDark">#color/colorSecondaryDark</item>
<item name="colorAccent">#color/colorSecondaryAccent</item>
</style>
I have two themes in my app so if i change the color or anything in my theme i will define that color in my selected theme for example:
i want to change the background color of 'AppThemeDark' i just define the color like this
<item name="background">#color/"#fff67"</item>
copy this line in your first screen
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
Ok In constraintlayout background tag is not worked so I will fix this problem with the use of View.
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AAA">
<View
android:id="#+id/background"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#FFF"
app:layout_constraintBottom_toBottomOf="#+id/textView3"
app:layout_constraintEnd_toEndOf="#+id/textView1"
app:layout_constraintStart_toStartOf="#+id/textView1"
app:layout_constraintTop_toTopOf="#+id/textView1" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="#+id/textView1"
app:layout_constraintStart_toStartOf="#+id/textView1"
app:layout_constraintTop_toBottomOf="#+id/textView1"
tools:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="#+id/textView1"
app:layout_constraintStart_toStartOf="#+id/textView1"
app:layout_constraintTop_toBottomOf="#+id/textView2"
tools:text="TextView" />
</android.support.constraint.ConstraintLayout>

Android Change TextColor of selected NumberPicker

I have created a NumberPicker in Android Studio but I cannot change the color of the selected item. The only thing I have found in this forum is this way:
<style name="AppTheme.Picker" parent="Theme.AppCompat.Light.NoActionBar" >
<item name="android:textColorPrimary">#android:color/black</item>
</style>
but that changes the text color of all items.
My NumberPicker:
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="306px"
android:layout_height="300px"
android:selectionDividerHeight="0dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:descendantFocusability="blocksDescendants"
android:fontFamily="#font/questrial"
android:scaleX="2.5"
android:scaleY="2.5"
android:theme="#style/AppTheme.Picker"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.827"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.513" />
<style name="AppTheme.Picker" >
<item name="android:textSize">50px</item>
</style>
sample
In this case, only want to change the text color of 36.4

Changing the Color of a Spinner

I have a spinner that I want to change the color when it is not clicked. the view in the activity. I managed to change the color of text and background when it is clicked, but not the default text and background. and because the background of the app is dark it is difficult to see. How can I do this. this is my spinner code on activity_main.xml
<Spinner
android:id="#+id/spinner"
android:layout_width="114dp"
android:layout_height="33dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:theme="#style/Spinner"
app:layout_constraintBottom_toTopOf="#+id/ping_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintStart_toEndOf="#+id/editText"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.025" />
my style for spinner
<style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
<item name="android:paddingStart">0dp</item>
<item name="android:paddingEnd">0dp</item>
<item name="android:textColor">#color/textColor</item>
<item name="android:backgroundTint">#color/background</item>
<item name="android:textSize">14sp</item>
</style>
here's a screenshot of the app
http://pctechtips.org/apps/droid.png
Make a custom XML file for your spinner item.
spinner_item.xml:
Give your customized color and size to text in this file.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="left"
android:textColor="#FF0000"
android:padding="5dip"
/>
Now use this file to show your spinner items like:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);
You don't need to set the drop down resource. It will take spinner_item.xml only to show your items in spinner.
Reference from 'How to change spinner text size and text color?'

Use custom hint text color for hints in enabled and disabled views

I'm trying to add custom styles to a TextInputLayout and TextInputEditText and I´m not able to get the expected results.
What I need is to have a custom color "A" for hints in enabled views and a custom color "B" for hints in disabled views.
Right now I have a style with a selector for hints enabled/disabled. This is the style:
<style name="CustomTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">#color/input_blue</item>
<item name="boxStrokeWidth">2dp</item>
<item name="errorTextAppearance">#style/ErrorText</item>
<item name="android:textColorHint">#color/selector_edithintcolor</item>
</style>
Here is the selector, you can see the color to make the point:
Now some of the components in the layout:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/layout_text_disabled"
style="#style/CustomTextInputLayoutStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/layout_phone_not_focused">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/txt_text_disabled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_deactivated"
android:inputType="text"
android:text="#string/txt_disabled" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/layout_text_disabled_hint"
style="#style/CustomTextInputLayoutStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/layout_text_disabled">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/txt_text_disabled_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_deactivated"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
Additionally I have a button to change the status of those 2 views from enabled to disabled, here is the behavior:
When views are enabled everything is as expected, hints have the green color as per the selector:
Now when I disable the views, this is what I get:
Any ideas for making the disabled hints be orange as per the selector? Thanks in advance!
Following this link it appears that it is now possible with version 1.2.0-alpha03 of com.google.android.material:material
You need to create a color state list like so :
box_stroke_color_state_list.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?colorPrimary" android:state_enabled="true" />
<item android:color="?colorSecondary" android:state_hovered="true" />
<item android:color="?colorSecondary" android:state_focused="true" />
<!-- This is where the disable hint and box will take it's color -->
<item android:alpha="#dimen/material_emphasis_disabled" android:color="#android:color/holo_green_dark" android:state_enabled="false" />
<item android:color="#color/mtrl_textinput_default_box_stroke_color" />
</selector>
and then call it in your code :
ContextCompat.getColorStateList(context, R.color.box_stroke_color_state_list)?.let {
layout_text_disabled_hint.setBoxStrokeColorStateList(it)
}
Based on the #Caktuspace reply, you can apply in this way:
layout.xml
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/til"
...
android:hint="#string/hello"
android:textColorHint="#color/hint_text_color_textinputlayout">
hint_text_color_textinputlayout.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="#dimen/material_emphasis_disabled" android:color="#color/desiredColorDisabled" android:state_enabled="false" />
<item android:color="#color/desiredColorEnabled" />
</selector>

Categories

Resources