javaandroidtextview

Buttons and TextView in Android


I'm trying to learn the ins and outs of Android and I'm finding some real challenges. I am trying to link a Button to a TextView, when you click the Button the value in the TextView changes. I've tried to use some of the sample code I've found online but I get a lot of errors and I'm not sure what to do with them. I'd appreciate any help you could offer.

Thanks so much for your help!

at line 25:

Multiple markers at this line
- main cannot be resolved or is not a field
- R cannot be resolved to a variable

at line 29:

btn1 cannot be resolved or is not a field

at line 32:

Syntax error on token(s), misplaced construct(s)

at line 34:

Multiple markers at this line
- Syntax error on token ")", ; expected
- Syntax error on token "(", ; expected
- void is an invalid type for the variable onClick

at line 36:

txtVw1 cannot be resolved or is not a field

and then here is my code:

package com.demuro1.views;
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

Button aBut = (Button) findViewById(R.id.btn1);

public void changeText(){
    mButton.setOnClickListener(new View.OnClickListener {
        public void onClick(View v) {
            final TextView mTextView = (TextView) findViewById(R.id.txtVw1);
            mTextView.setText("Some Text");
        }
    };
}

}

and then the xml from the view

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    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" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="129dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/txtVw1"
                    android:textSize="25sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="press button 1" />

                <Button
                    android:id="@+id/btn1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="Button 1" 
                    android:onClick="activateBtn1"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="140dp"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/txtVw2"
                    android:textSize="25sp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="press button 2" />

                <Button
                    android:id="@+id/btn2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 2" 
                    android:onClick="activateBtn2"/>
                    />
            </LinearLayout>
        </LinearLayout>

</LinearLayout>

Solution

  • Use The following code

    package com.demuro1.views;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        final Button aBut = (Button)findViewById(R.id.btn1);
        Button bBut = (Button)findViewById(R.id.btn2);
        final TextView aText = (TextView)findViewById(R.id.txtVw1);
        final TextView bText = (TextView)findViewById(R.id.txtVw2);
    
        aBut.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                aText.setText("You Clicked Button 1");
    
            }
        });
    
        bBut.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                bText.setText("You Clicked Button 2");
    
            }
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
       }