Create login application where you will have to validate username and passwords Till the username and password is not validated , login button should remain disabled.

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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="56dp"
        android:text="@string/Username"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:inputType="text" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="48dp"
        android:text="@string/Password"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/editText1"
        android:ems="10"
        android:inputType="textPassword" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="68dp"
        android:layout_toLeftOf="@+id/editText2"
        android:text="@string/Login" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignLeft="@+id/editText2"
        android:layout_marginLeft="42dp"
        android:text="@string/Cancel"/>

</RelativeLayout>

MainActivity.java
package com.example.loginform;

import com.example.loginform.R;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class MainActivity extends Activity implements OnClickListener,TextWatcher{
           
            EditText name;
            EditText pass;
            Button login;
            Button cancel;
           
           
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name=(EditText )findViewById(R.id.editText1);
        name.addTextChangedListener(this);
        pass=(EditText )findViewById(R.id.editText2);
        pass.addTextChangedListener(this);
        login=(Button )findViewById(R.id.button1);
        cancel=(Button )findViewById(R.id.button2);
        login.setOnClickListener(this);
        cancel.setOnClickListener(this);
        login.setEnabled(false);
        cancel.setEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

            @SuppressLint("ShowToast") @Override
            public void onClick(View v) {
                                                                                               
            }

            public void afterTextChanged(Editable arg0) {
                       
                       
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
                       
                       
            }

            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                       
                        String na=name.getText().toString();
                        String pa=pass.getText().toString();
                       
                        if(na.equals("chaitu") && pa.equals("root")) 
                        {
                                    Button btn = (Button) findViewById(R.id.button1);
                                    btn.setEnabled(true);
                        }
            }
   }


Post a Comment

0 Comments