Ad Code

How to Move Intent Android Java Part 5

Table of Contents [Show]


    Hello everyone, welcome back with us, of course, this time we are already in part 5 in learning Android Java and we will learn how to move intents from one place to another.

    What this means is that when we are in the activity login, then we want to move to the activity register, so that's called Intent and we want to try it.

    1. We fill in 📁 res - layout - activity_main.xml

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:app="http://schemas.android.com/apk/res-auto"

        xmlns:tools="http://schemas.android.com/tools"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical"

        android:padding="20dp"

        tools:context=".MainActivity"

        android:background="@color/teal_700">


        <ImageView

            android:layout_width="53dp"

            android:layout_height="97dp"

            android:layout_gravity="center"

            android:layout_marginBottom="20dp"

            android:paddingTop="20dp"

            android:src="@drawable/user" />


        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Login"

            android:textSize="35dp"

            android:layout_gravity="center"

            android:paddingBottom="25dp"

            android:textColor="@color/white"/>


        <!--Input Email-->

        <com.google.android.material.textfield.TextInputLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content">


            <EditText

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:hint="Email"

                android:inputType="textEmailAddress" />

        </com.google.android.material.textfield.TextInputLayout>


        <!--Input Password-->

        <com.google.android.material.textfield.TextInputLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginTop="20dp">


            <EditText

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:hint="Password"

                android:inputType="textPassword" />

        </com.google.android.material.textfield.TextInputLayout>


        <!-- Button Login -->

        <Button

            android:id="@+id/buttonLogin"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginTop="20dp"

            android:backgroundTint="@color/material_dynamic_secondary20"

            android:text="Login" />


        <!-- Button Register -->

        <Button

            android:id="@+id/btnRegister"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:text="Register"

            android:backgroundTint="@color/material_dynamic_secondary20"/>

    </LinearLayout>


    Pay attention to the Button ID register, we created it with the name btnRegister, so when we click the button with the id btnRegister it will do something (event / clickOnListener) which we instruct it to move to the activity register. 



    Next, we create a new activity with the name RegisterActivity, then we edit on the part  📁  res - layout - activity_register.xml

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:app="http://schemas.android.com/apk/res-auto"

        xmlns:tools="http://schemas.android.com/tools"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical"

        android:padding="20dp"

        tools:context=".RegisterActivity"

        android:background="@color/teal_700">


        <ImageView

            android:layout_width="53dp"

            android:layout_height="97dp"

            android:layout_gravity="center"

            android:layout_marginBottom="20dp"

            android:paddingTop="20dp"

            android:src="@drawable/user" />


        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Register"

            android:textSize="35dp"

            android:layout_gravity="center"

            android:paddingBottom="25dp"

            android:textColor="@color/white"/>


        <!--Input Username-->

        <com.google.android.material.textfield.TextInputLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content">


            <EditText

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:hint="Username"

                android:inputType="text" />


        </com.google.android.material.textfield.TextInputLayout>


        <!--Input Email-->

        <com.google.android.material.textfield.TextInputLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content">


            <EditText

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:hint="Email"

                android:layout_marginTop="20dp"

                android:inputType="textEmailAddress" />

        </com.google.android.material.textfield.TextInputLayout>


        <!--Input Password-->

        <com.google.android.material.textfield.TextInputLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginTop="20dp">


            <EditText

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:hint="Password"

                android:inputType="textPassword" />

        </com.google.android.material.textfield.TextInputLayout>



        <!-- Button Register -->

        <Button

            android:id="@+id/buttonRegister"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:text="Register"

            android:backgroundTint="@color/material_dynamic_secondary20"/>


        <TextView

            android:id="@+id/txtLogin"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:text="Sudah daftar ? Silahkan login di sini"

            android:textSize="20sp"

            android:paddingTop="10dp"

            android:textColor="@color/white"></TextView>

    </LinearLayout>

    Pay attention to the TextView with the txtLogin id, now the function is the same as the register button, when we click on the text it will move to the login activity.



    # Create Logic Or Event Or Command

    We enter MainActivity, then you edit it as below:

    package com.geraldprambudi.newblogjava;


    import androidx.appcompat.app.AppCompatActivity;


    import android.content.Intent;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Button;


    public class MainActivity extends AppCompatActivity {


        @Override

        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            

            // 

    we initialize the button btnRegister

            Button btnRegister = findViewById(R.id.btnRegister);

        

            // 

    when we click the button then MainActivity will move to

     RegisterActivity

            btnRegister.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View view) {

                    startActivity(new Intent(MainActivity.this, RegisterActivity.class));

                }

            });

        }

    }

    Next we edit the section RegisterActivity

    package com.geraldprambudi.newblogjava;


    import androidx.appcompat.app.AppCompatActivity;


    import android.content.Intent;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.TextView;


    public class RegisterActivity extends AppCompatActivity {


        @Override

        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_register);

        

            // We do the initialization first

            TextView txtLogin = findViewById(R.id.txtLogin);

            

            // 

    if we click the login text, it will move towards

     MainActivity (LOGIN) Back

            txtLogin.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View view) {

                    startActivity(new Intent(RegisterActivity.this, MainActivity.class));

                }

            });


        }

    }

    Next, let's run it... 


    Post a Comment

    0 Comments

    Close Menu