How to add style to a NumberPicker in Android - android

I am trying to add a custom style to an Android NumberPicker. What I am trying to do is have the text displayed by the picker as white since by default (or according to the default theme on the device) I have it in black. The problem is that my app background is a dark color so I want the text to be white or something clear.
so I have something like this in my style.xml file :
<style name="myPicker">
<item name="android:textColor">#FFFFFF</item>
</style>
the textcolor attribute doesn't even exist on the NumberPicker widget but I just tried to add it so that my text color changes. It does not work of course. The only thing that can be customized on this widget seems to be the background
Now the question : how can we change the text color of the picker ?
if somebody has the same issue or has solved it then please let us know.

I have found an "ugly" way to do it. Just get a reference to the child views of the number picker widget. There are three (3) child views : the upper arrow, the textview (containing the text or value to be displayed) and the lower arrow.
Let's say we have a Number picker called np
np.setValue(10);
np.setMinValue(5);
np.setMaxValue(50);
// retrieve the textview reference
TextView npTextView = (TextView) np.giftAmount.getChildAt(1); // since indexing begins at 0
npTextView.setTextColor(getResources().getColor(
R.color.my_custom_color)
I know that's bad but it works ...

Related

How to change hint text color of edittext when TextInputLayout is used

I have a screen with edittexts. When these edittexts are disabled, I need the hint color to be in one color
When these are enabled, I need the hint to be in a different color like this
This works fine when I tried like this
editText.setHintTextColor(color);
The problem is , I have to use a TextInputlayout to show the floating hint. So, When I add the EditText inside TextInputLayout this code doesn't work at all. I tried many things with TextInputLayout as shown here. But nothing works. All I need is the hint text color (Not floating hint text) of the EditText should be in one color when enabled , and in different color when disabled, when used inside a TextInputLayout. Please help!
Note: My question is not a duplicate of this question. There , it is mentioned about the floating hint text of the TextInputLayout when it is focused What I am talking about the normal hint text when the edit text is not focused and also when it is empty.
Add these to your App theme
<item name="colorControlNormal">#color/color</item>
<item name="colorControlActivated">#color/color</item>
<item name="colorControlHighlight">#color/color</item>
<item name="android:textColorHighlight">#color/color</item>

Is there any way to change a style attribute at run time in android

I want to change the textsize inside a style at run time. Can anyone suggest me how to achieve it. Here is the style "TitleView" in which textsize attribute is defined. I want to change it from 20sp to any other value at run time.
<style name="TitleView">
<item name="android:textSize">20sp</item>
</style>
I want to do it because I've 4 types of textview with different textsize. and In my app user can choose the textsize. So I've changed the text size of other view in relative to the user entered textsize.
Thanks in advance.
You cannot change style attribute at run time.
If you are just trying to change the text size just do:
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
this will change your text size and you can do this at any time you want. It is not necessary to change your style attribute. For setting text size at runtime see this
Edit: The poster has altered the original question. This answer is no longer directly applicable.
This cannot be done. The styles along with all the other resource values are all compiled into R.java at compile time.
Instead you could you create two styles and switch the style at runtime using setTextAppearance

Glass GDK:Change Color of Static/ Immersion Cards

I am trying to change the color of the text on a static/immersion card related to medical alerts. Such that an allergy is shown in red and bold colors and is easily noticeable.
Is there a way to do this in Glass GDK as the card.SetText() method just takes a String as its parameter and the card is given a layout before.
The GDK Card class does not have functionality to alter the color of text, reference here: https://developers.google.com/glass/develop/gdk/reference/com/google/android/glass/app/Card
However, you can alter text color in your style. Add a line in res/values/styles.xml within your <style> object
<item name="android:textColor">#00FF00</item>
More information on styles and themes here: http://developer.android.com/guide/topics/ui/themes.html
For those who need a solution to this
Make your modifications to the textview in the layout and then set that text to the card as-
card.setText((TextView).getText());

Android: Issue with showing dialog from Theme.Light activity

I am trying To show a dialog from a PreferenceActivity, which is set to Theme.Light. The dialog shows with dark text on a dark background:
I assume it uses dark text because it is inheriting the text color from the parent activity, or something similar. I would like the dialog to either use white text on the dark background, or use a white background with dark text, as the PreferenceActivity does when set to Theme.Light.
This seems to be a known problem, the workarounds I have found involve creating and using a custom style that extends Theme.Dialog and using it to instantiate the dialog. Something like:
<style name="CustomDialog" parent="#android:style/Theme.Dialog">
<item name="android:textColor">?android:attr/textColorPrimaryInverseDisableOnly</item>
</style>
Dialog dialog = new Dialog(context, R.style.CustomDialog);
I tried this, but it made no difference. I also tried a number of different values for textColor, none of which modified the Dialog's text color. As a sanity check, I added:
<item name="android:background">#FFFF0000</item>
to the style, which resulted in a dialog with a red background (so I am sure that I am instantiating the dialog properly).
The closest I have come to a solution is just setting the dialog's background color to white, which gives the below dialog. But this is not a good solution, because some version or some device might not use the same behavior I am seeing when inverting text color:
So, is there a good way to set text color on a dialog displayed from a Theme.Light activity?
I assume that you use AlertDialog.Builder and set the list using one of the setSingleChoiceItems methods which doesn't use your own ListAdapter. Instead it creates its own instead with the wrong style. To fix this, you should call setSingleChoiceItems(ListAdapter adapter, int checkedItem, DialogInterface.OnClickListener listener) and provide such an adapter which would use a layout with the needed style.
Now, why this happens. The actual adapter creation happens in the file com.android.internal.app.AlertController, where the following line selects the layout for single choice lists:
int layout = mIsSingleChoice
? R.layout.select_dialog_singlechoice : R.layout.select_dialog_item;
Here is the aforementioned layout:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/primary_text_light_disable_only"
android:gravity="center_vertical"
android:paddingLeft="12dip"
android:paddingRight="7dip"
android:checkMark="#android:drawable/btn_radio"
android:ellipsize="marquee"
/>
As you can see, the line which sets the text color contains not a reference to a theme, but a hardwired color. That's why when this thing gets inflated during the list creation, it will always use the same color, regardless of what style you want it to use. So the right action to overcome this problem is to use your own layout and your own ListAdapter.

what style/attr/drawable for inverse background?

I try to make a ExpandableListView where the group headers are drawn inverse. There is no problem with changing the text color, size etc. via XML. I even found out how to use the system defaults like
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
style="#android:style/TextAppearance.Medium.Inverse"
/>
But for the background color?! I don't understand why these styles don't include background color information?!
Of course I could use a direct color, but I look for good default background attributes or styles. Like "style/Background.For.TextAppearance.Medium.Inverse" ;-)
What would be a good solution? So that for the dark themed devices I get white/gray, and for the white themed I get black?
Or should I simply use R.color.background_light?
Greetings, Joerg
PS: First question here ;-) Thanx to all the people answering here the last months and years: You great people made it much more easier for me to find a re-entrance in programming after 12 years break ;-)
As you observe, the styles with "TextAppearance" in their name only affect the foreground text attributes of the view. They are appropriate for the android:textAppearance attribute. The styles with "Widget" in their names define all the UI properties and will work in a style attribute, but Android doesn't define a "Widget.TextView.Inverse" style.
When I wanted to display an console-like log as an inverse text view, I used the following XML:
<TextView
android:textAppearance="#style/TextAppearance.AppCompat.Small.Inverse"
android:background="?android:colorForeground"
... />
It uses the theme's foreground color as the background for the view. With a dark theme it displays dark text on white, and in a light theme it displays light text on black.

Categories

Resources