GTU DE Sem6 – 3360704 – 2 – Create sample application with login module.(Check username and password) On successful login, go to next screen. And on failing login, alert user using Toast. Also pass username to next screen

GTU DE Sem6 – 3360704 – 2 – Create sample application with login module.(Check username and password) On successful login, go to next screen. And on failing login, alert user using Toast. Also pass username to next screen

DE Sem6 - 3360704 - 2 - Create sample application with login module

This android application explain how to go from screen 1 to screen 2. In Android there is a terminology used for screen is Activity. So here I have explained how you can go from one activity to another activity.  You can go to another activity using intent. You need to create object of Intent and finally you need to pass that object to StartActivity() method of Android. Here i have am checking username and password, If user enters admin as username and admin as password then he/she is allowed to go to another page, else invalid userid/password message will be displayed on screen.

Jump to Android Program 3

FileName:MainActivity.java

/*

Mobile Computing and Application Development : 3360704

GTU DE Sem6 - 3360704 - 2 - Create sample application with login module.(Check username and 
password) On successful login, go to next screen. And on failing login, alert user using Toast. Also pass username to next screen

  
*/

package gtu.de.mcad.ex02;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	EditText et1,et2;
	Button b1;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		et1 = (EditText) findViewById(R.id.editText1);
		et2 = (EditText) findViewById(R.id.editText2);
		b1 = (Button) findViewById(R.id.button1);
		
		b1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			
				String s1 = et1.getText().toString();
				String s2 = et2.getText().toString();
				
				if(s1.equals("admin") && s2.equals("admin"))
				{
					Intent i = new Intent(getApplicationContext(),MainActivity2.class);
					startActivity(i);
				}
				else
				{
					Toast.makeText(getApplicationContext(), "Invalid Userid/Password", Toast.LENGTH_LONG).show();
				}
						
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.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.
		
		return super.onOptionsItemSelected(item);
	}
}

 

FileName: MainActivity2.java

/*

Mobile Computing and Application Development : 3360704

DE Sem6 - 3360704 - 2 - Create sample application with login module.(Check username and 
password) On successful login, go to next screen. And on failing login, alert user using Toast. Also pass username to next screen

  
*/

package gtu.de.mcad.ex02;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity2 extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_activity2);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_activity2, 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.
	
		return super.onOptionsItemSelected(item);
	}
}

 

FileName: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="52dp"
        android:text="Password"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignTop="@+id/textView2"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="54dp"
        android:text="Login" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="84dp"
        android:text="Username"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView1"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>

</RelativeLayout>

 

FileName: activity_main_activity2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal"
        android:textColor="@color/red"
        android:text="Welcome to Activity 2" />
</LinearLayout>

 

FileName: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="gtu.de.mcad.ex02"
    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=".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=".MainActivity2"
            android:label="@string/title_activity_main_activity2" >
        </activity>
    </application>

</manifest>

 

Output after adarsh/adarsh

    DE Sem6 - 3360704 - 2 - Create sample application with login module

Output : after admin/admin 

       DE Sem6 - 3360704 - 2 - Create sample application with login module

Leave a Reply