Shafizadeh
Shafizadeh

Reputation: 10340

How do I set a value to a textView?

I'm working on a pretty simple project .. All I'm trying to do is changing the value of a textView.

Here is the scenario: There is a button and a textView. The default value of that textView is no. it should be yes when I click on that button. That's it.

Here is my XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

And this is the JAVA code:

package ir.testApp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class TestAppActivity extends Activity {

    TextView text1;
    Button   btn1;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn1 = (Button) findViewById(R.id.button1);
        text1 = (TextView) findViewById(R.id.textView1);

        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                text1.setText("yes");

            }
        });
    }
}

Currently it doesn't work. I mean nothing happens when I click on that button. Well what's wrong?


Note: When I put that button in the top of that textView, it works like a charm. So Why it won't work when the button is after that textView?

Upvotes: 0

Views: 86

Answers (3)

user4696837
user4696837

Reputation:

Edit Code Like this

    btn1 = (Button) findViewById(R.id.button1);
    text1 = (TextView) findViewById(R.id.textView1);

    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            text1.setText("yes");
        }
    });

What is going on i don't know!! Code is 100000% ok. Nothing wrong.

Upvotes: 1

Sathish Kumar J
Sathish Kumar J

Reputation: 4345

Try this,

text1 = (TextView) findViewById(R.id.textView1);

btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            text1.setText("yes");
        }
    });

Note : Best way is, Initialize all UI Components at Outside of the listeners.

Upvotes: 1

Seyyed
Seyyed

Reputation: 1534

Changing the button position in XML only changes the View of your activity and can't resolve your problem.

Your are instantiating the TextView and setting a view to it in every click of button(!)
Instead of this, you have to instantiate the TextView like as your button and in your button clickListener just call setText("yes").

public class TestAppActivity extends Activity {

    TextView text1;
    Button   btn1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn1 = (Button) findViewById(R.id.button1);
        text1 = (TextView) findViewById(R.id.textView1);

        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                text1.setText("yes");
            }
        });
    }
}

Upvotes: 1

Related Questions