Change Divider Color Android DatePicker Dialog - android

I´m tying to change the divider color of the DatePicker Dialog.
I create the style:
<style name="dialog_custom" parent="#android:style/Widget.DatePicker">
<item name="android:divider">#drawable/dialog_divider</item>
</style>
And create the drawable like this
And the result is this
The divider no change color and the dialog take the content size..

This is my solution to change divider colors in NumberPickers, TimePickers, DatePickers and the TimePickerDialog. For DatePickerDialog you can call DatePickerDialog.getDatePicker()
public class NumberPickerStylingUtils {
private static final Drawable PICKER_DIVIDER_DRAWABLE = //Place your drawable here
private NumberPickerStylingUtils() {}
public static void applyStyling(TimePickerDialog timePickerDialog) {
try {
Field field = TimePickerDialog.class.getDeclaredField("mTimePicker");
field.setAccessible(true);
applyStyling((TimePicker) field.get(timePickerDialog));
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void applyStyling(TimePicker timePicker) {
try {
Field fields[] = TimePicker.class.getDeclaredFields();
for (Field field : fields) {
if (field.getType().equals(NumberPicker.class)) {
field.setAccessible(true);
applyStyling((NumberPicker) field.get(timePicker));
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void applyStyling(DatePicker datePicker) {
try {
Field fields[] = DatePicker.class.getDeclaredFields();
for (Field field : fields) {
if (field.getType().equals(NumberPicker.class)) {
field.setAccessible(true);
applyStyling((NumberPicker) field.get(datePicker));
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void applyStyling(NumberPicker numberPicker) {
try {
Field field = NumberPicker.class.getDeclaredField("mSelectionDivider");
field.setAccessible(true);
field.set(numberPicker, PICKER_DIVIDER_DRAWABLE));
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

You can do this using theme. Check accepted answer on this question. i think it will helpful to you.
UPDATES
Expand res folder in your application and expand values folder. Then create themes.xml file on values folder. then replace all code in themes.xml file with below code.
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MYTheme" parent="#android:style/Theme">
<item name="android:divider">#drawable/dialog_divider</item>
</style>
</resources>
Then open your AndroidManifest.xml file. and find android:theme and replcae with android:theme="#style/MYTheme"

Related

How to get color of Toolbar's/ActionBar's text color programmatically?

I'm trying to get toolbar's textColor using this method:
private int getToolbarTextColor() {
int toolbarTextColor;
TypedArray typedArray = getTheme().obtainStyledAttributes(new int[]{R.attr.titleTextColor});
try {
toolbarTextColor = typedArray.getColor(0, Color.TRANSPARENT);
} catch (UnsupportedOperationException e) {
toolbarTextColor = Color.TRANSPARENT;
} finally {
typedArray.recycle();
}
return toolbarTextColor;
}
but it returns 0. What is the problem in my code? What attribute should I use? Is there another better way to get it?
Here is a method that uses reflection for obtaining a Toolbar title TextView's color:
#ColorInt public static int getToolbarTitleTextColor(Toolbar toolbar) {
try {
Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
f.setAccessible(true);
TextView mTitleTextView = (TextView) f.get(toolbar);
if (mTitleTextView != null) {
return mTitleTextView.getCurrentTextColor();
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;}
val a = TintTypedArray.obtainStyledAttributes(context, null, R.styleable.Toolbar, R.attr.toolbarStyle, 0);
title.setTextColor(a.getColor(R.styleable.Toolbar_titleTextColor, -0x1))
subtitle.setTextColor(a.getColor(R.styleable.Toolbar_titleTextColor, -0x1))
(Kotlin)

Determine if two resource id's point to same layout

I have an Android setup that has a resource file that refers to a single layout from a choice of two.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
<item name="another_resource_id" type="layout">#layout/some_layout</item>
</resources>
or
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
<item name="another_resource_id" type="layout">#layout/some_layout2</item>
</resources>
This produces the R.layout
public static final class layout {
...
public static final int another_resource_id=0x7f030000;
public static final int some_layout=0x7f030001;
public static final int some_layout2=0x7f030002;
...
}
So the issue is that from the code I want to be able to tell which layout the another_resource_id resource identifier refers to.
I'll answer my question: Seems there is no straight forward way to tell the root view for a layout EXCEPT to dig down and get the original XML for the layout. Comparing the two is how I am able to tell if they are the same view. Kind of the long way around, but there it is. The following code just compares the addition of the resouce id for the underlying elements. Obviously you could put a lot more complex logic in.
try {
XmlResourceParser xrp = ctx.getResources().getLayout(id);
int eventType;
xrp.next();
String resourceIds = "";
do {
eventType = xrp.nextToken();
if (eventType == XmlPullParser.START_TAG) {
int attrs = xrp.getAttributeCount();
if (attrs > 0) {
for (int i = 0; i < attrs; i++) {
if (xrp.getAttributeName(i).compareTo("id") == 0) {
resourceIds += xrp.getAttributeValue(i);
}
}
}
}
} while (eventType != XmlPullParser.END_DOCUMENT);
returnVal = resourceIds.hashCode();
} catch (XmlPullParserException e) {
// e.printStackTrace();
} catch (FileNotFoundException e) {
// e.printStackTrace();
} catch (IOException e) {
// e.printStackTrace();
} catch (Resources.NotFoundException e) {
// e.printStackTrace();
}

Change text style / size of selected value in NumberPicker

I've got two problems to custom my numberpicker on my application. After lot of research I didn't found anything that help me.
First is that I want increase the selected text. I already have a class that extends number picker to customize the general text size, font and divider visibility, but I'm not able to increase only the selected value.
Second problems, I want to put some unicode on my text, so I'm trying to use a spannableString[] instead of String[] for the formattter, but nothing seems to work. I would use Spannable to be able to change the police font of a little part of my text (With some Regex)
Is there a way to do this ?
What I've done actually :
public class MyNumberPicker extends NumberPicker{
private Context context;
public MyNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
#Override
public void addView(View child) {
super.addView(child);
updateView(child);
}
#Override
public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
super.addView(child, index, params);
updateView(child);
}
#Override
public void addView(View child, android.view.ViewGroup.LayoutParams params) {
super.addView(child, params);
updateView(child);
}
private void updateView(View view) {
if(view instanceof EditText){
//Text size
((EditText) view).setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
//Text font
Typeface robotoFont = FontCache.get("fonts/Roboto-Regular.ttf", getContext());
((EditText) view).setTypeface(robotoFont);
}
//Non visible selectors
java.lang.reflect.Field[] pickerFields = NumberPicker.class.getDeclaredFields();
for (java.lang.reflect.Field pf : pickerFields) {
if (pf.getName().equals("mSelectionDivider")) {
pf.setAccessible(true);
try {
ColorDrawable colorDrawable = new ColorDrawable(ContextCompat.getColor(
getContext(), R.color.colorTransparent
));
pf.set(this, colorDrawable);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}
}
THe formatter :
npTemp.setMinValue(0);
npTemp.setMaxValue(values.length - 1);
NumberPicker.Formatter formatter = new NumberPicker.Formatter() {
#Override
public String format(int value) {
return values[value];
}
};
npTemp.setFormatter(formatter);
What I've tried to get the selected value EditText on my custom Number picker
java.lang.reflect.Field[] pickerFields = NumberPicker.class.getDeclaredFields();
for (java.lang.reflect.Field pf : pickerFields) {
if (pf.getName().equals("mInputType")) {
pf.setAccessible(true);
try {
((EditText)pf).setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
And to have SpannableString, I've done lot of things but the formatter only want String.
Thanks for helping

Change divider color doesn't work with min. SDK 21

I have an android app, where I would like to change the divider color of my date picker.
This works fine until SDK 20.
SDK 21 => shows the default blue divider
SDK 23 => shows grey divider
SDK 15 - 20 => shows my green divider (Correct)
This is my code:
// DatePicker Set Divider Color
private void preparePicker() {
try {
Field datePickerFields[] = DP.getClass().getDeclaredFields();
for (Field field : datePickerFields) {
if ("mSpinners".equals(field.getName())) {
field.setAccessible(true);
Object spinnersObj = field.get(DP);
LinearLayout mSpinners = (LinearLayout) spinnersObj;
NumberPicker monthPicker = (NumberPicker) mSpinners.getChildAt(0);
NumberPicker dayPicker = (NumberPicker) mSpinners.getChildAt(1);
NumberPicker yearPicker = (NumberPicker) mSpinners.getChildAt(2);
setDividerColor(monthPicker);
setDividerColor(dayPicker);
setDividerColor(yearPicker);
break;
}
}
} catch (Exception ex) {
Log.e("-->", "Unable to change date dialog");
}
}
private void setDividerColor(NumberPicker picker) {
Field[] numberPickerFields = NumberPicker.class.getDeclaredFields();
for (Field field : numberPickerFields) {
if (field.getName().equals("mSelectionDivider")) {
field.setAccessible(true);
try {
field.set(picker, getResources().getDrawable(R.drawable.picker_divider));
} catch (IllegalArgumentException e) {
Log.e("-->", "Illegal Argument Exception");
e.printStackTrace();
} catch (Resources.NotFoundException e) {
Log.e("-->", "Resources NotFound");
e.printStackTrace();
} catch (IllegalAccessException e) {
Log.e("-->", "Illegal Access Exception");
e.printStackTrace();
}

Set different text colors to different options in NumberPicker (android)

I would like to create a NumberPicker for the four suits of playing cards (as unicode) with 2 of the suits displaying with red font and the other 2 with black font.
All the solutions regarding NumberPicker and text-color change the color of all the elements of the picker.
Right now, I have made it so that when the value changes, the color also changes. This allows the suit selected to be in the correct color but will also change the adajacent suits to be the wrong color.
Is there any way to have alternating colors in NumberPicker?
Thanks in advance:)
String[] suits = {
"\u2665", "\u2660", "\u2666", "\u2663"
};
suitPicker.setMaxValue(suits.length - 1);
suitPicker.setMinValue(0);
suitPicker.setDisplayedValues(suits);
suitPicker.setWrapSelectorWheel(true);
suitPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
setNumberPickerTextColor(suitPicker, getResources().getColor(R.color.GreyDark));
suitPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {#
Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
if (newVal % 2 == 0) {
setNumberPickerTextColor(suitPicker, getResources().getColor(R.color.Red));
} else {
setNumberPickerTextColor(suitPicker, getResources().getColor(R.color.GreyDark));
}
}
});
.....
public boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) {
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);
selectorWheelPaintField.setAccessible(true);
((Paint) selectorWheelPaintField.get(numberPicker)).setColor(color);
((EditText) child).setTextColor(color);
numberPicker.invalidate();
return true;
} catch (NoSuchFieldException e) {
Log.w("setNumberPickerTextColor", e);
} catch (IllegalAccessException e) {
Log.w("setNumberPickerTextColor", e);
} catch (IllegalArgumentException e) {
Log.w("setNumberPickerTextColor", e);
}
}
}
return false;
}

Categories

Resources