22

How can I open another layout xml file when I click on a button in main.xml file?

so if I have main.xml which has a button sying click here and I click it, it opens up second.xml file (layout).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Yusef Bee
  • 281
  • 1
  • 4
  • 12

3 Answers3

35

First Create your two layout:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"  >

    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    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>

second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"  >

    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:text="This is Activity 2" />

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

</LinearLayout>

Second Add your Activity to the manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.rr"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity1"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Activity2"></activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest>

Activity1.java

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);
            }

        });
    }
}

To switch to Activity2 you have to:

  1. Gets a reference to the button with ID Button01 on the layout using (Button) findViewById(R.id.Button01).

  2. Create an OnClick listener for the button.

  3. And the most important part, creates an “Intent” to start another Activity. The intent needs two parameters: a context and the name of the Activity that we want to start (Activity2.class)

Activity2.java

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.second);

        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();
            }

        });
    }
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
K_Anas
  • 31,226
  • 9
  • 68
  • 81
5

-Inflate the button from the xml
-add an onClickListener on it
-set a new layout in the onClick event

Button btn = (Button) findViewById(R.id.myButton);
btn.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v)
{
    MyActivity.setContentView(R.layout.newlayout);
}

});

Something like this should work...

Thkru
  • 4,218
  • 2
  • 18
  • 37
  • how do I do `public void onClick() { MyActivity.setContentView(R.layout.newlayout); }` in XML – Yusef Bee Jun 07 '12 at 16:57
  • that's afaik not possible, the best thing you could do is to define the onClick Listener in XML using "android:onClick". The logic of this has to be in the java code. – Thkru Sep 11 '16 at 10:27
0

A Kotlin way:

-Add the onClick event directly in the designer.

  1. Open the activity (Activity1.xml for example) file in the designer mode
  2. Select the button that will trigger the transition
  3. Add the function name on the onClick box of the right panel with all the button properties

-Open the Activity .kt file

  1. Add the function with the name that you just defined in the designer

    fun openActivity2(view: View) { intent = Intent(view.context,Activity2::class.java) startActivity(intent) }

Now you have the function linked to the onClick event of your button

FedeH
  • 1,343
  • 18
  • 24