Swap screen (xml) with login button

Asked

Viewed 405 times

-1

I am developing an application. It has the option to login. I wanted to know how to move to another xml layout when login and password validated.

The login system is almost ready. I put in the tab of MainActivity, made a private void (oncreate) setting the button, and after, in a public void the code "IF" I put to validate the login and password and so show that the login was performed. I wanted to know how to switch to another XML screen with the button from login validation.

I’ve tried to use Intents to call another activity within the if rule, but was unsuccessful.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btLogin = (Button) findViewById(R.id.btLogin);
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tLogin = (TextView) findViewById(R.id.tLogin);
                TextView tSenha = (TextView) findViewById(R.id.tSenha);
                String Login = tLogin.getText().toString();
                String Senha = tSenha.getText().toString();
                if(Login.equals("hugo")&&Senha.equals("123")) {
                    alert("Login realizado com sucesso");

                }
                else{
                    alert("Login ou senha incorretos");
                }

            }
        });
    }

    private void alert(String s) {
        Toast.makeText(this,s,Toast.LENGTH_LONG).show();
    }
}

My intention was to put in the keys of the rule if an Intent to call another layout.

  • Hugo, also post the code you are writing, it is easier to help you

  • Okay, I put in the code and what I’m trying to.

1 answer

1

You probably are doing Intent wrong, see this simple example:

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

And you must also remember, to register the Segundaactivity in the Androidmanifest.xml, example:

<activity
     android:name=".SegundaActivity"
     android:theme="@style/AppTheme">
</activity>

In the documentation Android you can see everything as it is done.

  • In the second line of the first code, "startActivity(Intent);" should I put the name of the website I created? startActivity(User connected); The name of the Intent I created was User Connected.class

  • You should put the name you created for your variable Intent, which in the case is there Intent same, but can be anything, for example: Intent qualquer_name = new Intent(this, Segundaactivity.class); and then startActivity(any name);

  • "Error:(16, 23) error: cannot find Symbol variable Intent" I received this error, when I click on it it selects in the java file the word Intent in startActivity(Intent);

  • The variable name must be with minuscule letter, try to put another name in the variable which is not Intent

Browser other questions tagged

You are not signed in. Login or sign up in order to post.