Change text style / size of selected value in NumberPicker - android

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

Related

How to remove or hide MarkerView in MPAndroidChart?

I did a bar chart with MPAndroidChart. I did a custom marker view class to view the values of the bar.
I would remove or hide the view after some seconds. It should work as a toast. But I cannot do it.
This is a problem first of all because after I touched the chart for the first time, it becomes visible and it disappears only if i find the right tapped condition.
There is someone who knows how solve this problem?
In my code I have:
mChart.invalidate();
mv = new CustomMarkerView(getContext(), R.layout.alert_chart, position, jsonResponse);
mChart.setMarker(mv);
And my CustomMarkerView is
public class CustomMarkerViewextends MarkerView {
public TextView correct;
public TextView wrong ;
public int position;
public JSONArray jsonArray;
public CustomMarkerView(Context context, int layoutResource, int pos, JSONArray json) {
super(context, layoutResource);
correct = (TextView) findViewById(R.id.correct);
wrong = (TextView) findViewById(R.id.wrong);
position = pos;
jsonArray = json;
}
#Override
public void refreshContent(Entry e, Highlight highlight) {
switch (position){
case 1:
try {
correct.setText(getContext().getString(R.string.correct_alert) + " " + jsonArray.getJSONObject((int)(e.getX())).getString("TP"));
wrong.setText(getContext().getString(R.string.wrong_alert) + " " + jsonArray.getJSONObject((int)(e.getX())).getString("FP"));
} catch (JSONException e1) {
e1.printStackTrace();
} catch (ParseException e1) {
e1.printStackTrace();
}
break;
case 2:
try {
correct.setText(getContext().getString(R.string.tpr_alert) + " " + Math.round(Double.parseDouble(jsonArray.getJSONObject((int)(e.getX())).getString("TPR"))*100.0)/100.0);
wrong.setText(getContext().getString(R.string.msr_alert) + " " + Math.round(Double.parseDouble(jsonArray.getJSONObject((int)(e.getX())).getString("MSR"))*100.0)/100.0);
} catch (JSONException e1) {
e1.printStackTrace();
} catch (ParseException e1) {
e1.printStackTrace();
}
break;
case 3:
try {
correct.setText(getContext().getString(R.string.nmean_alert) + " " +Math.round(Double.parseDouble(jsonArray.getJSONObject((int)(e.getX())).getString("n_mean"))*100.0)/100.0);
wrong.setText(getContext().getString(R.string.nmax_alert) + " " +Math.round(Double.parseDouble(jsonArray.getJSONObject((int)(e.getX())).getString("n_max"))*100.0)/100.0);
} catch (JSONException e1) {
e1.printStackTrace();
} catch (ParseException e1) {
e1.printStackTrace();
}
break;
}
// this will perform necessary layouting
super.refreshContent(e, highlight);
}
private MPPointF mOffset;
#Override
public MPPointF getOffset() {
if(mOffset == null) {
// center the marker horizontally and vertically
mOffset = new MPPointF(-(getWidth()/2), -getHeight());
}
return mOffset;
}
}
I know, it's a bit late, but:
mChart.highlightValue(null);
Worked for me
Set an onTouchListener to your chart and in the MotionEvent ACTION_UP set the highlight value to null. Code :
chart.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP: {
chart.highlightValue(null);
break;
}
}
return false;
}
});

Dynamic Spinners item selection error

I' am creating and populating spinners dynamically,the problem is while selecting the items(eg:4th) of any spinner ,i'am getting the id of the last spinners which is created dynamically item(4th id). I want to get the corresponding id's which i was set dynamically to the spinner items.
How to get the values of each spinner correctly..
Codes are give below.
public void secondarray(JsonParser jsonParser) {
String fieldName;
ameList = new ArrayList<AmeModel>();
AmeModel ameModel = null;
try {
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
ameModel = new AmeModel();
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
fieldName = jsonParser.getCurrentName();
if ("valueid".equals(fieldName)) {
jsonParser.nextToken();
ameModel.setId((jsonParser.getText()));
} else if ("valuename".equals(fieldName)) {
jsonParser.nextToken();
ameModel.setName((jsonParser.getText()));
} else {
jsonParser.skipChildren();
}
}
ameList.add(ameModel);
}
Spinner sp=new Spinner(MainActivity.this);
AmeAdapter adapter = new AmeAdapter(this, ameList);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, ameList.get(i).getId(), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
container.addView(sp);
//setContentView(linearlayout);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Any help will be appreciated thank you.
For get selected item Name:
String itemName=sp.getSelectedItem().toString(); // sp is your spinner
For get selected item position :
int position = sp.getSelectedItemPosition();
Now as per your code snippiest If you want to get Id Those you have set dynamically.
HashMap<String, String> nameId;
String itemId;
String itemName= String.valueOf(sp.getSelectedItem());
for (String s : nameId.keySet()) {
if (nameId.get(s).equals(itemName)) {
itemId= s;
}
Try to use above suggestion

how to get label from custom tablayout?

I am creating tabs with icons and text using ,
private void setupTab() {
int i = 0;
for (Map.Entry<String, Integer> entry : tabvalues.entrySet()) {
try {
TextView tabView = (TextView) LayoutInflater.from(baseActivity).inflate(R.layout.custom_tab, null);
tabView.setText(entry.getKey());
tabView.setCompoundDrawablePadding(10);
tabView.setCompoundDrawablesWithIntrinsicBounds(0, entry.getValue(), 0, 0);
tabLayout.getTabAt(i).setCustomView(tabView);
i++;
} catch (Exception e) {
e.printStackTrace();
}
}
now I wnt to get the text of the tab,
tabLayout.getTabAt(position).getText().toString();
It is returning null.
You can try:
((TextView)tabLayout.getTabAt(position).getCustomView()).getText().toString();

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)

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