Android Double NumberPicker with increment and accuracy -
i trying create modified numberpicker double/float values increment , precision setting can increment values other 1. use cases, have calculator app want use 2-3 of these numberpickers input/select double value. inputs might on order of 10,000,000 , others might small 0.0001 need able adjust increment step , precision each picker. since there large range of values don't want use setdisplayedvalues. want allow user input numbers directly through edittext on numberpicker.
this have far numberpickerdouble class:
import android.annotation.targetapi; import android.content.context; import android.os.build; import android.text.inputfilter; import android.util.attributeset; import android.view.view; import android.view.viewgroup; import android.widget.edittext; import android.widget.numberpicker; import java.math.bigdecimal; public class numberpickerdouble extends numberpicker { private double mprecision = 1; private double mincrement = 1; private edittext numberedittext; private boolean shoulddisableedittext = true; //set true disable edit text input , focus public numberpickerdouble(context context) { super(context); } public numberpickerdouble(context context, attributeset attrs) { super(context, attrs); init(attrs); } public numberpickerdouble(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(attrs); } @targetapi(build.version_codes.lollipop) public numberpickerdouble(context context, attributeset attrs, int defstyleattr, int defstyleres) { super(context, attrs, defstyleattr, defstyleres); init(attrs); } private void init(attributeset attrs){ numberedittext = populateedittext((viewgroup) this); if (numberedittext != null && shoulddisableedittext) numberedittext.setfocusable(false); processattributeset(attrs); } public void setprecision(double precision){ mprecision = precision; } public double getprecision(){ return mprecision; } public void setincrement(double increment) { mincrement = increment; setformat(); } public double getincrement(){ return mincrement; } public string getvalueformat(){ int decimalplaces = bigdecimal.valueof(mprecision).scale(); if (decimalplaces > 0) return "%." + string.valueof(decimalplaces) + "f"; else return "%d"; } public void setdoublevalue(double value){ setvalue((int)(value / mincrement)); numberedittext.setfilters(new inputfilter[0]); } private void setformat() { numberpicker.formatter formatter = new numberpicker.formatter(){ @override public string format(int value) { double temp = value * mincrement; int precision = bigdecimal.valueof(mprecision).scale(); string newvalue; if (precision > 0) newvalue = string.format("%." + precision +"f", temp); else newvalue = "" + temp; return newvalue; } }; setformatter(formatter); } private edittext populateedittext(viewgroup viewgroup) { int count = viewgroup.getchildcount(); (int = 0; < count; i++) { final view child = viewgroup.getchildat(i); if (child instanceof viewgroup) populateedittext((viewgroup) child); else if (child instanceof edittext) return (edittext) child; } return null; } private void processattributeset(attributeset attrs) { } }
the increment , precision work expected swiping have 2 issues:
upon first initializing view, numberpickers don't format first value , either leaves out decimal or displays nothing (see image below). once scroll picker, goes away. tried of proposed solutions in post nothing seems work. numberpicker formatter not working on first rendering
i can't input numbers in edittext of numberpicker if could, can't figure out how best intercept input value divide increment have in setdoublevalue method.
here how setting numberpickers in image above:
final numberpickerdouble np1 = (numberpickerdouble) v.findviewbyid(r.id.numpicker1); final numberpickerdouble np2 = (numberpickerdouble) v.findviewbyid(r.id.numpicker2); final numberpickerdouble np3 = (numberpickerdouble) v.findviewbyid(r.id.numpicker3); np1.setmaxvalue(1000000); np2.setmaxvalue(200000); np3.setmaxvalue(200); np1.setminvalue(0); np2.setminvalue(0); np3.setminvalue(0); np1.setprecision(1); np2.setprecision(1); np3.setprecision(0.001); np1.setincrement(100); np2.setincrement(1); np3.setincrement(0.002); np1.setwrapselectorwheel(false); np2.setwrapselectorwheel(false); np3.setwrapselectorwheel(false); np1.setdoublevalue(0); np1.setdoublevalue(0); np1.setdoublevalue(0);
Comments
Post a Comment