Android Programming -9 Switching Between Application Pages

Hi guys in this tutorial we will coding switching that between application page.

In my previous tutorial, I talked about the created new java and xml page. If you have not read it yet, I recommend you read it first.

https://ittutorial.org/android-programming-8-create-new-app-page/

package com.example.mac.makale;

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

public class MainActivity extends AppCompatActivity {
EditText kullanici_adi,sifre;
Button login;
TextView mesaj;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

kullanici_adi=(EditText)findviewbyid(R.id.user);
        sifre=(EditText)findviewbyid(R.id.password);
        login=(Buton)findviewbyid(R.id.login);
        mesaj=(TextView)findviewbyid(R.id.text);

login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (kullanici_adi.getText()=="kubrahebes" && sifre.getText()=="12345") {
                    Intent yeni_sayfa =new Intent(MainActivity.this, Anasayfa.Activity);
                    start activity(yeni_sayfa);
                }
                else {
                    mesaj.setText("Başarısız Giriş !!!");
                }
            }
        });
    }
}

If we examine our example;

We have already mentioned this example in the previous article. Today we’re going to go over it and take our existing code a step further. We will redirect users to the new page with the correct username and password. Users who entered their username and password incorrectly will remain on the same page and will show the error message.

 

if we examine coding step by step ;

First of all we define the global variables that we used component which on xml page. Then we use findviewbyid method for each component. we write buton setonclick function for get user action. After that we check variable. if the users enter the right username and password, they will going to the main screen. We will made this action with the intent class. we must create intent class’s object with 2 parameter for using this. First parameter for available screen information, the second parameter is the page to go to.

After creating an object from intent class, We give startActivity as a parameter. In this way, the intent variable that we have created is activated according to the conditions we have mentioned. And it will redirect the user to the next page with the correct username and password.

Kübra Hebeş

Bilgisayar Mühendisi

Leave a Reply

Do NOT follow this link or you will be banned from the site!