Relation between activity, xml, views, intent and layout in Android


As a new guy, Android looks like simple if we already have know how the relation between components. From my friends who Android Developer, he said there are 3 important components in building Android applications :

1. Activity
2. Services
3. Intent
4. View

But for now, we will learn about corelation between activity, xml and layout.

First, we will learn about how Activity works. Activity located in /src//activity.java. Activity is single and be responsible to creating window, float and how to put window in UI. Remember, there could be more than one activity in this folder.

Always make name file and Activity same. For instance:

Activity1.java :

1
2
3
4
5
6
7
8
9
10
package myapp.android.namespace;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity1 extends Activity {
   
}

Now, we see that Activity have several action :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Activity extends ApplicationContext {
     public void onCreate(Bundle savedInstanceState);

     protected void onStart();
     
     protected void onRestart();

     protected void onResume();

     protected void onPause();

     protected void onStop();

     protected void onDestroy();
}

From this, we know that we can made custom action based on Activity condition. Let’s start with simple things :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package yahoo.android.namespace;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity1 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new View.OnClickListener() {           
            public void onClick(View view) {
                Intent myintent = new Intent(view.getContext(), Activity2.class);
                startActivityForResult(myintent, 0);               
            }
        });
    }  
}

“public void onCreate(Bundle savedInstanceState)”
This is meaning when Activity created, then run action inside.

“setContentView(R.layout.main);”
Activity produce / manage window on UI. Then, setContentView() is place for creating UI. R.layout.main is mean “//res/layout/main.xml”.

Example main.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is Activity 1" />

    <Button android:text="Next"
        android:id="@+id/Button01"
        android:layout_width="250px"
            android:textSize="18px"
        android:layout_height="55px"></Button>
</LinearLayout>

Yes, our UI is based on XML. we use “android:” for define / set property. We see on main.xml there is Button UI here :

1
2
3
4
5
<Button android:text="Next"
        android:id="@+id/Button01"
        android:layout_width="250px"
            android:textSize="18px"
        android:layout_height="55px"></Button>

Then on Activity, we can define how the button works if it pressed by pointing into button ID (Button01) :

1
Button next = (Button) findViewById(R.id.Button01);

“next.setOnClickListener(new View.OnClickListener()”
As explained in here, this called when view clicked which it mean is Button01.

“Intent myintent = new Intent(view.getContext(), Activity2.class);”
Intent handle how to operation to performed. Intent can made action and data to pass into another activity. In this example, when button01 clicked, it will run Activity2.class.

Activity2.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
package yahoo.android.namespace;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity2 extends Activity {
    /** Called when the activity is first created */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
       
        Button next = (Button) findViewById(R.id.Button02);
        next.setOnClickListener(new View.OnClickListener() {
           
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}

On this Activity2.java, it seen that need main2.xml for layout, so we create :

main2.xml :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Activity 2" />
       
       <Button android:text="Previous"
        android:id="@+id/Button02"
        android:layout_width="250px"
            android:textSize="18px"
        android:layout_height="55px">
     </Button>

</LinearLayout>

On Activity2.java it said that Intent has finish. So it will return into who called this class / activity, which mean back to Activity1.java.

Easy right? Kudos to : http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.