• Have Any Question?
  • contact@eitworld.com

Jump to another activity on button click





In android, we can switch to another activity on button click. For this purpose, we have to use the Intent class. Intent is used to switch one activity to another activity or can transfer the data of one activity to another activity. To perform this, first we have to create a button on the activity_main.xml layout file. To create a button, we have to just drag and drop button from the “Form Widgets” from Palette.
After that we have to provide the text and id to the button. To do this, first we have to select the button and press right click of mouse as follows:

android

Now we have to click on Edit ID, now a window will open as follows:

android

Now we have to provide the valid id to the button such as btnNext. Now we have to change the caption of the button. To change the caption of the button, first we have to select the button, and go to the properties window and find the Text property as follows:

android

From this window, we can change the Text of button, id and so on. Now the first activity looks like as follows:

android

Now we want to display message on other activity when we will click on button. Now go to the activity_new_main.xml layout and open it and it looks like as follows:

android

Now we will change the default message “Hello World” to “Welcome to new activity”. To change the text, we have to follow the same steps as button. After changing the text, the activity looks like as follows:

android

Now we have to open the MainActivity.java, when we perform any event we have to follow some rules which are as follows:

  • First we have to provide the unique id for each element which we have dragged and drop on the activity layout file.
  • We have to provide the proper caption to the elements.
  • When we code the java file we have to register the each element in the java file before using it.
  • R.id.<id_of_element> is used to register the elements and it is assigned to same type of object such as we have to register the button which has the id btnNext, now first of all we have to declare a Button class object in the java file then one method (typeOfElement) findViewByID(R.id. <id_of_element>) is used to assign that element to the variable.
  • Button btn;
  • btn = (Button) findViewById(R.id.btnNext);
  • Now we can use this button to perform the action in the java file.

Now we can start the code in java file. Now open the MainActivity.java file. When we declare the Button object then the red bulb indicates that we have not imported the package related to the button such as

android

To import the package related to button, click on this red crossed button then a dialog box will appear such as

android

Now double click on Import ‘Button’ (android.widget), now this package will be import in our java file.

Now we have to register the button in the java file using the button’s id that we have given in the activity_main.xml file.

android

Now we have to create a method which will perform the switching of the activity means jump to another activity. Now we have created a method called moveNext and pass the View class’s object, looks like when declared:

android

In this method, first we have to declare the Intent class’s object, always remember that we have to import the package related to Intent class, to import the package, we have to follow steps as button.

Now we have to initialize this object as follows:

android

In this “new” keyword is used to create the constructor. Here we are creating the constructor of the Intent class that take two parameters, the first parameter specifies the current activity and the 2nd parameter specifies that activity where we want to go when we click on the button.

In this “MainActivity.this” is used to represent the current reference means the current activity or java file in which we are coding, and another “NewMainActivity.class” file represent that activity that will display on button click of the main activity. Here we assigned this value to the “intent” variable of Intent type.

Now we have to call the startActivity(intent) method as follows:

android

This method starts the activity that is hold by the “intent” variable.

Now in the last, we have to call the method. To call the method, we have to go the activity_main.xml file and select the button and go to properties window and find the “onClick” property as follows:

android

Here we have to specify the name of the method as follows:

android

Here is the final code:

activity_main.xml

 

<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=”.MainActivity” ><TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/hello_world” />

<Button
android:id=”@+id/btnNext”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerVertical=”true”
android:layout_marginLeft=”16dp”
android:layout_toRightOf=”@+id/textView1″
android:onClick=”moveNext ”
android:text=”Next” />

</RelativeLayout>

MainActivity.java

 

package com.example.helloandroid;

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

public class MainActivity extends Activity {
Button btn; // declaration of Button object
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// now we have to register the button using the id

btn = (Button) findViewById(R.id.btnNext);
}

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

public void moveNext(View v)
{
Intent intent; // declaration of Intent class object

// now we have to initialize this object
intent = new Intent(MainActivity.this,NewMainActivity.class);

//now we have to call the startActivity() method
startActivity(intent);
}

}

activity_new_main.xml

 

<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=”.NewMainActivity” ><TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”188dp”
android:text=”Welcome to new activity” />

</RelativeLayout>

NewMainActivity.java

 

package com.example.helloandroid;

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

public class NewMainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_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.new_main, menu);
return true;
}

}

Now we are ready to run the application. This is the output:

android

When launched

android

when clicked Next button



Real Time Web Analytics