In Android programming, how do I carry a variable from a button press to my main program?
<p>How would one set the button1 variable to true in the ReadButtons method and have it be used in the onCreate method/class?</p>

<p>I am a noob and have spent many hours trying to figure this out. I have no idea where to look for the answer any further. I know I may be way off in my understanding of how this works. But I don't know how to correct that misunderstanding. My background is in C and other procedural programming languages. Thank you kindly for your help.</p>

<pre><code>package com.test;

// This is being run on Eclipse Helios with ADK set to Android 2.2 
// and a 10 inch landscape display tablet PC.

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.Boolean;

public class ButtonTestActivity extends Activity {


    public static Boolean button1 = false, button2 = false;
    public static String myText="Drat";

    TextView textV_Test_Display;

    public void ReadButtons(){
        Button btn_button1_Ref = (Button) findViewById(R.id.btn_button1);
        btn_button1_Ref.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myText="YAY!";
                button1 = true;

                // if(button1) textV_Test_Display.setText(myText);
                //
                // When I uncomment the above, it works as I desire visually.
                // 
                // How do I set button1=true here and have 
                // the value carry back to the onCreate class?
                //
                //
                Toast.makeText(v.getContext(), "Button 1", Toast.LENGTH_SHORT)
                        .show();
            }
        });


        Button btn_button2_Ref = (Button) findViewById(R.id.btn_button2);
        btn_button2_Ref.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myText="WOOT!";
                button2 = true;

                // if(button2) textV_Test_Display.setText(myText);
                //
                // When I uncomment the above, it works as I desire visually.
                // 
                // How do I set button2=true here and have 
                // the value carry back to the onCreate class?
                //
                //
                Toast.makeText(v.getContext(), "Button 2", Toast.LENGTH_SHORT)
                        .show();
            }
        });
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textV_Test_Display = (TextView)findViewById(R.id.txtV_ID_Test_Data);    

        ReadButtons();

        if(button1 || button2) textV_Test_Display.setText(myText);
        // This does not display myText.
        // I am not able to carry the button1 or button2 variables to 
        // this part of the program.

        // textV_Test_Display.setText(myText);
        // When uncommented, this displays the word "Drat".
        // When commented out, the word "nope" is displayed.        
        // In the main.xml file the text is set to the word "nope"
        // This tells me this command works as expected.

        }
    }
</code></pre>

<p>This is the main.xml file.</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;RelativeLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="horizontal"
    android:background="#FF000FFF" 
    xmlns:android="http://schemas.android.com/apk/res/android"&gt;


    &lt;Button android:id="@+id/btn_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textStyle="bold" 
        android:height="50dp" 
        android:text="Button 1" 
        android:width="140dp" 
        android:textSize="16dp" 
        android:layout_marginTop="160dp" 
        android:layout_marginLeft="220dp"&gt;&lt;/Button&gt;



    &lt;Button android:id="@+id/btn_button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textStyle="bold" 
        android:height="50dp" 
        android:text="Button 2" 
        android:width="140dp" 
        android:textSize="16dp" 
        android:layout_marginTop="160dp" 
        android:layout_marginLeft="450dp"&gt;&lt;/Button&gt;



    &lt;TextView android:id="@+id/txtV_ID_Test_Data"
        android:gravity="center"
        android:textSize="28dp" 
        android:textStyle="bold" 
        android:typeface="sans" 
        android:textColor="#FFFFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_marginTop="50dp" 
        android:height="50dp"
        android:width="120dp" 
        android:text="nope" 
        android:layout_marginLeft="338dp"
        android:background="#F00FF0F0"

    &gt;&lt;/TextView&gt;

&lt;/RelativeLayout&gt;
</code></pre>

<p>Thanks</p>
