Sunday, July 29, 2018

Starting With Android Studio Part-VI || Login page





Hello friends welcome to Tech Punch.
In this blog we will learn to create Login page
  • Create new project
  • Change layout to Relative layout
  • Drag and drop TextView and change its name to Login
  • Drag and drop plain text for user name
  • Give hint to the plain text i.e. editText as user name
  • Drag and drop password field i.e. editText for password
  • Drag and drop button for login
  • Drag and drop textView for showing attempt of wrong email and password

  • Create new activity so that on successful login new activity will be open

     
package com.developer.sanit.myapplication;

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 {
    EditText uname, psw;
    TextView atmpt;
    public int cntr=5;
    Button b1;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        atmpt=(TextView)findViewById(R.id.textView);;
       atmpt.setText("Attempt : "+cntr);


    }
    public void onclick(View v){
        uname=(EditText)findViewById(R.id.editText3);
        psw=(EditText)findViewById(R.id.editText4);

       b1=(Button)findViewById(R.id.button);
        if((uname.getText().toString()).equals("root")&&(psw.getText().toString()).equals("root")){
            Toast.makeText(this,"Login successful",Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this,homepg.class));
        }
        else{
            cntr--;
            atmpt.setText("Attempt : "+cntr);
            Toast.makeText(this,"Wrong email or password",Toast.LENGTH_SHORT).show();
            if(cntr==0) {

                b1.setEnabled(false);
            }
        }
    }

}

No comments:

Post a Comment