Android program to implement explicit intent

Android program to implement explicit intent

Android program to implement explicit intent. This Android program is to show username on next screen on successful login thereby demonstrating the use of explicit intent. Explicit intent helps us to call another activity in android. For example when we are on the login screen of any app, then on successful login you go on to another screen(Activity), this is nothing but explicit intent. Explicit intent also helps to pass values from one activity to another.

In the code below we will be going to another screen on successful login, and passing the username from login screen to the next screen(Activity). By now, you should be able to understand the XML part, if yet do not understand some bit of it let me know. Coming straight to MainActivity.java file, here we will create a logic to check if username and password are valid, if valid then we send the username to the next activity using intent. The passing of parameters is done using putExtra method which is a key value tuple i.e putExtra(key, value). Now in order to retrieve these values in the SecondActivity you will need the method getStringExtra(key). We are using getStringExtra as we want to display a string and not an integer, if we had an integer then getExtra would be fine. We can also send and retrieve values using Bundle, but I felt this is the easiest way to get you started with Intents. Let me know if you want the other way also using Bundle.

Steps to create the application:- 

  • Open Android Studio and create a new Android application and name it as “ExplicitIntent” and company domain as codedost so your package will be automatically set.
  • Open an Empty Activity and name it as MainActivity.
  • You will need to also create another Activity called SecondActivity. For that right click on app, goto New, goto Activity, Empty activity and type in SecondActivity and click on OK. This will create both java file as well as the xml file for you
  • Copy the contents of res/layout/activity_main.xml and res/layout/activity_second.xml file.
  • Run the application to launch Android emulator or you can run it on your mobile also(which is way faster).

Just make sure that the manifest file contains the second activity.

AndroidManifest.xml

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

    <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" />
    </application>
</manifest>

MainActivity.java

package com.example.jay.successlogin;

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;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button mLogin;
    EditText mUsername, mPassword;

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

        mLogin = (Button) findViewById(R.id.b1);
        mUsername = (EditText) findViewById(R.id.etusername);
        mPassword = (EditText) findViewById(R.id.etpassword);

        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //String u = mUsername.getText().toString();
                //String p = mPassword.getText().toString();
                if (mUsername.getText().toString().equals("codedost") && mPassword.getText().toString().equals("8989")) {
                    String str = "Username = " + mUsername.getText().toString();
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    intent.putExtra("username", str);
                    startActivity(intent);
                }
                else
                {
                    Toast.makeText(MainActivity.this,"Invalid credentials",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

SecondActivity.java

package codedost.explicitintent;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

    TextView t1;

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

        t1 = (TextView) findViewById(R.id.s_username);

        Intent intent = getIntent();

        String user = intent.getStringExtra("username");
        t1.setTextColor(Color.GREEN);
        t1.setText(user);
    }
}

XML File(res/layout/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:orientation="vertical"
    android:background="#00CC99"

    tools:context="codedost.explicitintent.MainActivity">


    <EditText
        android:id="@+id/etusername"
        android:hint="Username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="18dp"
        android:padding="8dp"
        android:background="#fff"
        android:layout_marginTop="92dp"
        android:layout_alignParentTop="true" />


    <EditText
        android:id="@+id/etpassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="18dp"
        android:padding="8dp"
        android:background="#fff"
        android:hint="Password"
        android:inputType="textPassword"
        android:layout_marginTop="14dp"
        android:layout_below="@+id/etusername"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/b1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:textColor="#00CC99"
        android:layout_marginTop="17dp"
        android:layout_below="@+id/etpassword"
        android:layout_alignStart="@+id/etpassword"
        android:layout_alignEnd="@+id/etpassword" />

</RelativeLayout>

XML File(res/layout/second_activity.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_second"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="codedost.explicitintent.SecondActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/s_username"
        android:text=""
        android:textColor="#0f0"
        android:gravity="center"
        android:textSize="18sp"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="14dp" />

</RelativeLayout>

Output

Share Me!