Opening new Activity from main activity when clicking button

Hi, Guys in this post i will show you how to open new activity when the user clicks the button.Well, before that we have to know about Intent.


What is Intent ? Who is He ?


Hmmmm !!!, He is a intermediate who helps us to call the Activity,Service within same application  and also you can call the activities of other applications.

How to create intent ?


Since Intent is a class we have to create a object for it.
Intent object = new Intent(context,AnotherActivity.class);
startActivity(object);


  • Above is the general syntax to call intent.


Let's Start !


Step-1 : Fire Up ! Android studio.

Step-2 :Create New Project and Name it CallActivity (Or your oun).

1prj

Step-3 : Click Next Select Api-17 as minimum sdk and leave all settings as default. wait until the android studio finish the project setup.

Step-4 : Open the activity_main.xml (Path: app/java/res/layout/activity_main.xml)

Step-5 : Design the layout,delete the default content(HelloWorld Text)  Drag and drop a button from the Virw list.

2pic

Step-6 : Change The default caption(Button) for the button.

3pic

Step-7 : Yeah ! It's Coding time. Give Life to the Button in your Application.open the MainActivity.java.

Step-8 : Declare the Button object and initialize that button in the java code.

Step-9 : Add the button Declaration code above the onCreate() Method.
Button btn;

Step-10 : Add the button initialization code after last line inside the  onCreate() Method.
btn = (Button) findViewById(R.id.button); //Button Initialization

Step-11 :Next important thing is we have to set the OnClickListener for our button to recogonize  the Click Event.
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});

Step-11 : Create New Activity ( FILE-> New -> Activity -> Empty Activity) and name it SecondActivity.

4poc

Step-12 : Add the main Intent code to out button's setOnclickListener -> OnClick() function.
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent secIntent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(secIntent);;
}

Step-13 : Just run and test it !!! ;)

FULL CODE


 package com.codeboltblog.rajakumar.callactivity;

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{
Button btn; //Button Object declaration

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button); //Button Initialization
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent secIntent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(secIntent);;
}
});
}
}

OUTCOME :)


[caption id="attachment_165" align="alignleft" width="281"]5pic MAINACTIVITY[/caption]

[caption id="attachment_168" align="alignleft" width="280"]6pic SECONDACTIVITY[/caption]

 

 

 

 

 

 

 

 

 

 

 

 

Are you liked this tutorial please share it !


Like Us on Facebook :)

Any doubt feel free to ask me !

1 comment:

  1. […] Note : set the id for the controls as following for two EditText  (frstno and secondno respectively), For 4 buttons (addbtn, subbtn, mulbtn and divbtn respectively), For TextView (ans). if you dont know how to change the id propery for a button take a look at this tutorial Opening new Activity from main activity when clicking button :) […]

    ReplyDelete

Powered by Blogger.