Pages

Thursday, May 5, 2016

Android Login Example Without Database

MainActivity.java
package kantapp.androidloginexamplewithoutdatabase;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{

    Button login,exit;
    EditText username,pass;
    TextView counts;
    int count=3;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onLoginClick();
        onExitClick();
    }

    public  void onLoginClick()
    {
        counts=(TextView)findViewById(R.id.count);

        username=(EditText)findViewById(R.id.username);

        pass=(EditText)findViewById(R.id.pass);

        login=(Button)findViewById(R.id.login);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {

                if(username.getText().toString().equals("admin")&&pass.getText().toString().equals("1234"))
                {
                    startActivity(new Intent(getApplicationContext(),Welcome.class));
                }
                else
                {
                    Toast.makeText(MainActivity.this, 
                            
                            "Your ID or Password is Wrong Please Enter Again",
                            
                            Toast.LENGTH_SHORT).show();
                }
                count--;

                counts.setText(Integer.toString(count));
                if(count==0)
                {
                    login.setEnabled(false);

                }
            }
        });
    }

    public void onExitClick()
    {
        exit=(Button)findViewById(R.id.exit);
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    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="kantapp.androidloginexamplewithoutdatabase.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="Login First"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="User Name" />

    <EditText
        android:id="@+id/pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Login" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Exit"
            android:id="@+id/exit" />
    </LinearLayout>


    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You Allow wrong Attempt only 3 time"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Welcome.java
package kantapp.androidloginexamplewithoutdatabase;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Welcome extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        Button button=(Button)findViewById(R.id.back);
         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v)
             {
                 startActivity(new Intent(getApplicationContext(),MainActivity.class));
                 finish();
             }
         });
    }
}

activity_welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<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="kantapp.androidloginexamplewithoutdatabase.Welcome"
    android:background="#d6d7d7">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:layout_marginTop="129dp"
        android:src="@mipmap/ic_launcher"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Welcome"
        android:paddingBottom="20dp"
        android:id="@+id/textView3"
        android:layout_alignBottom="@+id/imageButton"
        android:layout_toRightOf="@+id/imageButton"
        android:layout_toEndOf="@+id/imageButton" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Back"
        android:id="@+id/back"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageButton"
        android:layout_toEndOf="@+id/imageButton" />

</RelativeLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kantapp.androidloginexamplewithoutdatabase">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Welcome"></activity>
    </application>

</manifest>

No comments:

Post a Comment