GTU MCA SLMC Program 2 Android Intent Example

GTU MCA SLMC Program 2 To understand Activity, Intent  a.  Create sample application with login module.(Check username and password)  b.  On successful login, go to next screen. And on failing login, alert user using Toast.  c.  Also pass username to next screen

 GTU MCA SLMC Program 2

To understand Activity, Intent  a.  Create sample application with login module.(Check username and password)  b.  On successful login, go to next screen. And on failing login, alert user using Toast.  c.  Also pass username to next screen

Jump to SLMC Program 3

Hope you have understand helloworld Android Example, If so now its time time learn next one ! Here I have explained the use of multiple Activity in Android. In Android the screen what we can see is called as Activity. Here the program i would like to explain how to create two activity(Screen) and how can move from one activity to another activity on button click.

 

Question related with android intent.

  • How can an application start activity with startActivity() method  with an explicit intent? Explain with line of code.
  • Explain the line of code that create an intent Filter and register it within
    Android Manifest file?
  • How an additional data can be included in Intent?
  • What is Intent?  List  out  the various  uses  of  intent   and also explain  using
    example how to pass additional information with intent
  • What is Intent? How can  Intent  include two extra piece of information  –
    a string value and a Boolean value?
  • What is Intent? How are they used to Broadcast and Receive Events?
  • What is the use of <activity> tag and <intent-filter> tag

 

Step 1 : Create Android Application (Automatically it will add 1 Activity)

Step 2 : Right Click on Project Name goto New -> Other -> Select Activity and Add it to project.

Step 3 : Now prepare design and write down code for both activity and test it on your Android Virtual Device or your connected mobile !

 

Location : res\layout

FileName: activity_main.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="gtu.moc.prog02.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="36dp"
        android:text="Login"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:hint="Username"
        android:layout_marginTop="34dp" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:hint="Password"
        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="28dp"
        android:text="Login" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:text="Developed By: Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    
</RelativeLayout>

Location : res\layout

FileName: activity_main2.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="gtu.moc.prog02.MainActivity2" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Welcome to Activity 2"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp"
        android:text="Developed By: Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

 

Location : src\package\

FileName : MainActivity.java

package gtu.moc.prog02;

/*
 * 
 * GTU MCA SLMC Program 2 
 * 
 * To understand Activity, Intent  
 * a.  Create sample application with login module.(Check username and password)  
 * b.  On successful login, go to next screen. And on failing login, alert user using Toast.  
 * c.  Also pass username to next screen
 * 
 */

/*
 * This is the main activity will be automatically started when you starts apps
 * 
 */
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 {

	Button bt1;
	EditText et1,et2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		bt1 = (Button) findViewById(R.id.button1);
		et1 = (EditText) findViewById(R.id.editText1);
		et2 = (EditText) findViewById(R.id.editText2);

		
		
		bt1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			
				if(et1.getText().toString().equals("adarsh") && et2.getText().toString().equals("adarsh") )
				{
					//==========================================================================
					// Valid Userid and Password so we are redirecting user to another activity.
					// For the same we have to user Intent Object and need to pass source and destination class
					//=========================================================================
					Intent i = new Intent(getApplicationContext(),MainActivity2.class);
					startActivity(i);
				}
				else
				{
					//=======================================================
					// Display Message to user about Invalid Userid or Password
					//=======================================================
					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.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

Location : src\package\

FileName : MainActivity2.java

package gtu.moc.prog02;

/*
 * 
 * GTU MCA SLMC Program 2 
 * 
 * To understand Activity, Intent  
 * a.  Create sample application with login module.(Check username and password)  
 * b.  On successful login, go to next screen. And on failing login, alert user using Toast.  
 * c.  Also pass username to next screen
 * 
 */


/*
 * 
 * This activity will only load if you enter correct userid and password.
 */


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.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

Output 1

GTU MCA SLMC Program 2

 

Output 2

GTU MCA SLMC Program 2

Output 3

GTU MCA SLMC Program 2

Leave a Reply