/*******************************************************************************************
Program
Name: Write an
android application by using 2 activities with the help of intents.
Author:
141740
Date:
22/9/2016
*******************************************************************************************/
AndroidManifest.xml
<?xml
version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sujithbaby.intent">
<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=".Main2Activity"></activity>
</application>
</manifest>
activity_main.xml
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp"
android:background="#220E4D"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/txt1"
android:text="@string/magis"
android:textSize="25dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:textColor="#41FFC8"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txt1"
android:id="@+id/ed1"
android:hint="@string/uname"
android:textSize="25dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/pass"
android:textSize="25dp"
android:layout_below="@+id/ed1"
android:inputType="textPassword"
android:background="#ffffff"
android:layout_marginTop="10dp"
android:id="@+id/ed2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ed2"
android:text="@string/login"
android:gravity="center"
android:layout_marginTop="10dp"
android:id="@+id/login"
android:background="@color/colorPrimaryDark"
/>
</RelativeLayout>
MainActivity.java
package
com.example.sujithbaby.intent;
import
android.content.Intent;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
public
class MainActivity extends AppCompatActivity {
EditText
username,password;
Button
login;
String
st_user,st_pass;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username=(EditText)findViewById(R.id.ed1);
password=(EditText)findViewById(R.id.ed2);
login=(Button)findViewById(R.id.login);
login.setOnClickListener(new
View.OnClickListener() {
@Override
public
void onClick(View v) {
st_user=username.getText().toString();
st_pass=password.getText().toString();
Intent intent= new
Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("username",st_user);
intent.putExtra("password",st_pass);
startActivity(intent);
}
});
}
}
Activity_main2.xml
<?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:id="@+id/activity_main2"
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.sujithbaby.intent.Main2Activity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/user"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/user"
android:gravity="center"
android:id="@+id/pass"
/>
</RelativeLayout>
Main2Activity.java
package
com.example.sujithbaby.intent;
import
android.content.Intent;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.widget.TextView;
public
class Main2Activity extends AppCompatActivity {
TextView
txtUser,txtPass;
String
Uname,Pname;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
txtUser=(TextView)findViewById(R.id.user)
;
txtPass=(TextView)findViewById(R.id.pass);
Intent
intent=getIntent();
Uname=intent.getStringExtra("username");
Pname=intent.getStringExtra("password");
txtUser.setText("User
name: "+Uname);
txtPass.setText("Password
: "+Pname);
}
}
<resources>
<string
name="app_name">Intent</string>
<string
name="uname">User name</string>
<string
name="pass">Password</string>
<string
name="login">Login</string>
<string
name="magis">MAGIS</string>
</resources>
OUTPUT
No comments:
Post a Comment