Monday, 22 June 2015

Android single line for custom edittext background


   Here I am going to explain how to set custom background for edittext as single
   line


  

  Step 1).res->drawable->customedittext.xml

  <?xml version="1.0" encoding="utf-8"?>

  <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
   <item
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke
                android:width="1dp"
                android:color="#f45ccc" />
        </shape>
    </item>

</layer-list>



  Step 2).Edittext

  
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:background="@drawable/customedittext"
        android:ems="10"  />



Happy Coding:)

     
  

      

Wednesday, 17 June 2015

Reading incoming message automatically in android to verify OTP

Here I am going to explain how to read the incoming message in android automatically to verify OTP without closing the app.


Step 1).IncomingSms.class

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

public class IncomingSms extends BroadcastReceiver 
{
  @Override
public void onReceive(Context context, Intent intent) 
{

final Bundle bundle = intent.getExtras();
  try {
  if (bundle != null) 
{
   final Object[] pdusObj = (Object[]) bundle.get("pdus");
   for (int i = 0; i < pdusObj .length; i++) 
 {
    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[])                                                                                                    pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum phoneNumber ;
String message currentMessage .getDisplayMessageBody();
    try
  {
   if (senderNum .equals("TA-DOCOMO")) 
   {
Otp Sms = new Otp();
Sms.recivedSms(message );
   }
}
catch(Exception e){}

}
  }

} catch (Exception e) 
{
                
 }
}

}



Step 2).Otp.class

   public class Otp extends Activity 
   {

      static EditText OtpNumber;
       protected void onCreate(Bundle savedInstanceState) 
         {
super.onCreate(savedInstanceState);
setContentView(R.layout.otp);
                OtpNumber= (EditText) findViewById(R.id.txtName);
        }
      public void recivedSms(String message
       {
try 
         {
OtpNumber.setText(message);
       } 
         catch (Exception e) 
             {         
                   }
      }

       }   

Step 3).Add the below permissions and receiver to your manifest file


    <uses-permission android:name="android.permission.RECEIVE_SMS" >
    </uses-permission>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" >
     </uses-permission>

    <receiver android:name=".IncomingSms">
        <intent-filter>
         <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>


Tuesday, 9 June 2015

Validate mobile number using regular expressions in android


Here I am going to explain how to validate  Mobile number using Regular Expressions

  String Mobile="900*******";

  String MobileValid = "^[+]?[0-9]{10,13}$";

   if (Mobile.matches(MobileValid))
{
                Toast.makeText(activity, Valid, Toast.LENGTH_SHORT).show();

}
else
{
Toast.makeText(activity, InValid, Toast.LENGTH_SHORT).show();
}



Happy Coding:)

Validate an email address using regular expression in android


  Here I am going to explain how to validate an E-mail address using Regular    Expressions

  String Name="dhinakaran.cool@gmail.com";

  String EmailPattern = "^(([\\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})$";

   if (Name.matches(EmailPattern))
{
    Toast.makeText(activity, Valid, Toast.LENGTH_SHORT).show();

}
else
{
Toast.makeText(activity, InValid, Toast.LENGTH_SHORT).show();
}



Happy Coding:)