We can start an activity with help of startActivity aka Intent. Activity extends with defining method on the context object. Here is a sample code to clearly show how to wakeup or start another activity with intent.
To start an activity, use the method startActivity(intent). This method is defined on theContext object which Activity extends. The following code demonstrates how you can start another activity via an intent.
- Start the activity connect to the
- specified class
Intent i = new Intent(this, ActivityTwo.class); startActivity(i);
Subactivities are started by main activities. Activities with main thread are started by intent and subactivities are started by parent activities. Main activities are meant and subactivities are started by meant activities respectively.
Activities which are started by other Android activities are called subactivities. This wording makes it easier to describe which activity is meant.
In this tutorial we will learn How to Start New Activity On Button Click via Intent
Step 1 – Create new Android project
Step 2 – Add a button to main activity (layout/activity_main.xml) using designer.
Step 3 Add the new activity . Right Click Project -> New -> Activity -> Blank Activity
Give the name to your activity SecondActivity
Change the background color of SecondActivity if you wish.
write the following code
src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.programmingknowledge.thefirstapp" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity" android:label="@string/title_activity_second" > <intent-filter> <action android:name="com.example.programmingknowledge.thefirstapp.SecondActivity" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>
layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Open Second Activity" android:id="@+id/button" android:layout_gravity="center_horizontal" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="62dp" android:layout_marginStart="62dp" android:layout_marginTop="205dp" /> </RelativeLayout>
layout/activity_second.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" tools:context="com.example.programmingknowledge.thefirstapp.SecondActivity" android:background="#ff80ff3c"> </RelativeLayout>
com/example/programmingknowledge/thefirstapp/MainActivity.java
package com.example.programmingknowledge.thefirstapp; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends ActionBarActivity { private static Button button_sbm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); OnClickButtonListener(); } public void OnClickButtonListener() { button_sbm = (Button)findViewById(R.id.button); button_sbm.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.example.programmingknowledge.thefirstapp.SecondActivity"); startActivity(intent); } } ); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
com/example/programmingknowledge/thefirstapp/SecondActivity.java
package com.example.programmingknowledge.thefirstapp; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class SecondActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_second, 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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
OUTPUT:
Android Books To Learn Mobile Apps Programming
- Android Programming: The Big Nerd Ranch Guide
- Android Design Patterns: Interaction Design Solutions for Developers
- Android Application Development Cookbook – Second Edition
- Android User Interface Design: Turning Ideas and Sketches into Beautifully Designed Apps (Usability)
- Android Recipes: A Problem-Solution Approach for Android 5.0
- Hello, Android: Introducing Google’s Mobile Development Platform (Pragmatic Programmers)
- Beginning Android Games
Leave a Reply