MrJoshFisher
MrJoshFisher

Reputation: 1123

Android SDK Change text when Button clicked

 package com.purelymean.earnings;
 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.Button;
 import android.widget.TextView;

 public class Main extends Activity{
     /**Called when activity is first created. */
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         setContentView(R.layout.main);
         Button b = (Button) findViewById(R.id.button);
         TextView tx = (TextView) findViewById(R.id.textView1);
     }
 }

What do I need to put so that when the button is clicked it will change the textview to whatever i put?

Upvotes: 2

Views: 7787

Answers (2)

yugidroid
yugidroid

Reputation: 6690

Add an OnClickListener to your button and thats all you need.

b.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
       tx.setText("It Works!");
   }
});

Upvotes: 1

Fran Verona
Fran Verona

Reputation: 5476

You should add an OnClickListener to your button and override onClick function to set your desire text in your TextView

Button b = (Button) findViewById(R.id.button);

b.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
       TextView tx = (TextView) findViewById(R.id.textView1);
       tx.setText("yourtext");
   }
});

Upvotes: 2

Related Questions