0

Need help with switching to an activity if my form details match the string. So when the login button is pressed it will display main activity. This is the code I have so far:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    setupVariables();
}

private void setupVariables() {
    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    login = (Button) findViewById(R.id.button);

}

public void Login(View view) {
    if (username.getText().toString().equals("admin") && password.getText().toString().equals("secret")) {
        /* switches activity if the username and password matches the string */
    }
    else {

    }
}

}

DeeMoMo
  • 35
  • 1
  • 6

5 Answers5

0

Supposing the Login as the onClick function of the login button, you can use Intent like following:

Intent intent = new Intent(CurrentActivity.this, nextActivity.class);
startActivity(intent);

You are good to go!

Amir_P
  • 8,322
  • 5
  • 43
  • 92
0

you will it like this

    login.setOnClickListener(new View.OnclickListener(){
     @override
     public void onClick(View v){
      Login();
    }
    });
public void Login() {
    if (username.getText().toString().equals("admin") && password.getText().toString().equals("secret")) {
     Intent intent = new Intent(this,NewActivity.class);
startActivity(intent);
    }
    else {  
    Toast.makeText(this,"Cant't Login",Lenght.Short).show();

    }
Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
0

use this onclick method

login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 if (username.getText().toString().equals("admin")&&password.getText().toString().equals("secret")) {
                Intent intent = new Intent(Login.this, MainActivity.class);
                startActivity(intent);
                }
               else {  
                Toast.makeText(getApplicationContext(), "Wrong username or password", 
 Toast.LENGTH_LONG).show();
               }
            }
        });

I am assuming that Login.java is your current class EDIT--> try this

login.setOnClickListener(new OnClickListener(){
    @Override
    //On click function
    public void onClick(View view) {
if (username.getText().toString().equals("admin")&&password.getText().toString().equals("secret"))
{    
        //Create the intent to start another activity
        Intent intent = new Intent(Login.this, MainActivity.class);
        startActivity(intent);
    }
else {
Toast.makeText(this, "Wrong username or password", Toast.LENGTH_SHORT).show();       
}
});
P Sharma
  • 184
  • 9
  • I am receiving errors on can't resolve symbol for setOnClickListener, the v in (View v) and LENGTH_LONG @Praval Sharma – DeeMoMo Apr 02 '16 at 16:49
  • I edited my answer and paste another code for your errors @DeeMoMo – P Sharma Apr 02 '16 at 17:02
  • Again, now have an can't resolve symbol for 'button' at the beginning. The (new OnClickListener() has an error. And both (View view) and (view.getContext() have errors. – DeeMoMo Apr 02 '16 at 17:05
  • you have to use 'login' instead of 'button' because your 'login' is your button object and replace view.getcontext() with 'Login.this'. Here I am assuming Login is your current class. – P Sharma Apr 02 '16 at 17:14
  • Also i have changed code for your convenience @DeeMoMo – P Sharma Apr 02 '16 at 17:16
0

For this please use "Intent" class, used to move from one activity to another activity or service.

Intent intent=new Intent(first_activity.class, Second_activity.class);
startactivity(intent);

or you can use

Intent intent=new Intent(getapplicationcontext(),Second_activity.class);
startactivity(intent) 
NIKHIL SONI
  • 84
  • 1
  • 1
0

For Switching and working with activity.

You first might read details for this. Follow this.

http://developer.android.com/training/basics/firstapp/starting-activity.html

And try this with your code.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    setupVariables();

}

private void setupVariables() {
    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    login = (Button) findViewById(R.id.button);
    login.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           Login();
       }
    });

}

public void Login() {
    if (username.getText().toString().equals("admin") && password.getText().toString().equals("secret")) {
        // Here you can switch your activity.
        Intent i = new Intent(getApplicationContext(), SecondScreen.class);
        StartActivity(i);
    }
    else {
        Toast.makeText(this,"Invalid credentials.",Toast.LENGTH_SHORT).show();
    }
}

And you not need to pass view parameter as you need to call it directly on Button Click Listener method.

Edit

Toast.makeText(this,"Invalid credentials.",Toast.LENGTH_SHORT).show();

Hope this will help you to understand basics.

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58