Open new activity on button click in android by existing activity

Opening another activity through Intent function.

On every android application there Intent used to open new activity Without intent function developer cannot open another activity. Intent function is used like Anchor tags or you can say that Intent is anchor tag of android applications. So here is the complete step by step tutorial for Open new activity on button click in android by existing activity.

android-project-download-code-button

Open new activity on button click in android by existing activity.

Note: Please add new android activity on your project if your are using Android studio then Here is complete tutorial for How to Add new activity on android studio.

Code for First MainActivity.java file.

 package com.android_examples.com.opennewactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

 Button GoToNewActivity;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 GoToNewActivity = (Button)findViewById(R.id.button1);
 
 GoToNewActivity.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 // Intent code for open new activity through intent.
 
 Intent intent = new Intent(MainActivity.this, SecondMainActivity.class);
 startActivity(intent);
 
 }
 });
 }
}

Code for First activity_main.xml layout file.

 <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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.opennewactivity.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="149dp"
 android:text="This is First Activity."
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="27dp"
 android:text="Click Here to Open Second Activity." />

</RelativeLayout>

Code for Second MainActivity.java file.

 package com.android_examples.com.opennewactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class SecondMainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_second_main);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.second_main, menu);
 return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 // Handle action bar item clicks here. The action bar will
 // automatically handle clicks on the Home/Up button, so long
 // as you specify a parent activity in AndroidManifest.xml.
 int id = item.getItemId();
 if (id == R.id.action_settings) {
 return true;
 }
 return super.onOptionsItemSelected(item);
 }
}

Code for Second activity_main.xml layout file.

 <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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.opennewactivity.SecondMainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="This is The Second Activity."
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshots :

Open new activity on button click in android by existing activity

second activity

Click Here to Download Open new activity on button click in android by existing activity project.

Leave a Reply

Your email address will not be published. Required fields are marked *