How to disable a button only when EditText is numerically empty in android studio? -


i have button works according function when user enters number in edittext. however,when edittext numerically empty,when click on same button, make app stop working. help?

please consider "numerically" part because not find answers query?

code     public void send(view v){   string text = edittext.gettext().tostring();   if (text.matches("")) {  mybutton.setenabled(false);  return;        }  } 

xml

<linearlayout android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="horizontal">     <edittext         android:layout_width="0dp"         android:layout_weight="1"         android:inputtype="number"         android:layout_height="wrap_content"         android:hint="enter:"         android:id="@+id/edittext"         />     <button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="send"         android:onclick="send"         android:id="@+id/btn"/> </linearlayout>  <listview     android:id="@+id/listview"     android:layout_width="match_parent"     android:layout_height="wrap_content"     /> 

logcat  01-16 10:49:15.208 3014-3014/? i/selinux: function:    selinux_android_load_priority [0], there no sepolicy file.  01-16 10:49:15.208 3014-3014/? i/selinux: function: selinux_android_load_priority [1], there no sepolicy version file.  01-16 10:49:15.208 3014-3014/? i/selinux: function: selinux_android_load_priority , priority version ve=google_policy   01-16 10:49:15.208 3014-3014/? i/selinux: selinux_android_seapp_context_reload: seapp_contexts file loaded /seapp_contexts 01-16 10:49:15.208 3014-3014/? e/selinux: [debug] seapp_context_lookup: seinfocategory = default 01-16 10:49:15.208 3014-3014/? e/selinux: [debug] seapp_context_lookup: seinfocategory = default 01-16 10:49:15.208 3014-3014/? d/dalvikvm: late-enabling checkjni 01-16 10:49:15.298 3014-3014/com.example.dell.uomopenday i/dalvikvm: not find method android.app.notification$builder.setlocalonly, referenced method com.google.android.gms.common.googleplayservicesutil.zza 01-16 10:49:15.298 3014-3014/com.example.dell.uomopenday w/dalvikvm: vfy: unable resolve virtual method 254:   landroid/app/notification$builder;.setlocalonly   (z)landroid/app/notification$builder; 01-16 10:49:15.298 3014-3014/com.example.dell.uomopenday d/dalvikvm: vfy: replacing opcode 0x6e @ 0x00c8 01-16 10:49:15.308 3014-3014/com.example.dell.uomopenday i/dalvikvm: not find method android.content.pm.packagemanager.getpackageinstaller, referenced method com.google.android.gms.common.googleplayservicesutil.zzj 01-16 10:49:15.308 3014-3014/com.example.dell.uomopenday w/dalvikvm: vfy: unable resolve virtual method 549:   landroid/content/pm/packagemanager;.getpackageinstaller ()landroid/content/pm/packageinstaller;  01-16 10:49:15.308 3014-3014/com.example.dell.uomopenday d/dalvikvm: vfy: replacing opcode 0x6e @ 0x000b  01-16 10:49:15.358 3014-3037/com.example.dell.uomopenday i/gmpm: app measurement starting  01-16 10:49:15.358 3014-3037/com.example.dell.uomopenday e/gmpm: getgoogleappid failed status: 10  01-16 10:49:15.368 3014-3037/com.example.dell.uomopenday e/gmpm: uploading not possible. app measurement disabled  01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday w/dalvikvm: vfy: unable find class referenced in signature (landroid/view/searchevent;)  01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday i/dalvikvm: not find method android.view.window$callback.onsearchrequested, referenced method android.support.v7.view.windowcallbackwrapper.onsearchrequested  01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday w/dalvikvm: vfy: unable resolve interface method 19469: landroid/view/window$callback;.onsearchrequested (landroid/view/searchevent;)z  01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday d/dalvikvm: vfy: replacing opcode 0x72 @ 0x0002  01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday i/dalvikvm: not find method android.view.window$callback.onwindowstartingactionmode, referenced method android.support.v7.view.windowcallbackwrapper.onwindowstartingactionmode  01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday w/dalvikvm: vfy: unable resolve interface method 19473: landroid/view/window$callback;.onwindowstartingactionmode (landroid/view/actionmode$callback;i)landroid/view/actionmode;  01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday d/dalvikvm: vfy: replacing opcode 0x72 @ 0x0002  01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday i/dalvikvm: not find method android.content.res.typedarray.getchangingconfigurations, referenced method android.support.v7.widget.tinttypedarray.getchangingconfigurations 01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday w/dalvikvm: vfy: unable resolve virtual method 627: landroid/content/res/typedarray;.getchangingconfigurations ()i 01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday d/dalvikvm: vfy: replacing opcode 0x6e @ 0x0002 01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday i/dalvikvm: not find method android.content.res.typedarray.gettype, referenced method android.support.v7.widget.tinttypedarray.gettype 01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday w/dalvikvm: vfy: unable resolve virtual method 649: landroid/content/res/typedarray;.gettype (i)i 01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday d/dalvikvm: vfy: replacing opcode 0x6e @ 0x0002 01-16 10:49:15.508 3014-3014/com.example.dell.uomopenday i/adreno-egl: <qegldrvapi_eglinitialize:410>: egl 1.4 qualcomm build:  ()                                                                    opengl es  shader compiler version: e031.24.00.08                                                                    build date: 03/21/14 fri                                                                    local branch: au200+patches_03212014                                                                    remote branch:                                                                     local   patches:                                                                            reconstruct branch:  01-16 10:49:15.758 3014-3014/com.example.dell.uomopenday d/openglrenderer: enabling debug mode 0 

buttononclick

 button.setonclicklistener(new view.onclicklistener() {  @override  public void onclick(view v) {      search = new arraylist<>();      int points = integer.parseint(edittext.gettext().tostring());     (int = 0; < courses.length; i++) {          course c2 = courses[i];          if (c2.getpoints() <= points) {             search.add(c2);         }      }      course searching [] = new course[search.size()];     searching = search.toarray(searching);      adapter = new listadapter(mainactivity.this, searching);     mylist.setadapter(adapter);  }   string text = edittext.gettext().tostring();  if (text.matches("")) {  button.setenabled(false);  return;    }  }); 

first step, limit input type numbers.

in layout:

android:inputtype="number" 

or in code:

<edittext>.setinputtype(inputtype.type_class_number); 

then validate text number:

final string value = <edittext>.gettext(); if (!stringutils.isnullorwhitespace(value) && value.matches("\\d*")) {     // value number! yay! } else {     // value not number! boo! } 

edit: completeness here stringutils (not necessary):

public class stringutils {     public static final string empty = "";      public static boolean isnullorwhitespace(string value) {         if (value == null) {             return true;         }          (int = 0; < value.length(); i++) {             if (!character.iswhitespace(value.charat(i))) {                 return false;             }         }          return true;     } } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -