Create sample application with login module. (Check username and password) On successful login, Change TextView “Login Successful”. And on failing login, alert user using Toast “Login fail”

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") && pa.equals("root") || pa.equals("admin")){
                                                            Intent i = new Intent(this,Second.class);
                                                            i.putExtra("c1", na);
                                                            startActivity(i);
                                                }
                                                else
                                                {
                                    Toast.makeText(this,"check #username or #Password", 3600).show();                                                                     
                                                }
                                    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

1 Comments

  1. AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\flipkart\\AndroidStudioProjects\\186490316068_02\\app\\src\\main\\res\\layout\\activity_main.xml","position":{"startLine":6}}],"original":"C:\\Users\\flipkart\\AndroidStudioProjects\\186490316068_02\\app\\src\\main\\res\\layout\\activity_main.xml:7: AAPT: error: resource string/Username (aka com.example.a186490316068_02:string/Username) not found.\n ","tool":"AAPT"}
    AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\flipkart\\AndroidStudioProjects\\186490316068_02\\app\\src\\main\\res\\layout\\activity_main.xml","position":{"startLine":25}}],"original":"C:\\Users\\flipkart\\AndroidStudioProjects\\186490316068_02\\app\\src\\main\\res\\layout\\activity_main.xml:26: AAPT: error: resource string/Password (aka com.example.a186490316068_02:string/Password) not found.\n ","tool":"AAPT"}
    AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\flipkart\\AndroidStudioProjects\\186490316068_02\\app\\src\\main\\res\\layout\\activity_main.xml","position":{"startLine":48}}],"original":"C:\\Users\\flipkart\\AndroidStudioProjects\\186490316068_02\\app\\src\\main\\res\\layout\\activity_main.xml:49: AAPT: error: resource string/Login (aka com.example.a186490316068_02:string/Login) not found.\n ","tool":"AAPT"}
    AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\flipkart\\AndroidStudioProjects\\186490316068_02\\app\\src\\main\\res\\layout\\activity_main.xml","position":{"startLine":57}}],"original":"C:\\Users\\flipkart\\AndroidStudioProjects\\186490316068_02\\app\\src\\main\\res\\layout\\activity_main.xml:58: AAPT: error: resource string/Cancel (aka com.example.a186490316068_02:string/Cancel) not found.\n ","tool":"AAPT"}
    AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\flipkart\\AndroidStudioProjects\\186490316068_02\\app\\src\\main\\res\\layout\\activity_second.xml","position":{"startLine":5}}],"original":"C:\\Users\\flipkart\\AndroidStudioProjects\\186490316068_02\\app\\src\\main\\res\\layout\\activity_second.xml:6: AAPT: error: resource string/Secondform (aka com.example.a186490316068_02:string/Secondform) not found.\n ","tool":"AAPT"}
    AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\flipkart\\AndroidStudioProjects\\186490316068_02\\app\\src\\main\\res\\layout\\activity_second.xml","position":{"startLine":14}}],"original":"C:\\Users\\flipkart\\AndroidStudioProjects\\186490316068_02\\app\\src\\main\\res\\layout\\activity_second.xml:15: AAPT: error: resource string/Logout (aka com.example.a186490316068_02:string/Logout) not found.\n ","tool":"AAPT"}

    Execution failed for task ':app:processDebugResources'.
    > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
    > Android resource linking failed
    C:\Users\flipkart\AndroidStudioProjects\186490316068_02\app\src\main\res\layout\activity_main.xml:7: AAPT: error: resource string/Username (aka com.example.a186490316068_02:string/Username) not found.

    ReplyDelete