Monday, 23 October 2017

Automatic Cursor focus

step 1:

edittext_style.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/white"/>

   <stroke 
    android:width="1dp"        
    android:color="@color/colorBlue" />
</shape>

step 2:

edittext_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:orientation="vertical"    
android:id="@+id/layout_entercode"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:background="@android:color/white">
<LinearLayout    
android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
<EditText    
android:id="@+id/code1"    
android:layout_width="wrap_content"    
android:layout_height="40dp"    
android:ems="2"    
android:maxLength="1"    
android:textStyle="bold"    
android:gravity="center"    
android:inputType="number"    
android:textColor="@android:color/black"    
android:background="@drawable/button_style2"/>
<View    
android:layout_width="10dp"    
android:layout_height="match_parent"/>
<EditText    
android:id="@+id/code2"    
android:layout_width="wrap_content"    
android:layout_height="40dp"    
android:ems="2"    
android:maxLength="1"    
android:textStyle="bold"   
android:inputType="number"    
android:textColor="@android:color/black"    
android:gravity="center"    
android:background="@drawable/button_style2"/>
<View    android:layout_width="10dp"    
android:layout_height="match_parent"/>
<EditText    
android:id="@+id/code3"    
android:layout_width="wrap_content"    
android:layout_height="40dp"    
android:ems="2"    
android:inputType="number"    
android:textStyle="bold"    
android:maxLength="1"    
android:gravity="center"    
android:textColor="@android:color/black"    
android:background="@drawable/button_style2"/>
<View    
android:layout_width="10dp"    
android:layout_height="match_parent"/>
<EditText    android:id="@+id/code4"    
android:layout_width="wrap_content"    
android:layout_height="40dp"    
android:ems="2"    
android:inputType="number"    
android:textStyle="bold"    
android:maxLength="1"    
android:gravity="center"    
android:textColor="@android:color/black"    
android:background="@drawable/button_style2"/>

step 3:

EdittextActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

public class EnterCodeActivity extends AppCompatActivity {
    EditText editText_code1,editText_code2,editText_code3,editText_code4;
    int size=1;
    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edittext);

        editText_code1=(EditText)findViewById(R.id.code1);
        editText_code2=(EditText)findViewById(R.id.code2);
        editText_code3=(EditText)findViewById(R.id.code3);
        editText_code4=(EditText)findViewById(R.id.code4);
        ImageView imageView=(ImageView)findViewById(R.id.send);

        editText_code1.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start,int before, int count)
            {
                // TODO Auto-generated method stub                
                 if(editText_code1.getText().toString().length()==size)     //size as per your requirement                {
                    editText_code2.requestFocus();
                }
            }
            @Override           
          public void afterTextChanged(Editable s) {

            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
            }});
       
         editText_code2.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start,int before, int count)
            {
                // TODO Auto-generated method stub               
                 if(editText_code2.getText().toString().length()==size)     //size as per your requirement                {
                    editText_code3.requestFocus();
                }
            }
            @Override            
               public void afterTextChanged(Editable s) {

            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
            }});

        editText_code3.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start,int before, int count)
            {
                // TODO Auto-generated method stub                
              if(editText_code3.getText().toString().length()==size)     //size as per your requirement                {
                    editText_code4.requestFocus();
                }
            }
            @Override            
          public void afterTextChanged(Editable s) {

            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
            }});
    }
}

No comments:

Post a Comment

HOW TO CREATE CIRCLE IMAGE VIEW

Step 1: implementation 'de.hdodenhof:circleimageview:3.0.0' Step 2: <de.hdodenhof.circleimageview.CircleImageView xmlns:ap...