Activity
In Android, an activity is represent a single screen. Most applications have multiple activities to represent different screens.
For example one activity is loginUser and another is WelcomeUser.
Now, we need to know how these activities interact with each other, when user click on login button.
Then next activity WelcomeUser will be called and display the welcome user message.
Steps to be followed:
1) Create android project name called (ActivityExample)
2) Create two layouts one is for login and another is for welcome user
3) As we said, need to add two activities loginUser and WelcomeUser
loginUser: Having login button. – This is the main activity
WelcomeUser: Having welcome user string.
4) These two activities should be defined in manifest file AndroidManifest.xml
5) Run the project & Demo in device.
loginUser
Step 2:
Create two layouts one is for login and another is for welcome user
1) login_user.xml
2) welcome_user.xml
login_user.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="com.example.activityexample.MainActivity" > <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/text1" android:layout_below="@+id/text1" android:layout_marginTop="16dp" android:text="@string/button_login" /> </RelativeLayout>
welcome_user.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="com.example.activityexample.WelcomeUserActivity" > <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/welcomeUser" /> </RelativeLayout>
Step 3:
Crate two activities loginUser and WelcomeUser
1) LoginUserActivity.java
2) WelcomeUserActivity.java
LoginUserActivity.java
package com.example.activityexample; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class LoginUserActivity extends Activity { Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login_user); addListenerOnButton(); } public void addListenerOnButton() { final Context context = this; button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(context, WelcomeUserActivity.class); startActivity(intent); } }); } }
WelcomeUserActivity.java
package com.example.activityexample; import android.app.Activity; import android.os.Bundle; public class WelcomeUserActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.welcome_user); } }
Step 4:
These two activities should be defined in manifest file.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.activityexample" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".LoginUserActivity" 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:label="@string/app_name" android:name=".WelcomeUserActivity" > </activity> </application> </manifest>
Demo :
Run as “Android Application”,
Click on login , button.
Then User Welcome page will display.
*** Venkat – Happy leaning ****