r_benjamin
r_benjamin

Reputation: 53

Android App: Press a button and open a new layout

I'm a beginner developer and I am making an Android app. I made a layout and I called it activity_stats and I want to open the layout (layout name: youtubestats) when I press the button called Channel Stats.

Activity_Main:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Go to the GoldenNuggetNL channel" />

    <Button
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox1"
        android:layout_below="@+id/checkBox1"
        android:text="Go to the Website of GoldenNuggetNL" />

    <Button
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/checkBox2"
        android:text="Watch latest video" />

    <Button
        android:id="@+id/checkBox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/checkBox3"
        android:text="Contact" />

    <Button
        android:id="@+id/checkBox5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/checkBox4"
        android:text="Rate app" />

    <Button
        android:id="@+id/checkBox6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/checkBox5"
        android:text="Channel stats" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Run selected objects" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentLeft="true"
        android:onClick="sendMessage"
        android:text="Quit" />

</RelativeLayout>

Can anyone help me out?

Upvotes: 0

Views: 2247

Answers (1)

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95509

The documentation here explains it pretty well:
http://developer.android.com/training/basics/firstapp/starting-activity.html

The basics are that you need to create an onclick handler for your button (which you can do either in the XML file using android:onClick or in your Java code by calling the setOnClickListener method), and then in the handler for your click, you need to use an intent to activate the second activity.

Note that such an intent would need to be listed in the set of intent filters for that activity so that the activity responds to that particular intent.

If you want to show a layout within the current activity instead of opening a new activity, then you can simply call setContentView() or inflate a ViewStub in the current activity.

Upvotes: 1

Related Questions