Thursday, 5 November 2015

Floating labels in android material design for editText

Hi guys Here I am going to explain Floating Labels for EditText using Android Material Design.

Step 1)AndroidManifest.xml

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

   <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.floatinglabel" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MyMaterialTheme" >
        <activity
            android:name=".MainActivity"
            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>

Step 2)styles.xml

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


  <resources>

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

    </style>

    <style name="MyMaterialTheme.Base"    parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

Step 3)strings.xml

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


<resources>
    <string name="app_name">Floating Labels</string>
    <string name="action_settings">Settings</string>
    <string name="hint_name">Name</string>
    <string name="hint_email">Email</string>
    <string name="hint_password">Password</string>
    <string name="btn_sign_up">Sign Up</string>
    <string name="err_msg_name">Enter your full name</string>
    <string name="err_msg_email">Enter valid email address</string>
    <string name="err_msg_password">Enter the password</string>
</resources>

Step 4)colors.xml

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


<resources>
    <color name="colorPrimary">#457dd7</color>
    <color name="colorPrimaryDark">#443685</color>
    <color name="textColorPrimary">#FFFFFF</color>
    <color name="windowBackground">#FFFFFF</color>
    <color name="navigationBarColor">#000000</color>
    <color name="colorAccent">#800000</color>
</resources>

Step 5)build.gradle


apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "com.floatinglabel"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets { main { res.srcDirs = ['src/main/res', 'src/main/res/ values-v21'] } }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
}
    
Step 6) activity_main.xml




<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="60dp">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/input_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:hint="@string/hint_name" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/input_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="@string/hint_email" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:hint="@string/hint_password" />
        </android.support.design.widget.TextInputLayout>

        <Button android:id="@+id/btn_signup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/btn_sign_up"
            android:background="@color/colorPrimary"
            android:layout_marginTop="40dp"
            android:textColor="@android:color/white"/>

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>


Step 7)Mainactivity.class

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{

    private Toolbar toolbar;
    private EditText inputName, inputEmail, inputPassword;
    private TextInputLayout inputLayoutName, inputLayoutEmail,inputLayoutPassword;      
    private Button btnSignUp;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        inputLayoutName= (TextInputLayout) findViewById(R.id.input_layout_name);
        inputLayoutEmail= (TextInputLayout) findViewById(R.id.input_layout_email);
        inputLayoutPassword= (TextInputLayout) findViewById(R.id.input_layout_password);
        inputName = (EditText) findViewById(R.id.input_name);
        inputEmail = (EditText) findViewById(R.id.input_email);
        inputPassword = (EditText) findViewById(R.id.input_password);
        btnSignUp = (Button) findViewById(R.id.btn_signup);

        inputName .addTextChangedListener(new MyTextWatcher(inputName ));
        inputEmail .addTextChangedListener(new MyTextWatcher(inputEmail ));
        inputPassword .addTextChangedListener(new MyTextWatcher(inputPassword ));

        btnSignUp.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                 if (!validateName()) {
            return;
        }

        if (!validateEmail()) {
            return;
        }

        if (!validatePassword()) {
            return;
        }

       Toast.makeText(getApplicationContext(), "Android dhina", Toast.LENGTH_SHORT).show();
            }
        });
    }

   private boolean validateName() {
        if (inputName.getText().toString().trim().isEmpty()) {
            inputLayoutName.setError("Enter Your Name");
          requestFocus(inputName);
            return false;
        } else {
            inputLayoutName.setErrorEnabled(false);
        }

        return true;
    }

    private boolean validateEmail() {
        String email = inputEmail.getText().toString().trim();

        if (email.isEmpty() || !isValidEmail(email)) {
            inputLayoutEmail.setError("Enter Valid Email");
           requestFocus(inputEmail);
            return false;
        } else {
            inputLayoutEmail.setErrorEnabled(false);
        }

        return true;
    }

    private boolean validatePassword() {
        if (inputPassword.getText().toString().trim().isEmpty()) {
            inputLayoutPassword.setError("Enter Password");
            requestFocus(inputPassword);
            return false;
        } else {
            inputLayoutPassword.setErrorEnabled(false);
        }

        return true;
    }

    private static boolean isValidEmail(String email) 
        {
            return !TextUtils.isEmpty(email)
               &&android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
        }

    private void requestFocus(View view) 
         {
        if (view.requestFocus()) 
        {
         getWindow().setSoftInputMode
         (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }

    private class MyTextWatcher implements TextWatcher
    {

        private View view;

        private MyTextWatcher(View view) {
            this.view = view;
        }

        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        public void afterTextChanged(Editable editable) {
            switch (view.getId()) {
                case R.id.input_name:
                    validateName();
                    break;
                case R.id.input_email:
                    validateEmail();
                    break;
                case R.id.input_password:
                    validatePassword();
                    break;
            }
        }
    }
}

Validate mobile number using regular expressions in android

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

MobileNo = mobileid.getstring();


if(Helper.isValidMobile(MobileNo)){
    Toast.makeText(activity, "Valid", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(activity, "Not Valid", Toast.LENGTH_SHORT).show();
}


public static boolean isValidMobile(String argEditText) {
    try {
        Pattern pattern = Pattern.compile("^[+]?[0-9]{10,13}$");
        Matcher matcher = pattern.matcher(argEditText);
        return matcher.matches();
    } catch (Exception e) {
        if (ConfVars.PRINTEXECEPTOION) {
            e.printStackTrace();
        }
        return false;
    }
}

Validate Email Id using regular expressions in android

Hi guys  Here I am going to explain how to validate  email id  using Regular Expressions

EmailID = emailid.getstring();


if(Helper.isValidEmail(EmailID)){
    Toast.makeText(activity, "Valid", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(activity, "Not Valid", Toast.LENGTH_SHORT).show();
}

public static boolean isValidEmail(String argEditText) {
    try {
        Pattern pattern = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
        Matcher matcher = pattern.matcher(argEditText);
        return matcher.matches();
    } catch (Exception e) {
        if (ConfVars.PRINTEXECEPTOION) {
            e.printStackTrace();
        }
        return false;
    }
}

Read incoming message automatically in android for verify OTP

Hi guys i am going to explain how to read the incoming message in android automatically to verify OTP without closing the app using the BrodcastReceiver

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>

Wednesday, 4 November 2015

How to trigger the button in android without click the button in android

Very simply trigger the button without click the button in oncreate

Button bt1 = (Button)findViewbyId(R.id.btl);
bl.performClick();

bl.setOnClickListener(new OnClickListener() 
         {public void onClick 
         (View  v) { 
             Intent i = new Intent(getApplicationContext(), exampleActivity.class);
             startActivity(i);}
         });

Monday, 12 October 2015

creating a multi select dialog box in android

  1. Create a java Activity class where you want your dialog to appear. 
  2. Create a field     private ArrayList mSelectedItems; for storing your array list.
  3. Call launchAddItems() when you need to create the dialog.
     
  4. Add the following function to create the dialog protected void
  5.  launchAddItems() {
              mSelectedItems = new ArrayList();
              final String items[] = {"Android","Iphone","Nokia"};

              AlertDialog.Builder ab=new AlertDialog.Builder(this);
              ab.setTitle("Dialog Title")
              .setMultiChoiceItems(items, null,  new DialogInterface.OnMultiChoiceClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    if (isChecked){
                          mSelectedItems.add(which);
                    }
                    else if(mSelectedItems.contains(which)){
                        mSelectedItems.remove(Integer.valueOf(which));
                    }
                }
                 
              }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int id) {
                      // User clicked OK, so save the mSelectedItems results somewhere
                      // or return them to the component that opened the dialog
                  }
              }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int id) {
                  }
              });

              ab.create();;
             
           
              ab.show();       
        }