Help with login in android studio

0

Sorry for the beginner, but I'm recently introducing myself in this world of programming, what happens is that I want to redirect the activity login to another activity but I do not know how, when logging in correctly, the user is validated but not redirect to another activity, why will it be?

What I mainly seek is that after sending the message that the user logged in correctly, send me directly to another activity.

Login.java:

    package com.example.asus.crudaplicacion;

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.Toast;

public class Login extends AppCompatActivity {
    EditText e1,e2;
    Button b1,b2;
    DatabaseHelper db;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        db=new DatabaseHelper(this);
        e1=(EditText)findViewById(R.id.usuario);
        e2=(EditText)findViewById(R.id.password);
        b1=(Button)findViewById(R.id.loguear);
        b2=(Button)findViewById(R.id.segunda);

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

                String email=e1.getText().toString();
                String password=e2.getText().toString();
                Boolean Chkemailpass=db.emailpassword(email,password);
                if(Chkemailpass==true)
                    Toast.makeText(getApplicationContext(),"Ha iniciado sesión correctamente",Toast.LENGTH_SHORT).show();

                else
                    Toast.makeText(getApplicationContext(),"Email o contraseña incorrectos",Toast.LENGTH_SHORT).show();
            }

        });


    }
}

activity_login.xml:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="48dp"
    android:layout_marginEnd="8dp"
    android:text="LOGIN"
    android:textSize="60sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/usuario"
    android:layout_width="330dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="24dp"
    android:layout_marginEnd="8dp"
    android:ems="10"
    android:hint="Escriba su email..."
    android:inputType="textPersonName"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.21"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView2" />

<EditText
    android:id="@+id/password"
    android:layout_width="330dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="8dp"
    android:ems="10"
    android:hint="Escriba su password..."
    android:inputType="textPassword"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.21"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/usuario" />

<Button
    android:id="@+id/loguear"
    android:layout_width="288dp"
    android:layout_height="54dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="60dp"
    android:layout_marginEnd="8dp"
    android:text="Iniciar sesión"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/password" />

    
asked by nicolasyo1WWE 24.10.2018 в 06:54
source

2 answers

1

The problem is that nowhere do you indicate that you have to move on to the next activity, for this you have to use an Intent.

Here is a script to see everything in detail: link

According to your code, it would look like this:

package com.example.asus.crudaplicacion;

import android.content.Intent;//Esta es la librería para poder utilizar el 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.Toast;

public class Login extends AppCompatActivity {
    EditText e1,e2;
    Button b1,b2;
    DatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        db=new DatabaseHelper(this);
        e1=(EditText)findViewById(R.id.usuario);
        e2=(EditText)findViewById(R.id.password);
        b1=(Button)findViewById(R.id.loguear);
        b2=(Button)findViewById(R.id.segunda);

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

                String email=e1.getText().toString();
                String password=e2.getText().toString();
                Boolean Chkemailpass=db.emailpassword(email,password);
                if(Chkemailpass==true){ //Añadimos llaves ya que va a haber más de una línea de código
                    Toast.makeText(getApplicationContext(),"Ha iniciado sesión correctamente",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(this, OtraActividad.class);  //Creamos el intent y le indicamos desde donde vamos (this) y a donde vamos (OtraActividad.class)
                    startActivity(intent);  //Abrimos la otra actividad
                }else
                    Toast.makeText(getApplicationContext(),"Email o contraseña incorrectos",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
    
answered by 24.10.2018 / 07:24
source
1
public void onClick(View v) {

            String email=e1.getText().toString();
            String password=e2.getText().toString();
            Boolean Chkemailpass=db.emailpassword(email,password);
            if(Chkemailpass==true)
                Toast.makeText(getApplicationContext(),"Ha iniciado sesión correctamente",Toast.LENGTH_SHORT).show();

Intent intent = new Intent (getApplicationContext (), NAME OF YOUR CLASS ACTIVITY.class);             startActivity (intent);

            else
                Toast.makeText(getApplicationContext(),"Email o contraseña incorrectos",Toast.LENGTH_SHORT).show();
        }

    });

Only enter this line of code to send you to the second activity You must add the library of the Intent with the keys alt-enter or directly import import android.content.Intent; I hope you have explained, it is useful and useful.

    
answered by 24.10.2018 в 07:05