Dynamically changing the text of a TextView

12,211

Solution 1

i didn't understand why you are calling the method invalidate() on your TextView, otherwise ,a simple code like this should work (add this code in the onCreate() method) :

setContentView(R.layout.main);
TextView lblSlogan = (TextView) findViewById(R.id.lblSlogan);
Button btnChangeSlogan = (Button) findViewById(R.id.btnChangeSlogan);

btnChangeSlogan.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
       lblSlogan.setText("Put your new text here");// cal setText() in the onclick method when ever you want to change the text 
   }
});

Solution 2

I think your problem is this:

answerBox.Text.ToLower() == currentSlogan.Company.ToLower()

you should use "equals", not "==".

(answerBox.Text.ToLower()).Equals( currentSlogan.Company.ToLower())

Solution 3

A couple of points here.

Personally I use the built in abstract methods that Xamarin provides. They tend to give me much more consistent results. You can simply assign the new value to the .Text property of the Textview. IE

textView.Text = newValue;

In C# you do not need to use the .Equals operator to do string comparisons. That's strictly a Java requirement. See this [link] (Why would you use String.Equals over ==?).

Share:
12,211
user1704677
Author by

user1704677

Updated on June 05, 2022

Comments

  • user1704677
    user1704677 almost 2 years

    I have a TextView that starts out with a default text value, and then based on what a user does, that TextView's text needs to change in the code when a button is clicked. Seems simple enough, yet I'm running into problems.

    Currently, what is happening is when the user clicks the submit button which triggers the change of the text, the new text is just being added to the screen under the original TextView instead of just simply changing the text value. It's almost as if it's adding a new TextView.

    Here is the code that does this:

    lblSlogan.Invalidate();
    lblSlogan.SetText(currentSlogan.Slogan, TextView.BufferType.Normal);
    

    I also tried it this way, with no luck:

    lblSlogan.Invalidate();
    lblSlogan.Text = currentSlogan.Slogan;
    

    lblSlogan is a TextView. Am I missing something? I also tried it without the invalidate(), but that changed nothing either.

    Thanks.

    -- edit --

    It's important to note that I'm using C# with Xamarin. Not Java. Here is my click method for the button. This is where the TextView change happens.

    btnOk.Click += delegate(object sender, EventArgs e)
          {
                if (answerBox.Text.ToLower() == currentSlogan.Company.ToLower())
                {
                    // correct answer
                    currentUserScore += currentSlogan.Points;
                    currentSlogan.Answered = true;
                    DatabaseBuffer.MarkSloganAnsweredAndUpdateScore(currentSlogan, currentUserScore);
                    currentSlogan = DatabaseBuffer.GetNextUnansweredSlogan(currentSlogan.ID);
                }
    
                if (currentUserScore >= pointsToPass)
                {
                    // user has beaten level
                }
                else
                {
                    lblSlogan.SetText(currentSlogan.Slogan, TextView.BufferType.Normal);
                    answerBox.Text = "";
                }
            };