How to call From First Activity to second Activity using Intent


How to call another activity



Go to File -> Open New Select Activty after the selection give activity name for example NewActivity
1.in Activity_Home Drag Button in XML file
Give Button id 
For Example: android:id="@+id/id_btn_click"
<RelativeLayout 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"
    tools:context=".Home">

    <Button
        android:id="@+id/id_btn_click"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Click" />

</RelativeLayout>


 2. Now open  Home Class
Import  Button;
Now Java Code is

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

public class Home extends AppCompatActivity {

    Button btn_click;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        btn_click=findViewById(R.id.id_btn_click);
        btn_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent newIntent = new Intent(Home.this, NewActivity.class);
                startActivity(newIntent);

            }

        });

    }

}

Thank You so much J

Post a Comment

Previous Post Next Post