Validating Email address pattern in Android/Java


We use regular expressions along with Pattern & Matcher classes and their methods to validate email address

Required regular expression is  as below:

String regExpn =
"^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
    +"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
      +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
      +"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
      +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
      +"([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";

Validation code goes as below:

private Boolean isValidEmail(String inputEmail)
{
      Pattern patternObj = Pattern.compile(regExpn);

      Matcher matcherObj = patternObj.matcher(inputEmail/*May be fetched from  EditText. This string is the one which you wanna validate for email*/);
            if (matcherObj.matches())
            {
                  //Valid email id…
                  return true;
}
            Else
{
//not a valid email id…
                  return false;
}
}


Happy coding:-)

Supporting desired fonts in your Android application


To begin with, you should have desired True Face Font file(*.ttf).

If you are having *.ttf  file then
1.        open your project in eclipse and copy  * .ttf  file into the  assets  folder, which will be in your project hierarchy.

2.        open  *.java file(which extends Activity) where you want to use the desired  font. Decalare necessary variables. In my example, I’m using a TextView and an EditText(In case of EditText, probably you should have a corresponding soft key board in your  desired font)
 Typeface font1;
 TextView txt1;
 EditText edtxt;

3.         In onCreate() method just write down this code.

/*here fonttype.ttf is the desired True Face Font file which was copied in assets folder.*/
font1 =Typeface.createFromAsset(getAssets(),"fonttype.ttf");
txt1 =(TextView) findViewById(R.id.user_id_txt);
edtx = (EditText) findViewById(R.id.editText);
//Set new type face to the views
txt1.setTypeface(font1);
edtx.setTypeface(font1);


That’s it.
Happy coding :-)

Handling SOAP in Android

In this post we will go through a few steps to be followed to handle SOAP web services for Android, which are created in  .Net


Below are the steps I have followed to handle web services in Android using KSOAP. However  WSClient++  is cool too.


1.   Download the KSOAP library (.jar file) from  below link.

      KSOAP2 [In the page opened by this link, Click on "view raw file" to get jar file]

2.   Add the downloaded jar file to your application build path by following below instructions:

      Right click on your application name in the package explorer> Click on “Build Path”>Click on “Configure Build Path”>Click on “Java Build Path” from the left most panel from new window opened> Click on “Libraries”> Click on “Add External jars” from list of buttons located in right panel> Browse for the downloaded jar file(ksoap2-android-assembly-2.3-jar-with-dependencies.jar) just downloaded.>Click on “OK”.

3. My calss which handles SOAP request and response looks like below:

package org.sample.code;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class Main extends Activity {
    /** Called when the activity is first created. */
     
      //You can get SOAP_ACTION from the
      //sample SOAP xml structure for the corresponding operation.
      private static final String SOAP_ACTION    ="http://tempuri.org/CelsiusToFahrenheit";

      private static final String METHOD_NAME ="CelsiusToFahrenheit";

      private static final String NAMESPACE ="http://tempuri.org/";

      //Make sure URL ends with ".asmx"
      private static final String URL ="http://www.w3schools.com/webservices/tempconvert.asmx";

     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      
        TextView tv=(TextView)findViewById(R.id.textview);
       
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
       
        //Add the parameters to be paused..
        request.addProperty("Celsius", "32");
        //In case of multiple parameters,
        // request.addProperty("param2", "val2");
  // val2 with in double quote,if val2 has to be passed as String
        // request.addProperty("param3", "val3");
  // val3 with in double quote,if val3 has to be passed as String
        //etc..
       
        SoapSerializationEnvelope mySoapEnvelop = new   SoapSerializationEnvelope(SoapEnvelope.VER11);
       
        //Set this true, iff its created using .Net webservice.
        mySoapEnvelop.dotNet=true;
      
        mySoapEnvelop.setOutputSoapObject(request);
       
        AndroidHttpTransport myAndroidHttpTransport  = new AndroidHttpTransport(URL);
       
        try
        {
            myAndroidHttpTransport.call(SOAP_ACTION, mySoapEnvelop);
            SoapPrimitive resultString = (SoapPrimitive) mySoapEnvelop.getResponse();
//You may parse "resultStrng" as you wish..
            tv.setText("result = "+ resultString);
        }
        catch( Exception e )
        {
            Log.d("ERROR" , "exception e = "+ e.getMessage());
            tv.setText("result = "+ e.toString());
        }
    }
}





4. Add the below permission to your manifest file, just before closing tag of “manifest” :
<uses-permission android:name="android.permission.INTERNET" />


Above code is for sample and working with little modifications.

Feel free to post comments/doubts back.

I should be thankful to Falafel(http://vimeo.com/9633556) for sharing nice video for the same.


Happy coding :-)