So I am trying to change my app color to blue and some of most the views I have are not willing to cooperate with me.
Here is the image:
Here I want to change the color of the green parts on the spinner, edittext and checkbox views (which are green) to black or blue.
I've looked all over Stack Overflow and I can't find the solution!
Thank you very much, If possible I would like to have a XML solution but I wouldn't mind a programmatic solution!
Add these to your base theme in styles.xml
<item name="colorControlNormal">#color/YOUR_COLOR</item>
<item name="colorControlActivated">#color/YOUR_COLOR</item>
<item name="colorControlHighlight">#color/YOUR_COLOR</item>
Note: The above change will affect the EditTexts and other views probably throughout the application.
If not, and if you are using the AppCompat v22 support library, you can specify the theme in the EditText like: android:theme="#style/Theme.App.Base.
This will ensure the style won't also affect other views in your layouts that you don't want to change
Also if you want to change the above solution, just add another Theme specific to EditTexts and Spinners and apply it to all Spinners if you want
<style name="MyWidgetTheme">
<item name="colorControlNormal">#color/YOUR_COLOR</item>
<item name="colorControlActivated">#color/YOUR_COLOR</item>
<item name="colorControlHighlight">#color/YOUR_COLOR</item>
</style>
and in your EditText, Spinner or any other View, just assign this theme:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Demo"
android:lines="1"
android:theme="#style/MyWidgetTheme"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/MyWidgetTheme"></Spinner>
Take a look to this resource generator.
Choose your color, the widgets you want to generate and voila! You copy them to your project and reference them in your xmls.
Related
I'm new to android and I have been trying to find out an answer to this. I have a layout file .xml for listview that has two textviews, one is for heading with a greater size and other below is for description. What I want is that heading textview's text stays white and description textview's text stays grey in my current theme. That is dark theme. But user has the option to change the theme, so when user selects Light theme I want my heading textview's text (that is within layout that I have for listview's rows) to become black and also the description textview's text to become black.
Please help and thanks in advance.
you can set the text color programmatically using the method textView.setTextColor(int color), or via XML using the attribute android:textColor="#yourhexcolor"
You can change theme programmatically. There is one guy asked this hours ago. Can you check this: how-to-change-colour-when-user-wants-another-colour-in-the-app
Also if you want to use diferent themes for different TextViews you can try like :
textView.setTextAppearance(context, android.R.style.TextAppearance_Small);
How can i create custom themes for TextViews
in your styles.xml create different styles :
<style name="MyBlueTextTheme" parent="#android:style/TextAppearance.Medium">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#123456</item>
<item name="android:textStyle">bold</item>
</style>
Then use it like :
<TextView
android:id="#+id/textBlue"
android:text="This is a blue styled text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="#style/MyBlueTextTheme" />
Or use it like: textView.setTextAppearance(context,R.style.MyBlueTextTheme);
but its deprecated and you can use textView.setTextAppearance(R.style.MyBlueTextTheme); but this can be used Api23 or higher
i am trying to modify the underline color of a EditText by applying a theme.
Style:
<style name="MyTheme.EditText" parent="Widget.AppCompat.EditText">
<item name="colorControlActivated">#color/green</item>
</style>
EditText:
<EditText
android:id="#+id/editText_amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_enter_amount"
android:theme="#style/MyTheme.EditText"/>
Basically it works, but when i try to select or move the cursor the selection handle is also underlined. You can see this in the screenshot.
Does someone know how to fix this?
You can use this style as a
<EditText
style="#style/MyTheme.EditText"/>
Or, you can separate your theme for referencing the editTextStyle attribute.
<style name="MyEditTextStyle" parent="Widget.AppCompat.EditText">
<item name="colorControlActivated">#color/green</item>
</style>
<style name="MyTheme.EditText">
<item name="editTextStyle">#style/MyEditTextStyle</item>
</style>
<EditText
android:theme="#style/MyTheme.EditText"/>
Alright, but where are these underlines come from?
android:theme is an attribute of View and when you set a style as android:theme, that style will be wrapped by ContextThemeWrapper with context theme while in inflation of view.
So that means, if you set android:theme property with style that contains android:background item like
<item name="android:background">#color/green</item>
every child view of this theme owner will be have a green background.
"Widget.AppCompat.EditText" is a style and references ?attr/editTextBackground as a "android:background". And in v21/values-21.xml file #drawable/abc_edit_text_material is defined as editTextBackground.
So, for your example, #drawable/abc_edit_text_material becomes a background of your EditText and SelectionHandlers.
You should remove parent attribute. This is caused only API 23 device.
<style name="MyTheme.EditText">
<item name="colorControlActivated">#color/green</item>
</style>
I had the same issue.
My layout shows a Edittext inside a blue box. There i wanted to have a white text and a white underline and a white selection control.
I've set all in my AppTheme. All fine!
But i also had a Recyclerview blow the blue box. The RecyclerView has white cards that contains Edittext. White text, underline and control makes no sense in white cards. :)
So i tried to change the color of the text, underline and controls to dark gray. But with no success. I had the same issues with the control like in this question.
I tried using the solved answer, but this didn't helped me.
Maybe my fault, don't know.
So i post here my solution, even the question already answered.
Inside the styles.xml i added:
<style name="RecyclerEditText">
<item name="colorAccent">#color/darkGray</item>
<item name="colorControlNormal">#color/darkGray</item>
<item name="colorControlActivated">#color/darkGray</item>
</style>
and in the layout xml i added inside edittext:
android:textColor="#color/darkGray"
android:textColorHint="#color/darkGray"
android:theme="#style/RecyclerEditText"
--
Now i have no offset in control icon, and no double underline.
Thanks a lot #litmon !
You pushed me to the "remove parent" idea.
I have a quick question!
In my styles.xml file, I have
<style name="TextViewStyle" parent="android:Widget.TextView">
<item name="android:padding">20px</item>
<item name="android:background">#9cd0e8</item>
<item name="android:textColor">#254b7c</item>
<item name="android:textSize">18sp</item>
<item name="android:textStyle">bold</item>
</style>
And in my activity_main.xml, I have
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:theme="#+styles/TextViewStyle"
android:text="Sample Text"/>
What I am trying to do is, in my Android application, on a certain activity I plan to place many TextViews with similar properties. Instead of writing these 'properties' every time with each TextView instance, I grouped them together in a style in styles.xml file and set theme of each of my TextViews to that style.
It works fine and does what I want it to do, but only with APIs above 21! My application's supposed to support devices from API level 15 up. Why is my approach not working with lower APIs?
Please help soon. I need to finish this soon.
EDIT
By 'working', I meant that the attributes I set in my style (padding, color, etc.) appear on the TextViews as they should. In lower APIs however, the TextViews appear as if I had not applied any attribute on them. Plain text appears instead of a styled one.
remove parent from your style
remove android:theme from textView, (why there is + sign?)
instead of theme put this into your textView
style="#style/TextViewStyle"
btw, use dp instead of px ;)
I can able to set theme to button using android:theme attribute in xml. Is there any equivalent programmatical code?
<Button
android:id="#+id/btnPayment"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Add Payment"
android:theme="#style/ButtonPeter" // theme..
/>
style.xml
<style name="ButtonPeter" parent="Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/fbutton_color_peter_river</item>
<item name="android:textColor">#android:color/white</item>
</style>
Can I able to set theme dynamically using something like this btnPayment.setTheme(R.style.ButtonPeter) ?
I search a lot but all posts are related to set style or create dynamic button and apply style to it but I dont want to do that .I want to set theme to button
How to achieve this?
I think you are confused with style and theme. From this document, it says: theme for the whole activity while style is for view.
A style is a collection of properties that specify the look and format for a View or window.
A theme is a style applied to an entire Activity or application, rather than an individual View.
For this code:
android:theme="#style/ButtonPeter" // theme is used but only valid style attributes for button will be applied
Hope this help.
As I mention here, using TextViewCompat.setTextAppearance should work :-)
I have one EditText field that I want to stylize as a Spinner, and Im using the support library to Android Lollipop. However, I'm not able to give the correct color to the dropdown selector (the same that is used on the default spinner) .
The example can be seen here: http://imgur.com/g0ia41h
I want a darker selector, but I'm only able to get a white selector as it can be seen in the image (the first one is my editText as a spinner, with the white selector, and the second one is the real spinner, with the darker and corrector selector)
How can I change that color?
The style of the activity is this one:
<style name="SettingsTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textSize">#dimen/text_size_medium</item>
<item name="colorPrimary">#color/h19_black</item>
<item name="colorPrimaryDark">#color/white</item>
</style>
The style of my EditText is the following:
<com.devspark.robototextview.widget.RobotoEditText
android:id="#+id/et_birth_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="date"
android:focusable="false"
style="?android:attr/spinnerStyle" />
Any ideas? I've been struggling for a while with this. I have no more ideas.
Thank you!!
Aha!
The AppCompat library doesn't seem to have a black version of the arrow, so what you have to do is take the white version and change the colour manually. The file name is abc_spinner_mtrl_am_alpha.9.png and you can find it here. There's one in each drawable-* folder (10 of them).
Then just add this attribute to your EditText:
android:background="#drawable/spinner_arrow_black"
You can also do this programmatically, maintaining you spinner style and without playing with the drawable files:
EditText editText = (EditText) findViewById(R.id.edit_text);
editText.getBackground().setColorFilter(Color.BLACK, PorterDuff.Mode.MULTIPLY);