Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

test: Reply
 
LinkBack Thread Tools
Old March 15th, 2012, 02:44 AM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2012
Posts: 5
 
Device(s):
Carrier: Not Provided

Thanks: 2
Thanked 0 Times in 0 Posts
Default Using two buttons in activity

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.

Advertisements
Trace3k is offline  
Last edited by Trace3k; March 15th, 2012 at 07:03 PM. Reason: Clarity
Reply With Quote
sponsored links
Old March 16th, 2012, 11:02 AM   #2 (permalink)
Junior Member
 
ondrovic's Avatar
 
Join Date: Dec 2011
Location: Carmel, Indiana
Posts: 42
 
Device(s): Droid Charge
Carrier: Not Provided

Thanks: 7
Thanked 1 Time in 1 Post
ondrovic@gmail.com
Default

Check two posts down for a better example
ondrovic is offline  
Last edited by ondrovic; March 16th, 2012 at 12:33 PM. Reason: podted better example below
Reply With Quote
Old March 16th, 2012, 11:56 AM   #3 (permalink)
Junior Member
 
Join Date: Dec 2011
Posts: 24
 
Device(s): LG 3d Optimus (LG-P920) Android 4.0.4
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!
Ecniv is offline  
Reply With Quote
Old March 16th, 2012, 11:59 AM   #4 (permalink)
Junior Member
 
ondrovic's Avatar
 
Join Date: Dec 2011
Location: Carmel, Indiana
Posts: 42
 
Device(s): Droid Charge
Carrier: Not Provided

Thanks: 7
Thanked 1 Time in 1 Post
ondrovic@gmail.com
Default

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.
ondrovic is offline  
Last edited by ondrovic; March 16th, 2012 at 12:02 PM.
Reply With Quote
The Following User Says Thank You to ondrovic For This Useful Post:
Trace3k (March 16th, 2012)
Old March 16th, 2012, 04:10 PM   #5 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2012
Posts: 5
 
Device(s):
Carrier: Not Provided

Thanks: 2
Thanked 0 Times in 0 Posts
Default Thanks!

This is fantastic! Thank you for your time and hard work. It works like a charm.

I was way off, no wonder I couldn't get it to work.
Trace3k is offline  
Reply With Quote
Old March 16th, 2012, 06:14 PM   #6 (permalink)
Junior Member
 
ondrovic's Avatar
 
Join Date: Dec 2011
Location: Carmel, Indiana
Posts: 42
 
Device(s): Droid Charge
Carrier: Not Provided

Thanks: 7
Thanked 1 Time in 1 Post
ondrovic@gmail.com
Default

No problem glad I helped :-)
ondrovic is offline  
Reply With Quote
Old March 16th, 2012, 06:23 PM   #7 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2012
Posts: 5
 
Device(s):
Carrier: Not Provided

Thanks: 2
Thanked 0 Times in 0 Posts
Default

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!
Trace3k is offline  
Last edited by Trace3k; March 16th, 2012 at 07:33 PM. Reason: Figured it out.
Reply With Quote
Old March 24th, 2012, 02:16 PM   #8 (permalink)
New Member
 
Join Date: Mar 2012
Posts: 2
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by Trace3k View Post
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
rajafahim is offline  
Reply With Quote
The Following User Says Thank You to rajafahim For This Useful Post:
Trace3k (March 24th, 2012)
Old March 24th, 2012, 02:37 PM   #9 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2012
Posts: 5
 
Device(s):
Carrier: Not Provided

Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thank you very much. That's way better than what I was trying to do.
Trace3k is offline  
Reply With Quote
Old April 6th, 2012, 09:26 PM   #10 (permalink)
New Member
Thread Author (OP)
 
Join Date: Mar 2012
Posts: 5
 
Device(s):
Carrier: Not Provided

Thanks: 2
Thanked 0 Times in 0 Posts
Default Still having problems

I've pasted all the code into Eclipse. The app opens but when I click on a button on the Main activity I get a force close.

Any ideas?

The logcat says: 04-07 07:55:56.637: E/AndroidRuntime(273): java.lang.IllegalStateException: Could not execute method of the activity
Trace3k is offline  
Last edited by Trace3k; April 7th, 2012 at 03:02 AM.
Reply With Quote
sponsored links
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 03:04 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.