You will also need to create another Activity (java file) for the multiple views to work. We are going to make a simple application that will use two buttons and two views. Each view will have a button that switches to the other view. Many applications require more than one view. If you need to setup the environment please refer to my first article, Your First Android App.
I named my project MultiViews and am using Android 2.3. The first thing you can do is go ahead and create the views. The first view has a button that links our second view. The first view is stored in main.xml and our second one is main2.xml. They are identical besides the names of the buttons and the caption on the buttons. Here is a screen shot and the code from both views.
main.xml
1: <?xml version="1.0" encoding="utf-8"?>
2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3: android:orientation="vertical"
4: android:layout_width="fill_parent"
5: android:layout_height="fill_parent"
6: >
7: <Button android:text="View 2"
8: android:id="@+id/button1"
9: android:layout_width="fill_parent"
10: android:layout_height="wrap_content">
11: </Button>
12: </LinearLayout>
main2.xml
1: <?xml version="1.0" encoding="utf-8"?>
2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3: android:orientation="vertical"
4: android:layout_width="fill_parent"
5: android:layout_height="fill_parent"
6: >
7: <Button android:text="View 1"
8: android:id="@+id/button2"
9: android:layout_width="fill_parent"
10: android:layout_height="wrap_content">
11: </Button>
12: </LinearLayout>
Now, create a new activity MultiViews2.java This will contain essentially the same as the first MultiViews.java file. Here is the First and Second activities files.
MultiViews.java
1: package com.ler.multiviews;
2: import android.app.Activity;
3: import android.content.Intent;
4: import android.os.Bundle;
5: import android.view.View;
6: import android.widget.Button;
7: public class MultiViews extends Activity {
8: /** Called when the activity is first created. */
9: @Override
10: public void onCreate(Bundle savedInstanceState) {
11: super.onCreate(savedInstanceState);
12: setContentView(R.layout.main);
13: }
14: }
MultiViews2.java
1: package com.ler.multiviews;
2: import android.app.Activity;
3: import android.content.Intent;
4: import android.os.Bundle;
5: import android.view.View;
6: import android.widget.Button;
7: public class MultiViews2 extends Activity {
8: /** Called when the activity is first created. */
9: @Override
10: public void onCreate(Bundle savedInstanceState) {
11: super.onCreate(savedInstanceState);
12: setContentView(R.layout.main2);
13: }
14: }
We now have to add the Listeners to the Activity files, to let us click the buttons. Here is the first one:
1: Button next = (Button) findViewById(R.id.button1);
2: next.setOnClickListener(new View.OnClickListener() {
3: public void onClick(View view) {
4: Intent myIntent = new Intent(view.getContext(), MultiViews2.class);
5: startActivityForResult(myIntent, 0);
6: }
7: });
and the second:
1: Button next = (Button) findViewById(R.id.button1);
2: next.setOnClickListener(new View.OnClickListener() {
3: public void onClick(View view) {
4: Intent myIntent = new Intent(view.getContext(), MultiViews.class);
5: startActivityForResult(myIntent, 0);
6: }
7: });
Now, you will need to open your AndroidManifest.xml file and add the Activity to it, the second class we wrote.
1: <activity android:name=".MultiViews2"></activity>
I guess the title should be 'Android Multiple Activities'
ReplyDelete