• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Using two buttons in activity

Trace3k

Lurker
Mar 15, 2012
5
0
I am new to android apps and I'm trying to make an interactive book for children. I want each activity to link to two other activities via buttons so that the child can make a choice on each page.

I can not figure out how to have two buttons on one activity that each lead to new activities. I can get the first button to work but not the second.

It would look like:

Activity 1 leads to activity 2 or 3.
Activity 2 leads to activity 4 or 5, etc.
 
Hi,

I was going to try this sort of thing later on - so good luck!!



But I think you'd need a file to read the text to view and where to jump to. Then you can use one layout to view, and refresh what is shown inside, pulling from an array perhaps?


Alternatively you'd have lots of xml layouts (I guess) with the different texts in. The button would use the onClick to call a function that closes the current layout and creates the required new one...?

Probably worth trying both ideas?

Again - Good luck!
 
Upvote 0
Here is a better example

ButtonActivityTest.java
Code:
package com.ondroivc.button.test;

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

public class ButtonTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void Activity1_clicked(View view) {

        startActivity(new Intent(getApplicationContext(), one.class));
    }

    public void Activity2_clicked(View view) {

        startActivity(new Intent(getApplicationContext(), two.class));
    }
}
one.java
Code:
package com.ondroivc.button.test;

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

public class one extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one);
    }

    public void Activity2_clicked(View view) {

        startActivity(new Intent(getApplicationContext(), two.class));
    }

    public void MainActivity_clicked(View view) {

        startActivity(new Intent(getApplicationContext(), ButtonTestActivity.class));
    }
}
two.java
Code:
package com.ondroivc.button.test;

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

public class two extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.two);
    }

    public void Activity1_clicked(View view) {

        startActivity(new Intent(getApplicationContext(), one.class));
    }

    public void MainActivity_clicked(View view) {

        startActivity(new Intent(getApplicationContext(), ButtonTestActivity.class));
    }
}
androidmanifest.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ondroivc.button.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ButtonTestActivity"
            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=".one" />
        <activity android:name=".two" />
    </application>

</manifest>
main.xml
Code:
<?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="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity1"
        android:onClick="Activity1_clicked" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity2"
        android:onClick="Activity2_clicked" />

</LinearLayout>
one.xml
Code:
<?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="Activity1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MainActivity" 
        android:onClick="MainActivity_clicked"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity2" 
        android:onClick="Activity2_clicked"/>

</LinearLayout>
two.xml
Code:
<?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="Activity2" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity1" 
        android:onClick="Activity1_clicked"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MainActivity" 
        android:onClick="MainActivity_clicked"/>

</LinearLayout>

Not sure if this is the most efficient way to do it but it works.
 
  • Like
Reactions: Trace3k
Upvote 0
Perhaps you could help me with another question... :)

I have set the text color, forced landscape view, and everything looks great. The last thing I would like to do is set a background image for all of the activities. I have a picture of parchment paper that I would like in the background of each activity to give it an old book feel.

I'm guessing it goes into the manifest.xml or the style.xml but I can't figure out how to do it.

Any help would be appreciated.

Trace


Nevermind, I figured it out by myself. Yay!
 
Upvote 0
Perhaps you could help me with another question... :)

I have set the text color, forced landscape view, and everything looks great. The last thing I would like to do is set a background image for all of the activities. I have a picture of parchment paper that I would like in the background of each activity to give it an old book feel.

I'm guessing it goes into the manifest.xml or the style.xml but I can't figure out how to do it.

Any help would be appreciated.

Trace


Nevermind, I figured it out by myself. Yay!
dear use this code below andriod:text color
andriod:background image"image.png"

use only png images
 
  • Like
Reactions: Trace3k
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones