Saturday, April 19, 2014

Android Form Validation

This example show how to prevent null and blank field. Here username field check whether the submitted value is null or some blank space. If the submitted value is null or blank it displays a error message. Another amount field prevent user from entering negative value in the field, it simply uses android:inputType="numberDecimal" to prevent user to enter negative value. Likewise you can limit input size with android:maxLength="20", this limits text size to 20 characters.

Click Here to download Source Code

Form Validation
Fig: Form Validation

FormValidationExampleActivity.java

 package com.rnd.action;  
 import java.util.regex.Matcher;  
 import java.util.regex.Pattern;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.util.Log;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.Toast;  
 public class FormValidationExampleActivity extends Activity{  
   /** Called when the activity is first created. */  
      EditText username, amount;  
       Button btn;  
      @Override  
   public void onCreate(Bundle savedInstanceState)  
      {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.main);  
        username = (EditText) findViewById(R.id.username);  
     amount = (EditText) findViewById(R.id.amount);  
        btn = (Button) findViewById(R.id.button1);  
     btn.setOnClickListener(new View.OnClickListener() {  
                public void onClick(View v) {  
                     Log.d("Checking value of textbox", amount.getText().toString());  
               if(formValidator(username.getText().toString()) == true)  
               {  
                    Toast.makeText(FormValidationExampleActivity.this, "Valid Data", Toast.LENGTH_LONG).show();  
               }  
               else  
               {  
                    Toast.makeText(FormValidationExampleActivity.this, "Name field is required", Toast.LENGTH_LONG).show();  
               }  
                }  
           });  
      }  
      public boolean formValidator(String values)  
      {  
           if((values.trim()).equals("") || values.equals(null))  
           {  
                return false;  
           }  
           else  
           {  
                return true;  
           }  
      }  
      public void checkemail(String email)  
      {  
        Pattern pattern = Pattern.compile(".+@.+\\.[gmail.com]+");  
        Matcher matcher = pattern.matcher(email);  
        boolean emailcheck = matcher.matches();  
      }  
 }  

Main.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:orientation="vertical" >  
   <TextView  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:text="@string/hello" />  
   <EditText  
     android:id="@+id/username"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:ems="10"  
      >  
     <requestFocus />  
   </EditText>  
   <EditText  
     android:id="@+id/amount"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:ems="10"  
     android:inputType="numberDecimal" />  
   <Button  
     android:id="@+id/button1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Ok" />  
 </LinearLayout>  

Friday, April 4, 2014

Android ListView onItemClick Event

Here are the output screen of the application where users are able to insert their data through a textbox by clicking ok button as shown in image below. After adding the items users are able to select the items via clicking on it and the selected item is displayed via a toast.
Click Here to download source folder




 

ListExampleActivity.java

 package com.rnd.action;  
 import java.util.ArrayList;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.util.Log;  
 import android.util.SparseBooleanArray;  
 import android.view.KeyEvent;  
 import android.view.MotionEvent;  
 import android.view.View;  
 import android.widget.AdapterView;  
 import android.widget.ArrayAdapter;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.ListView;  
 import android.widget.Toast;  
 public class ListExampleActivity extends Activity {  
   /** Called when the activity is first created. */  
   ListView lv;  
   String[] outputStrArr;  
   ArrayAdapter<String> aa = null;  
      @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.main);  
     lv = (ListView) findViewById(R.id.listView_user);  
     final EditText myEditText = (EditText) findViewById(R.id.myeditText);  
     final ArrayList<String> todoItems = new ArrayList<String>();  
     aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,todoItems);  
     lv.setAdapter(aa);  
     myEditText.setOnKeyListener(new View.OnKeyListener() {  
                public boolean onKey(View v, int keyCode, KeyEvent event) {  
                     // TODO Auto-generated method stub  
                     if(event.getAction()== KeyEvent.ACTION_DOWN)  
                     {  
                          if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER)  
                          {  
                               todoItems.add(0,myEditText.getText().toString());  
                               aa.notifyDataSetChanged();  
                               myEditText.setText("");  
                               return true;  
                          }  
                     }  
                     return false;  
                }  
           });  
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
                          long arg3) {  
                     //Log.i("Selected Item in list", arg1.toString());  
                     String test = (String) lv.getAdapter().getItem(arg2);  
                     Log.i("Selected Item in list", test);  
                     Toast.makeText(ListExampleActivity.this,test ,Toast.LENGTH_LONG);  
                }  
           });  
     }  
 }  

Main.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:orientation="vertical" >  
   <TextView  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:text="@string/hello" />  
   <EditText  
     android:id="@+id/myeditText"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:ems="10" >  
     <requestFocus />  
   </EditText>  
   <ListView  
     android:id="@+id/listView_user"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:clickable="true"   
      >  
   </ListView>  
 </LinearLayout>  

 

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.rnd.action"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk android:minSdkVersion="8" />  
   <application  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name" >  
     <activity  
       android:name=".ListExampleActivity"  
       android:label="@string/app_name" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>