logo
down
shadow

How to set input type and format in EditText using kotlin?


How to set input type and format in EditText using kotlin?

Content Index :

How to set input type and format in EditText using kotlin?
Tag : android , By : Chris Tattum
Date : November 20 2020, 04:01 AM

this one helps. I want to force users to enter a Number and i'm using that (and it's working fine): , Here's how I like to do this:
add this for edit text
 android:inputType="numberDecimal"  
android:digits="0123456789."
class InputFilterMinMax(min:Float, max:Float): InputFilter {
    private var min:Float = 0.0F
    private var max:Float = 0.0F

    init{
        this.min = min
        this.max = max
    }

    override fun filter(source:CharSequence, start:Int, end:Int, dest: Spanned, dstart:Int, dend:Int): CharSequence? {
        try
        {
            val input = (dest.subSequence(0, dstart).toString() + source + dest.subSequence(dend, dest.length)).toFloat()
            if (isInRange(min, max, input))
                return null
        }
        catch (nfe:NumberFormatException) {}
        return ""
    }

    private fun isInRange(a:Float, b:Float, c:Float):Boolean {
        return if (b > a) c in a..b else c in b..a
    }
}
myEditText.setFilters(arrayOf<InputFilter>(InputFilterMinMax(0.0F, 20.0F)))
public class InputFilterMinMax implements InputFilter {
    private float min;
    private float max;

    public InputFilterMinMax(float min, float max) {
        this.min = min;
        this.max = max;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        //noinspection EmptyCatchBlock
        try {
            float input = Float.parseFloat(dest.subSequence(0, dstart).toString() + source + dest.subSequence(dend, dest.length()));
            if (isInRange(min, max, input))
                return null;
        } catch (NumberFormatException nfe) { }
        return "";
    }

    private boolean isInRange(float a, float b, float c) {
        return b > a ? c >= a && c <= b : c >= b && c <= a;
    }
}
myEditText.setFilters(new InputFilter[]{new InputFilterMinMax(0.0, 20.0)});

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Better way to Format Currency Input editText?


Tag : android , By : dummyadresse
Date : March 29 2020, 07:55 AM
this one helps. I tested your method, but it fails when I use great numbers... I created this:
private String current = "";
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    if(!s.toString().equals(current)){
       [your_edittext].removeTextChangedListener(this);

       String cleanString = s.toString().replaceAll("[$,.]", "");

       double parsed = Double.parseDouble(cleanString);
       String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));

       current = formatted;
       [your_edittext].setText(formatted);
       [your_edittext].setSelection(formatted.length());

       [your_edittext].addTextChangedListener(this);
    }
}

EditText Input Format


Tag : chash , By : JoeCh
Date : March 29 2020, 07:55 AM
like below fixes the issue I would like to make a EditText field with format like this: 0000 AA. , This should do the trick:
EditText zipcode = FindViewById<EditText>(Resource.Id.zipcode);
zipcode.InputType = Android.Text.InputTypes.ClassNumber;
bool numberMode = true;
zipcode.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) => {
    if(zipcode.Text.Length == 4){
        if(numberMode){
            numberMode = false;
            zipcode.Text = zipcode.Text + " ";
            zipcode.SetSelection(zipcode.Text.Length);
        }
    }

    if(zipcode.Text.Length > 4){
        numberMode = false;
        zipcode.InputType = Android.Text.InputTypes.ClassText;
    }

    if(zipcode.Text.Length <= 4){
        numberMode = true;
        zipcode.InputType = Android.Text.InputTypes.ClassNumber;
    }
};

android: EditText input format


Tag : android , By : Alex Sadzawka
Date : March 29 2020, 07:55 AM
Hope that helps I can think of two ways of achieving it:
Use addTextChangedListener:
yourEditText.addTextChangedListener(new TextWatcher() {

          public void afterTextChanged(Editable s) {

            // Do your tricks here 

          }

          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          public void onTextChanged(CharSequence s, int start, int before, int count) {}
       });

how can create specific input type on edittext? The input accettable is Roman numbers M D C X V I


Tag : java , By : Henry Fatino
Date : March 29 2020, 07:55 AM
it should still fix some issue You'll want to implement an InputFilter to restrict the input to the acceptable characters and assign that to the EditText. In first iteration, this is really simple. Take a look at the source code of LoginFilter.UsernameFilterGeneric for an example. You'll have this set up in minutes.
If that's all you need, great, stop there! Unfortunately, this may be all you need. There are some grammatical rules to roman numerals, for instance XXCI is (100-20)+1=81, but IXXC is not 100-(20-1)=81. If you want to cover that, then InputFilter is too crude. You'll still use the InputFilter to make the initial filtering, but you'll have to implement TextWatcher on top of that to handle the advanced input validation. The PhoneNumberFormattingTextWatcher (source) gives you some clues on how to do that. It's an awful lot more work than just the InputFilter, because you'll have to deal with all sorts of weird multi-character inputs, such as clipboard paste-to-replace-selection.

Parsing user input in edittext field of the type date and check for correct date format


Tag : java , By : Ronnie Carlin
Date : March 29 2020, 07:55 AM
wish of those help
I just want to check that the users does not enter more than 6 digits and that these digits have the right format.
if (inputSTR.matches("\\d{2}-\\d{2}-\\d{2}")) {
    // input matches to required pattern
} else {
    // Show error msg
}
Related Posts Related QUESTIONS :
  • Schedule a repeating alarm clock
  • Can't assign values outside onTouchListener
  • Is setContentView() needed to inflate a fragment?
  • Why instead of string method returns Kotlin.Unit?
  • Retrofit slash as Query
  • When does onCreateView() get called in this app?
  • Fragment is reloaded each time after rejecting location permission
  • How to read ViewModel data from sttrings.xml list of items
  • Flutter doctor does not see vs code ,VS code does not detect flutter/bin
  • How to build aar in flutter
  • Is displayMetrix xdpi and ydpi accurate?
  • How to detect doubletap on a View?
  • how to play an encrypted file in Android
  • Android: keytool error on the command line when locating debug.keystore
  • Android paint/canvas issue; drawing smooth curves
  • Problems with starting an activity in onStart
  • Performance of map overlay in conjunction with ItemizedOverlay is very poor
  • How to change android indeterminate ProgressBar color?
  • How do I change the Mobile Country Code (MCC) in the Android Emulator?
  • Stop Progressbar manual scrolling in Android
  • Android: download large file
  • Android : uploaded .apk file to server but cant download that .apk file
  • How do I distribute Android app to beta tester without using Market
  • Android Unable to launch the Inbox from a notification
  • Android: Ringer mode changed to silent doesn't stop the phone vibrating
  • Change the side the text appears on a radio button
  • Check the memory usage anp cpu and power of an application in android
  • What is the String 'volumeName' argument of MediaStore.Audio.Playlists.Members.getContentUri referring to?
  • Playing Multiple sounds at the same time in Android
  • Android: how to change the time in emulator?
  • close the soft key pad when i click on other views in android
  • Setting the position within a spinner
  • How to play ringtone/alarm sound in Android
  • How to adjust text font size to fit textview
  • "Android 2.x" vs "Google APIs" for Android AVD Setup
  • How to center buttons on screen horizontally and vertically plus equidistant apart?
  • Discrete seekbar in Android app?
  • Gradle Build Fails - Illegal exception table range in class file com/google/wireless/android/sdk/stats/GradleBuildMemory
  • register has type Long (High Half) but expected Long (Low Half)
  • Is it good idea to use Dispatcher.Main with delay( )?
  • WebView and Advanced View is Not loading partiucalr URL
  • How to show three dots at the end of text view
  • Is it possible to run macros within Android Studio project?
  • Adding a button with default appearance but changed background color
  • What is best way to show notification at a certain time in Android?
  • Jetifier not working when library uses android component and app uses androidx component
  • How can I write the contents of an image in a filesystem
  • Retrieve a list of images from the Database and display them using a ListView?
  • java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
  • Timer/hourglass wont stop
  • How can I make AndroidX the default library for new Android projects?
  • Is it okay to delete buildToolsVersion android studio because of a warning in the Asyncstorage library?
  • Can I call a class that doesn't have an activity withIntent?
  • Android TextView fontFamily not working when set via TextAppearance
  • Unable to find nearby places
  • Is there any way to launch my application right away without 'Ok Google"?
  • Places api set location biased based on current location
  • Layout Preview is not displaying layout when Material Design is added
  • How do I resolve this diamond problem in Kotlin?
  • How to update the custom value in a view model object
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com