Create and Login application as above. Validate login data and display Error to user using setError() method.

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>

Activity_Second.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/Secondform"
        tools:context=".Second" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:text="@string/Logout" />

</RelativeLayout>

MainActivity.java
package com.example.loginform;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class MainActivity extends Activity implements OnClickListener{
           
            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);
        pass=(EditText )findViewById(R.id.editText2);
        login=(Button )findViewById(R.id.button1);
        cancel=(Button )findViewById(R.id.button2);
        login.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

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

            @SuppressLint("ShowToast") @Override
            public void onClick(View v) {
                        String na=name.getText().toString();
                        String pa=pass.getText().toString();
                        switch(v.getId())
                        {
                        case R.id.button1:
                                    if(na.equals("chaitu"))
                                    {         
                                                if( pa.equals("root"))
                                                {
                                                            Intent i = new Intent(this,Second.class);
                                                            i.putExtra("c1", na);
                                                            startActivity(i);
                                                }
                                                else
                                                {
                                                            pass.setError("Invalid Password !");
                                                }
                                    }
                                    else
                                    {
                                                            name.setError("Invalid UserName !");                                                                       
                                    }
                                    break;
                       
                        case R.id.button2:
                                    name.setText("");
                                    pass.setText("");
                                    break;
                                   
                        default:
                                   
                           break;
                        }
                       
                        }

}

Second.java
package com.example.loginform;
import com.example.loginform.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;
import android.content.*;

public class Second extends Activity implements OnClickListener{
           
            Button Logout;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_second);
                        Intent i=getIntent();
                        String str = i.getStringExtra("c1");
                        TextView t1 = (TextView )findViewById(R.id.textView1);
                        t1.setText("Logged In Successfully ! "+str);
                        Logout=(Button )findViewById(R.id.button1);
        Logout.setOnClickListener(this);
            }

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

            @Override
            public void onClick(View v1) {
switch(v1.getId())
                        {
                        case R.id.button1:
                                    Intent i = new Intent(this,MainActivity.class);
                                    startActivity(i);
                                    finish();
                                    System.exit(0);
                                    break;
                       
                        default:
                                    break;
                        }
                       
            }


}

Post a Comment

0 Comments