Android

Linking Activities with Intents

Google+ Pinterest LinkedIn Tumblr

1. Using Android Studio, create a new Android project with an empty Activity named MainActivity; name the project UsingIntent.
2. Right-click your package name under the app>>app>>src>>main>>java folder in the Project Files windows and select New ➪ Java Class
3. Name the new class SecondActivity and click OK.
4. Add the bolded statements from the following code to the AndroidManifest.xml file. Be sure to change all instances of “com.akbansal” to whatever package name your project is using.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.akbansal.usingintent">

<application
android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" >
<intent-filter >
<action android:name="com.akbansal.usingintent.SecondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

</manifest>

5. Make a copy of the activity_main.xml file (in the res/layout folder) by right-clicking it and selecting Copy. Then right-click the res/layout folder and select Paste. Name the file activity_second.xml.
6. Modify the activity_second.xml file as follows:

<?xml version="1.0" encoding="utf-8"?>
<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.akbansal.usingintent.SecondActivity">

<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is the Second Activity!" />
</RelativeLayout>

 

7. In the SecondActivity.java file, add the bolded statements from the following code:

package com.akbansal.usingintent;

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

public class SecondActivity extends Activity {
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second);
}

}

8. Add the bolded lines in the following code to the activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<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.akbansal.usingintent.MainActivity">

<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Main Activity!" android:id="@+id/textView" />

<Button
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Display second activity" android:onClick="onClick" android:id="@+id/button" android:layout_below="@+id/textView" android:layout_alignParentStart="true" android:layout_marginTop="56dp" />
</RelativeLayout>

10. Press Shift+F9 to debug the application on the Android emulator. When the first activity is loaded, click the button and the second activity also loads

   

Write A Comment