In this android application we will learn how to open another activity of our project. We will use Explicit intent to open another activity first we'll lean what is intent than what is explicit intent:
Intent:- Intent is an android service that gives us the facilities of accessing one activity from the other activity. Basically, There are two types of Intent:
1) Implicit Intent
2) Explicit Intent
Explicit Intent:- Accessing one user define activity from the other activity like open new page of the application.
Here we are giving example of Implicit intent. So, create new project and drop one text view and one button. The code of android XML files is given below:
(Main XML file:- activity_main.xml)
Intent:- Intent is an android service that gives us the facilities of accessing one activity from the other activity. Basically, There are two types of Intent:
1) Implicit Intent
2) Explicit Intent
Explicit Intent:- Accessing one user define activity from the other activity like open new page of the application.
Here we are giving example of Implicit intent. So, create new project and drop one text view and one button. The code of android XML files is given below:
(Main XML file:- 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:background="#373"> <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="50dp" android:text="first Activity" android:textSize="35sp" /> <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="42dp" android:onClick="call" android:text="Click to call Second" /> </RelativeLayout>
(Second XML file:- second.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#077"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Second Activity" android:layout_marginTop="20dp" android:layout_marginLeft="30dp" android:textSize="35sp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="call" android:layout_marginLeft="110dp" android:text="Go to first" /> </LinearLayout>
Now open Java file and initialize all objects. Use the following trick to use explicit intent:
Intent obj=new Intent(From,To);
The code of both android Java files is given below with description:
(Main Java file:- MainActivity.java)
(Second Java file:- SecondActivity.java)
Now update AndroidManifist.xml file to use another activity in your project. Use the code below </activity> tag:
(Main Java file:- MainActivity.java)
package sel.expli; //you package name import android.os.Bundle; import android.view.View; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //set layout setContentView(R.layout.activity_main); } //This method is called when we click on button //and it is declare in main XML file in button tag //we have to pass View object in this method public void call(View v) { //define in intent where you want to go //use Intent(From,To) like we are sending a post Intent i=new Intent(this, SecondActivity.class); //start the given action to Intent startActivity(i); //finish this activity finish(); } }
(Second Java file:- SecondActivity.java)
package sel.expli; //you package name import android.os.Bundle; import android.view.View; import android.app.Activity; import android.content.Intent; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //set layout setContentView(R.layout.second); } //See the explanation of main activity //both are same public void call(View v) { Intent i=new Intent(this, MainActivity.class); startActivity(i); finish(); } }
Now update AndroidManifist.xml file to use another activity in your project. Use the code below </activity> tag:
<activity android:name="sel.expli.SecondActivity" android:label="@string/app_name"> </activity>
Here sel.expli is your package name and SecondActivity (SeconActivity.java) is your second activity name.
Intent-filter in Android XML file is used to define which activity will launch first. Now run your project and test application. If you have any doubt please comment.
Related Tutorials:-
★ Make any column invisible in Table layout
★ Create a Dialog box
★ Send SMS without any Report
★ Create List and perform Actions on it
★ Create Auto Complete Text
Intent-filter in Android XML file is used to define which activity will launch first. Now run your project and test application. If you have any doubt please comment.
Related Tutorials:-
★ Make any column invisible in Table layout
★ Create a Dialog box
★ Send SMS without any Report
★ Create List and perform Actions on it
★ Create Auto Complete Text
This comment has been removed by the author.
ReplyDeletebrother where is the button code(reference) in java files... ???
ReplyDeleteNo need to make object of button in Java file because there are two method to perform action on button: 1. implements on click listener in Java which is more complex and take more memory. 2. define tag in XML button: android:onClick="method_name" and use this method in activity and pass View object which keeps id of clicked button. I mostly used second method.
DeleteHai sir,
DeleteI am kiran kumar ,i have a dot on otp
1.how to send otp from android app to customer
Okay thanks...
ReplyDeletebrother! for every button we will need such code...?
ReplyDeletepublic void on_click_tag(View v)
{
//code
}
Yes but it depends on your use. You can use other method also.
Deleteit works excellently! awesome work sir!
ReplyDeleteThanks Bro.. :)
DeleteThis comment has been removed by the author.
ReplyDeleteIt Excellent Works.!yehh
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteHello Sir, I want to develop an android app which is a 500 Songs lyrics. i want to use list view but pls tell me how to show songs 1, songs 2 and so on till songs 500. when we click on songs 1, it should display the lyrics in another activity screen.please help me out. thanks in advance
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis application is run but not move on second activity.imulator is given the error..plz help me
ReplyDeletehow to set phone number and E-mail validation in android
ReplyDeleteHow do I open another application's activity using the Explicit intent.
ReplyDelete