I'm new to Android and I'm implementing NumberPicker to one of my activities in my app. Below is the excerpt of my code:
picker = (NumberPicker)findViewById(R.id.order_confirm_bring_time_minute_picker);
picker.setMinValue(15);
picker.setMaxValue(120);
picker.setWrapSelectorWheel(false);
setNumberPickerTextColor(picker, android.R.color.black);
public boolean setNumberPickerTextColor(NumberPicker numberPicker, int color)
{
final int count = numberPicker.getChildCount();
for(int i = 0; i < count; i++){
View child = numberPicker.getChildAt(i);
if(child instanceof EditText){
try{
Field selectorWheelPaintField = numberPicker.getClass()
.getDeclaredField("mSelectorWheelPaint");
selectorWheelPaintField.setAccessible(true);
((Paint)selectorWheelPaintField.get(numberPicker)).setColor(color);
((EditText)child).setTextColor(color);
numberPicker.invalidate();
return true;
}
catch(NoSuchFieldException e){
Log.d("setNumberPickerTextColor", "NoSuchFieldException");
}
catch(IllegalAccessException e){
Log.d("setNumberPickerTextColor", "IllegalAccessException");
}
catch(IllegalArgumentException e){
Log.d("setNumberPickerTextColor", "IllegalArgumentException");
}
}
}
return false;
}
I've looked into this post for the setNumberPickerTextColor method. But it does not seem to work as I set my color to be changed to black, but it is not visible anymore. If I do not use the setNumberPickerTextColor method, then my default color is white, which can be seen when I highlight the text in the EditText field of the NumberPicker.
This is a screenshot of the NumberPicker when the color is not changed.
This is a screenshot of the NumberPicker when the color is changed to black or any other color (I have tested and they give the same result).
Would there be a way to customize the text color in my NumberPicker? Also, I know it is a different question, but the colors of the top and bottom 'bar' as well because they do not fit the color theme of my app. Thanks in advance for help.
You need to pass the resolved color to the setTextColor method, not the resource id.
((EditText)child).setTextColor(getResources().getColor(color));
Related
So I have a LinearLayout and 4 EditText-s in it with grayish hint colors in XML. And I have button that dynamically adds new EditText-s to LinearLayout. The problem is when I use setHint("text") it makes hint color for new created views black.
Also tried setHintTextColor() but the only way it worked for me by setting custom color. Is there a default hint color that I can set by setHintTextColor()?? or maybe some method that does it when it's called?
Code looks like this:
private EditText createNewTextView(String text) {
++x;
final ActionBar.LayoutParams lparams = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
final EditText editText = new EditText(this);
editText.setLayoutParams(lparams);
editText.setHint("Name" + x);
editText.setHintTextColor(getResources().getColor(R.color.hintcolor));
return editText;
}
p.s. I made new color in colors which is called hintcolor
I've been looking for solution, but there was nothing that would help me, or I just didn't understood it. I'm new at android and programming so don't judge please, just explain. Thanks a lot
It may be too late but for the sake of others who have the same problem, I solved it by making a method to get default textColorHint.It returns the color of the hint text for all the states (disabled, focussed, selected...) that specified in the current theme.
int getHintTextColor(Context context) {
int[] hintTextColor = new int[] { android.R.attr.textColorHint };
int indexOfAttrTextColorHint = 0;
TypedArray ta = context.obtainStyledAttributes(hintTextColor);
int textColor = ta.getColor(indexOfAttrTextColorHint, 0xFF808080);
ta.recycle();
return textColor;
}
I hope this helps.
I'm working on an app that has full UI customization. If you look at a EditText wrapped in an TextInputLayout, the EditText's default text hint color and line color are the same as the color textPrimaryColor defined in styles. When the EditText receives focus however, the line becomes the color of the accent defined in styles, the hint goes through the floating hint animation and changes to the accent color. It's pretty easy to do with styles.xml and themes.xml, however, I can't quite get it programmatically.
The current method I have set up is this:
public static void setInputTextLayoutColor(final int accent, final int text, TextInputLayout textInputLayout, AppCompatEditText edit) {
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
AppCompatEditText editText = (AppCompatEditText) v;
editText.getBackground().clearColorFilter();
if(hasFocus) editText.getBackground().setColorFilter(accent, PorterDuff.Mode.SRC_IN);
else editText.getBackground().setColorFilter(text, PorterDuff.Mode.SRC_IN);
}
});
setCursorColor(edit, accent);
try {
Field field = textInputLayout.getClass().getDeclaredField("mFocusedTextColor");
field.setAccessible(true);
int[][] states = new int[][]{
new int[]{}
};
int[] colors = new int[]{
accent
};
ColorStateList myList = new ColorStateList(states, colors);
field.set(textInputLayout, myList);
Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
fDefaultTextColor.setAccessible(true);
fDefaultTextColor.set(textInputLayout, myList);
Method method = textInputLayout.getClass().getDeclaredMethod("updateLabelState", boolean.class);
method.setAccessible(true);
method.invoke(textInputLayout, true);
} catch (Exception e) {
e.printStackTrace();
}
}
The problem with this method is that A) the hint text is colored immediately to the accent color, it doesn't color to the accent when focus is received B) The EditText line starts off with the right color, but when it receives focus, it colorizes to accent defined in styles, not the programatically set color.
This is the non-focused picture (in reference to the "Event name" field). Here, the color of "event name" should be gray / white (like the text"):
This is the focused picture, here, everything is right except the color of the line, which should be green as well.
Wasn't able to figure out dynamically changing the hint color, but I got the line color to change with a listener:
AppCompatEditText edit = (AppCompatEditText)findViewById(R.id.event_create_name_edit);
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
AppCompatEditText edit2 = (AppCompatEditText)v;
if(hasFocus) edit2.setSupportBackgroundTintList(ColorStateList.valueOf(rui.getAccent()));
else edit2.setSupportBackgroundTintList(ColorStateList.valueOf(rui.getText()));
}
});
I'm trying to set the expanded and collapsed CollapsingToolbarLayout's title text color to be different, but no matter what I do, it is always white.
Here is the code in question:
mCollapsingToolbar.setCollapsedTitleTextColor(getResources.getColor(R.color.foo));
mCollapsingToolbar.setExpandedTitleColor(getResources.getColor(R.color.bar));
In my layout XML file, I'm not specifying any color styles on either the AppBarLayout, the CollapsingToolbarLayout, or the Toolbar itself.
Is there some sort of interference with my Activity's theme settings?
Thanks!
I think you need to try below code:
private void changeCollapsedTitleTextColor(CollapsingToolbarLayout collapsingToolbarLayout) {
try {
final Field field = collapsingToolbarLayout.getClass().getDeclaredField("mCollapsingTextHelper");
field.setAccessible(true);
final Object object = field.get(collapsingToolbarLayout);
final Field tpf = object.getClass().getDeclaredField("mTextPaint");
tpf.setAccessible(true);
((TextPaint) tpf.get(object)).setColor(getResources().getColor(R.color.your_color));
} catch (Exception ignored) {
}
}
I found here.
I hope it may help you.
I've got a simple TextInputLayout containing an EditText View.
Now I wonder how to change the accent color (underline, hintTextColor when highlighted) programmatically.
I can't seem to find a suitable method inside TextInputLayout.
Any suggestions? Thanks in advance.
IMHO InputTextLayout can not change label color programmatically, because it is set by style. I examined source code of InputTextLayout and wrote this hack helper method which create access to private color member:
public static void setInputTextLayoutColor(EditText editText, #ColorInt int color) {
TextInputLayout til = (TextInputLayout) editText.getParent();
try {
Field fDefaultTextColor = TextInputLayout.class.getDeclaredField("mDefaultTextColor");
fDefaultTextColor.setAccessible(true);
fDefaultTextColor.set(til, new ColorStateList(new int[][]{{0}}, new int[]{ color }));
Field fFocusedTextColor = TextInputLayout.class.getDeclaredField("mFocusedTextColor");
fFocusedTextColor.setAccessible(true);
fFocusedTextColor.set(til, new ColorStateList(new int[][]{{0}}, new int[]{ color }));
} catch (Exception e) {
e.printStackTrace();
}
}
mFocusedTextColor is used for set internal CollapsingTextHelper.mCollapsedTextColor which sets color of label.
You can try this for the text,
InputTextLayout.getEditText().setHighlightColor(yourColor);
InputTextLayout.getEditText().setHintTextColor(yourColor);
and this for the line at the bottom of the EditText
Drawable background = InputTextLayout.getEditText().getBackground();
DrawableCompat.setTint(background, yourColor);
InputTextLayout.getEditText().setBackground(background);
Hope it works!
I'm searching for a possibilitie to adjust the text color of the datepicker widget in an android honeycomb app. I knew that the widget inherent the global text-color which is white in my case, but i need a black text-color for the datepicker as the background here is light grey.
Anyone know how to fix this?
DONE IT
Did it in a Theme in the application styles.xml (basically set a style on all EditText fields)
I have this in /values-v11/ so it only affects >HC
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.SelectDate" parent="#android:style/Theme.Holo.NoActionBar">
<item name="android:editTextStyle">#style/Widget.EditText.Black</item>
</style>
<style name="Widget.EditText.Black" parent="#android:style/Widget.EditText">
<item name="android:textColor">#color/black</item>
</style>
</resources>
Then in my AndroidManifest, for the Activity that uses the DatePicker:
<activity
android:name=".ui.phone.SelectDateActivity"
android:label="Date Selection"
android:screenOrientation="portrait"
android:theme="#style/Theme.SelectDate" />
That's it!
My Working Out:
I came to this conclusion by checking the DatePicker source:
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/date_picker.xml
That showed me the DatePicker used NumberPicker
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/number_picker.xml
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/number_picker_with_selector_wheel.xml
The NumberPicker uses an EditText
You can therefore style an EditText
android : how to change the style of edit text?
And if you search in this file for "editText" you will see you can set a style on all edittext fields in one Activity!
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
You override this item:
<item name="editTextStyle">#android:style/Widget.EditText</item>
I have found this solution: debugging DatePicker object, I get the object jerarqy. Maybe it's not an elegant solution but it works:
private void setNumberPickerProperties(DatePicker dp)
{
LinearLayout l = (LinearLayout)dp.getChildAt(0);
if(l!=null)
{
l = (LinearLayout)l.getChildAt(0);
if(l!=null)
{
for(int i=0;i<3;i++)
{
NumberPicker np = (NumberPicker)l.getChildAt(i);
if(np!=null)
{
EditText et = (EditText)np.getChildAt(1);
et.setTextColor(Color.BLACK);
}
}
}
}
}
Hi there :) There is an EditText widget somewhere within the datepicker widget. You just have to find it. You can do this by using some creative coding and start searching through the childrens of the datepicker widget using methods like getChildAt(index) and getChildCount() and then loop through it.
You can also do something like this, but i'm not sure that it will work on all devices, better loop through the datepickers children:
DatePicker picker;
ViewGroup childpicker;
childpicker = (ViewGroup) findViewById(Resources.getSystem().getIdentifier("month" /*rest is: day, year*/, "id", "android"));
EditText textview = (EditText) picker.findViewById(Resources.getSystem().getIdentifier("timepicker_input", "id", "android"));
textview.setTextColor(Color.GREEN);
I hope this helps :)
Hmm I did it like this:
private void hackDatePickerTextColorToBlack(){
setTextColorBlack(datePicker);
}
private static void setTextColorBlack(ViewGroup v) {
int count = v.getChildCount();
for (int i = 0; i < count; i++) {
View c = v.getChildAt(i);
if(c instanceof ViewGroup){
setTextColorBlack((ViewGroup) c);
} else
if(c instanceof TextView){
((TextView) c).setTextColor(Color.BLACK);
}
}
}
This changes the text color to black but careful with recursion this could take some time.
Also when the date picker is used the text goes back to white so that sucks!
FYI here's the source for DatePicker: https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/date_picker.xml
The EditTexts are NumberPickers
I had a similar issue, although I was looking to change the text size, but that's a minor detail. I used the same process to pick apart the View hierarchy and change the font size. However, once a month (or day or year) was changed, the font changed back to the original value. Great for viewing, bad for editing. I took the next step and added a change listener. Now when the value gets changed, it pops back to the preferred font size:
public void setFontSize(final int size) {
LinearLayout l = (LinearLayout) mPicker.getChildAt(0);
if (l != null) {
l = (LinearLayout) l.getChildAt(0);
if (l != null) {
for (int i = 0; i < 3; i++) {
NumberPicker np = (NumberPicker) l.getChildAt(i);
for (int x = 0; x < np.getChildCount(); x++) {
View view = np.getChildAt(x);
if ((view != null) && (view instanceof TextView)) {
final TextView tv = (TextView) view;
tv.setTextSize(size);
tv.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v,
int actionId, KeyEvent event) {
tv.setTextSize(size);
return false;
}
});
}
}
}
}
}
}