Skip to content

Instantly share code, notes, and snippets.

@codinginflow
Last active March 2, 2024 13:15
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codinginflow/9c701d8696c17312add96afe57e82870 to your computer and use it in GitHub Desktop.
Save codinginflow/9c701d8696c17312add96afe57e82870 to your computer and use it in GitHub Desktop.
Open Activity on Button Click Tutorial
package com.example.application.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Activity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.application.myapplication.Activity2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Activity 2" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.application.myapplication.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Activity 1" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:layout_centerHorizontal="true"
android:text="open activity 2" />
</RelativeLayout>
package com.example.application.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
}
@GokulVinod
Copy link

Thanks

@harrynic
Copy link

From a beginner - thanks very much for your clear YouTube explanation and for providing your code on GitHub.

@nethu99
Copy link

nethu99 commented May 8, 2022

thnks

@caltexboy
Copy link

Great tutorials. You are the professor.

@09dian
Copy link

09dian commented Dec 31, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment