-3

I have made a Edittext that contains a data which i copied from Clipboard. so I want to check that copied data is text or number in android only. Please help me if you know

ADM
  • 20,406
  • 11
  • 52
  • 83
  • 1
    Possible duplicate of [How to check if a String is numeric in Java](https://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-numeric-in-java) – ADM Jan 15 '19 at 06:57
  • Also check https://stackoverflow.com/questions/6651184/set-text-and-get-text-from-clipboard – ADM Jan 15 '19 at 06:58

1 Answers1

1

If you want to get the test from clipboard, you can use ClipboardManager to do that.

ClipboardManager clipboard = (ClipboardManager) 
getSystemService(Context.CLIPBOARD_SERVICE);
   try {
     CharSequence textToPaste = clipboard.getPrimaryClip().getItemAt(0).getText();
   } catch (Exception e) {
     return;
}

I want to check that copied data is text or number in android only

Since you have to use getText() it will return a CharSequence, so if you want to detect if what's inside this CharSequence is a number or string you'll have to do some algorithm to get that, something like this :

You can use Character.isNumber() to check if it's a number or Character.isLetter() to check if it's a string.

For example :

for (char c : yourDataFromClipboard.toCharArray()) {
    if (Character.isDigit(c)) {
       Character.getNumericValue(c); //is a number
    }
    else if(Character.isLetter(c)){
       //is string 
    }
}
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148