Working with multiple activities (Explicit Intent)



In this article we are going to learn about working with more than one activity in same project.(Explicit Intent)











You can use it from GitHub or DirectDownload.

This is how our android application looks like;


Project Structure;



Inside of the Project

activity_first.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#08dfea"
    tools:context="com.example.rincemathew.intent.FirstActivity">

    <Button
        android:id="@+id/buttonToSecondActivity"
        android:layout_width="257dp"
        android:layout_height="185dp"
        android:textSize="25dp"
        android:text="Go To Second Activity"
        android:background="#df07c6"
        tools:layout_editor_absoluteX="90dp"
        tools:layout_editor_absoluteY="263dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="50dp"
        android:layout_marginStart="50dp"
        android:layout_marginBottom="103dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="385dp"
        android:layout_height="102dp"
        android:textSize="40dp"
        android:gravity="center"
        android:text="FIRST ACTIVITY" />

</RelativeLayout>

FirstActivity.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
mport android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class FirstActivity extends AppCompatActivity {

    Button firstButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        firstButton = (Button)findViewById(R.id.buttonToSecondActivity);
        firstButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent i=new Intent(getApplicationContext(),SecondActivity.class);
                startActivity(i);

            }
        });
    }
}

activity_second.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#efef07"
    tools:context="com.example.rincemathew.intent.SecondActivity">

    <Button
        android:id="@+id/buttonToFirstActivity"
        android:layout_width="268dp"
        android:layout_height="180dp"
        android:textSize="25dp"
        android:text="Go To First Activity"
        android:background="#c70909"
        tools:layout_editor_absoluteX="58dp"
        tools:layout_editor_absoluteY="271dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="48dp"
        android:layout_marginStart="48dp"
        android:layout_marginBottom="97dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="377dp"
        android:layout_height="79dp"
        android:text="SECOND ACTIVITY"
        android:gravity="center"
        android:textSize="40dp"
        tools:layout_editor_absoluteY="26dp"
        tools:layout_editor_absoluteX="4dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp" />
</RelativeLayout>

ActivitySecond.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends AppCompatActivity {

    Button secondButton;

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

        secondButton=(Button)findViewById(R.id.buttonToFirstActivity);
        secondButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent i=new Intent(getApplicationContext(),FirstActivity.class);
                startActivity(i);

            }
        });
    }
}




You can download app from here;

You can download full source code of this tutorial here;


If you have any doubts, Make a comment.......
SHARE

Androidstuff.net

Hai. I am one of the Authors of androidstuff.net. I am Android Developer, UI/UX Designer, Web Developer, Business Enthusiast, Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment