I'm trying to style edit text widgets in my app. Right now if I use this:
<style name="MyEditText" parent="#android:style/Widget.EditText">
<item name="android:textColor">#color/input_text</item>
<item name="android:gravity">left</item>
</style>
It will show black background on phone I use for testing. I'd like to get something that looks the same everywhere. I think I will be OK with this style (I also believe this is "standard"). How do I pull this stale or should I make my own somehow?
EDIT:
Here is my code for TextEdit
<EditText
style="#style/MyEditText"
android:id="#+id/et_serverURL" android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxLength="25"
android:imeOptions="actionNext" android:singleLine="true" android:inputType="textUri"
android:hint="#string/str_login_activity_server_url_hint"
/>
The style shown in your image is called Holo. You can use it like so
<style name="MyEditText" parent="#android:style/Widget.Holo.EditText">
<!-- Your custom attributes here -->
</style>
EDIT
If you want your app to use your themes as you have them right now but have your EditText be Holo then you can override the <item name="android:editTextStyle"></item>under your current theme to use MyEditText instead of the default parent style. Then set your theme either to your activity, or to your application.
Related
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 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 a DialogFragment that contains a Button:
<Button
android:id="#+id/btn_wishlist_rename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:theme="#style/WishlistDialogButton"
android:text="#string/rename" />
The theme is defined like this:
<style name="WishlistDialogButton" parent="Widget.AppCompat.Button">
<item name="android:textColor">#color/white</item>
<item name="colorButtonNormal">#color/default_blue</item>
<item name="backgroundTint">#color/default_blue</item>
</style>
When I just embed the Fragment within the layout of my Activity everything works fine: I get a Material Design raised button with white text and blue background.
However, when the Fragment is used as a dialog the backgroundTint is not applied anymore: I get a Material Design raised button with white text but with grey background.
Is there a way to fix this - preferably without creating all the drawables from scratch or importing some third party library?
I have a dialog box that I create to display messages in android. It basically contains a text view in a scrollview like this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/about_msg"
android:text="#string/about_msg"
android:autoLink="web"
android:padding="10dip"
style="#style/DialogTextSmall" />
</ScrollView>
As you can see I have applied a style to TextView the style looks like this
<style name="DialogTextSmall">
<item name="android:textSize">#dimen/text_size_small</item>
</style>
The application theme set is like this
<style name="AppTheme" parent="android:Theme.Light">
The Problem:
On ICS api-15 it shows fine black text on white background of TextView.
The problem is When I show dialogbox in Froyo its the text does'nt seem to show even though it seems to have taken space - My guess is the color of text is same as background (greyish black)
I know I can quick fix by hard-coding black background and white text, but Is it not possible to have the default colors of platform for the text color and background of the TextView to appear, without me having to hardcode them ?
You can inherit a parent style and then only change the values you want to change. Try changing your XML to this:
<style name="DialogTextSmall" parent="#android:style/Widget.TextView>
<item name="android:textSize">#dimen/text_size_small</item>
</style>
The list of styles you can inherit can be found in the AOSP source on Github here.
EIDT:
By default text views have black text and transparent background, so you will need to set one or the other if the background behind the text view (which, again, is transparent) is black.
Inheritance from Textview style did not really help. It is a little quirky problem and here is one way to do it
http://blog.andromo.com/2011/fixing-text-colours-on-an-alertdialog-when-using-theme-light/
In my case I did it another way
Solved it for theme I inherited it from default android theme
<style name="Theme" parent="android:Theme"></style>
<style name="Theme.AppTheme" parent="#android:style/Theme.Light"/>
and
<style name="DialogTextSmall">
<item name="android:textSize">#dimen/text_size_small</item>
<item name="android:textColor">#android:color/white</item>
</style>
This way for all platforms , froyo, gingerbread and above, the dialog boxes are black and text is white on them
I want to extend Android's small button style. I can do it inline:
<Button android:id="#+id/myButton"
style="?android:attr/buttonStyleSmall"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="10dp"
android:text="Click Here"/>
But in the interest of reusability, I want to transfer these styles to a custom style. How can I add buttonStyleSmall (or Widget.Button.Small?) as a parent to a style? Something like this in my custom style XML:
<style name="RightLink" parent="?android:attr/buttonStyleSmall">
<item name="android:layout_alignParentRight">true</item>
<item name="android:layout_gravity">right</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:paddingLeft">2dp</item>
<item name="android:paddingRight">2dp</item>
<item name="android:textSize">10dp</item>
</style>
with the button declaration using that style:
<Button android:id="#+id/myButton"
style="#style/RightLink"
android:text="Click Here"/>
EDIT
Using the correct syntax as described by Lukas below (use #android:attr/buttonStyleSmall as the parent), I'm still seeing a difference between the two:
Button with buttonStyleSmall as style and inline styles added:
Custom style with buttonStyleSmall as the parent:
What am I missing?
So you try to inherit style informations from a standard Android style. For this, you'll need to use the parent-attribute as you already did.
The only exception when inheriting a standard style is, that you have to use an # not a ?:
<style name="RightLink" parent="#android:attr/buttonStyleSmall">
A little more about how to do that for platform styles (because that differs from styles you created yourself) can be found here.
After playing around with it i found that the way you inhirate is correct, but the recourse is wrong:
<style name="RightLink" parent="#android:style/Widget.Button.Small">
This works.
The difference seams to be that the integer-constant which is used to add this by using the style-attribute and in code is declared in Androids R-class in the attr-subclass.
The XML-Style definitions (from which you can actually inherit) are stored in the style-subclass of the R-class. So the above line should solve your problem.