GTU MCA SLMC Program 3 Android OnKeyListener Example

GTU MCA SLMC Program 3 Create login application where you will have to validate EmailID(UserName). Till the username and password is not validated , login button should remain disabled.

GTU MCA SLMC Program 3

Validation is must for any kind of applications, Here I have explained validation using OnKeyListener() of the EditText Control of Android. In Android EditText Control is used to get user input, suppose If you are accepting some values from the user and you want to validate on the fly untill user enter exact correct values. If user enter correct values then you would like to move to next operation in this situation you can use following code.

Jump to SLMC Program 4

Location : res\layout

FileName: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="gtu.moc.prog03.MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="31dp"
        android:hint="Username"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:text="Login" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Login"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:text="Developed By: Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

 

Location : src\package\

FileName : MainActivity.java

package gtu.moc.prog03;

/*
 * GTU MCA SLMC Program 3
 *
 * Create login application where you will have to validate EmailID(UserName). Till the 
 * username and password is not validated , login button should remain disabled.   
 * 
 */
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	EditText et1,et2;
	Button b1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		b1 = (Button) findViewById(R.id.button1);
		et1 = (EditText) findViewById(R.id.editText1);
		et2 = (EditText) findViewById(R.id.editText2);
		
		b1.setEnabled(false);
		
		et1.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
				// TODO Auto-generated method stub

				if(et1.getText().toString().equals("adarsh") && et2.getText().toString().equals("adarsh") )
				{
					b1.setEnabled(true);
				}
				else
				{
					b1.setEnabled(false);	
				}
				
				return false;
			}
		});
		
		
		et2.setOnKeyListener(new View.OnKeyListener() {
			@Override
			public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
				// TODO Auto-generated method stub

				if(et1.getText().toString().equals("adarsh") && et2.getText().toString().equals("adarsh") )
				{
					b1.setEnabled(true);
				}
				else
				{
					b1.setEnabled(false);
				}
				
				return false;
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

Output 1

GTU MCA SLMC Program 3

 

Output 2

GTU MCA SLMC Program 3

Leave a Reply