Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Maybe some implementation similar to this? I'm pretty sure it can be optimized a lot!</p> <pre><code>EditText et; ..... // force number input type on edittext et.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); et.addTextChangedListener(new CustomTextWatcher(et)); </code></pre> <p>where:</p> <pre><code>class CustomTextWatcher implements TextWatcher { private NumberFormat nf = NumberFormat.getNumberInstance(); private EditText et; private String tmp = ""; private int moveCaretTo; private static final int INTEGER_CONSTRAINT = 6; private static final int FRACTION_CONSTRAINT = 1; private static final int MAX_LENGTH = INTEGER_CONSTRAINT + FRACTION_CONSTRAINT + 1; public CustomTextWatcher(EditText et) { this.et = et; nf.setMaximumIntegerDigits(INTEGER_CONSTRAINT); nf.setMaximumFractionDigits(FRACTION_CONSTRAINT); nf.setGroupingUsed(false); } public int countOccurrences(String str, char c) { int count = 0; for (int i = 0; i &lt; str.length(); i++) { if (str.charAt(i) == c) { count++; } } return count; } @Override public void afterTextChanged(Editable s) { et.removeTextChangedListener(this); // remove to prevent stackoverflow String ss = s.toString(); int len = ss.length(); int dots = countOccurrences(ss, '.'); boolean shouldParse = dots &lt;= 1 &amp;&amp; (dots == 0 ? len != (INTEGER_CONSTRAINT + 1) : len &lt; (MAX_LENGTH + 1)); if (shouldParse) { if (len &gt; 1 &amp;&amp; ss.lastIndexOf(".") != len - 1) { try { Double d = Double.parseDouble(ss); if (d != null) { et.setText(nf.format(d)); } } catch (NumberFormatException e) { } } } else { et.setText(tmp); } et.addTextChangedListener(this); // reset listener //tried to fix caret positioning after key type: if (et.getText().toString().length() &gt; 0) { if (dots == 0 &amp;&amp; len &gt;= INTEGER_CONSTRAINT &amp;&amp; moveCaretTo &gt; INTEGER_CONSTRAINT) { moveCaretTo = INTEGER_CONSTRAINT; } else if (dots &gt; 0 &amp;&amp; len &gt;= (MAX_LENGTH) &amp;&amp; moveCaretTo &gt; (MAX_LENGTH)) { moveCaretTo = MAX_LENGTH; } try { et.setSelection(et.getText().toString().length()); // et.setSelection(moveCaretTo); &lt;- almost had it :)) } catch (Exception e) { } } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { moveCaretTo = et.getSelectionEnd(); tmp = s.toString(); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { int length = et.getText().toString().length(); if (length &gt; 0) { moveCaretTo = start + count - before; } } } </code></pre> <p>Not 100% but you can use as a base and build on top of it ;)</p> <p><strong>EDIT:</strong> tried to polishing setting the caret position after text changed but it was more difficult than I estimated and reverted to setting the caret at the end after each char input. I left the code I started on for the caret maybe you can improve it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload