Using two buttons in activity


  1. Trace3k

    Trace3k Member

    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.

    Advertisement
  2. ondrovic

    ondrovic Active Member

    Check two posts down for a better example
  3. Ecniv

    Ecniv Member

    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!
  4. ondrovic

    ondrovic Active Member

    Here is a better example

    ButtonActivityTest.java
    Code (Text):
    1.  
    2. package com.ondroivc.button.test;
    3.  
    4. import android.app.Activity;
    5. import android.content.Intent;
    6. import android.os.Bundle;
    7. import android.view.View;
    8.  
    9. public class ButtonTestActivity extends Activity {
    10.     /** Called when the activity is first created. */
    11.     @Override
    12.     public void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.         setContentView(R.layout.main);
    15.     }
    16.  
    17.     public void Activity1_clicked(View view) {
    18.  
    19.         startActivity(new Intent(getApplicationContext(), one.class));
    20.     }
    21.  
    22.     public void Activity2_clicked(View view) {
    23.  
    24.         startActivity(new Intent(getApplicationContext(), two.class));
    25.     }
    26. }
    one.java
    Code (Text):
    1.  
    2. package com.ondroivc.button.test;
    3.  
    4. import android.app.Activity;
    5. import android.content.Intent;
    6. import android.os.Bundle;
    7. import android.view.View;
    8.  
    9. public class one extends Activity {
    10.     /** Called when the activity is first created. */
    11.     @Override
    12.     public void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.         setContentView(R.layout.one);
    15.     }
    16.  
    17.     public void Activity2_clicked(View view) {
    18.  
    19.         startActivity(new Intent(getApplicationContext(), two.class));
    20.     }
    21.  
    22.     public void MainActivity_clicked(View view) {
    23.  
    24.         startActivity(new Intent(getApplicationContext(), ButtonTestActivity.class));
    25.     }
    26. }
    two.java
    Code (Text):
    1.  
    2. package com.ondroivc.button.test;
    3.  
    4. import android.app.Activity;
    5. import android.content.Intent;
    6. import android.os.Bundle;
    7. import android.view.View;
    8.  
    9. public class two extends Activity {
    10.     /** Called when the activity is first created. */
    11.     @Override
    12.     public void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.         setContentView(R.layout.two);
    15.     }
    16.  
    17.     public void Activity1_clicked(View view) {
    18.  
    19.         startActivity(new Intent(getApplicationContext(), one.class));
    20.     }
    21.  
    22.     public void MainActivity_clicked(View view) {
    23.  
    24.         startActivity(new Intent(getApplicationContext(), ButtonTestActivity.class));
    25.     }
    26. }
    androidmanifest.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    4.     package="com.ondroivc.button.test"
    5.     android:versionCode="1"
    6.     android:versionName="1.0" >
    7.  
    8.     <uses-sdk android:minSdkVersion="10" />
    9.  
    10.     <application
    11.         android:icon="@drawable/ic_launcher"
    12.         android:label="@string/app_name" >
    13.         <activity
    14.             android:name=".ButtonTestActivity"
    15.             android:label="@string/app_name" >
    16.             <intent-filter>
    17.                 <action android:name="android.intent.action.MAIN" />
    18.  
    19.                 <category android:name="android.intent.category.LAUNCHER" />
    20.             </intent-filter>
    21.         </activity>
    22.         <activity android:name=".one" />
    23.         <activity android:name=".two" />
    24.     </application>
    25.  
    26. </manifest>
    main.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     android:orientation="vertical" >
    7.  
    8.     <TextView
    9.         android:layout_width="fill_parent"
    10.         android:layout_height="wrap_content"
    11.         android:text="@string/hello" />
    12.  
    13.     <Button
    14.         android:id="@+id/button1"
    15.         android:layout_width="wrap_content"
    16.         android:layout_height="wrap_content"
    17.         android:text="Activity1"
    18.         android:onClick="Activity1_clicked" />
    19.  
    20.     <Button
    21.         android:id="@+id/button2"
    22.         android:layout_width="wrap_content"
    23.         android:layout_height="wrap_content"
    24.         android:text="Activity2"
    25.         android:onClick="Activity2_clicked" />
    26.  
    27. </LinearLayout>
    one.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     android:orientation="vertical" >
    7.  
    8.     <TextView
    9.         android:layout_width="fill_parent"
    10.         android:layout_height="wrap_content"
    11.         android:text="Activity1" />
    12.  
    13.     <Button
    14.         android:id="@+id/button1"
    15.         android:layout_width="wrap_content"
    16.         android:layout_height="wrap_content"
    17.         android:text="MainActivity"
    18.         android:onClick="MainActivity_clicked"/>
    19.  
    20.     <Button
    21.         android:id="@+id/button2"
    22.         android:layout_width="wrap_content"
    23.         android:layout_height="wrap_content"
    24.         android:text="Activity2"
    25.         android:onClick="Activity2_clicked"/>
    26.  
    27. </LinearLayout>
    two.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     android:orientation="vertical" >
    7.  
    8.     <TextView
    9.         android:layout_width="fill_parent"
    10.         android:layout_height="wrap_content"
    11.         android:text="Activity2" />
    12.  
    13.     <Button
    14.         android:id="@+id/button1"
    15.         android:layout_width="wrap_content"
    16.         android:layout_height="wrap_content"
    17.         android:text="Activity1"
    18.         android:onClick="Activity1_clicked"/>
    19.  
    20.     <Button
    21.         android:id="@+id/button2"
    22.         android:layout_width="wrap_content"
    23.         android:layout_height="wrap_content"
    24.         android:text="MainActivity"
    25.         android:onClick="MainActivity_clicked"/>
    26.  
    27. </LinearLayout>
    Not sure if this is the most efficient way to do it but it works.
    Trace3k likes this.
  5. Trace3k

    Trace3k Member

    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.
  6. ondrovic

    ondrovic Active Member

    No problem glad I helped :)
  7. Trace3k

    Trace3k Member

    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!
  8. rajafahim

    rajafahim New Member

    dear use this code below andriod:text color
    andriod:background image"image.png"

    use only png images
    Trace3k likes this.
  9. Trace3k

    Trace3k Member

    Thank you very much. That's way better than what I was trying to do.
  10. Trace3k

    Trace3k Member

    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
Loading...

Share This Page