3

someone can tell me how to update a control Textview Android from a function? I have searched deep into the internet and see many people who ask the same question, I tested threads but could not work, someone has a simple working example of doing this? for example to call a function (which runs several times in a loop) and the function writes in the TextView, but the problem is that until the function is not finished running, it shows me the text.

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

    while(condition) //here freezes the UI and the text in textview only shows when the loop ends
    {
        HardWork();  
    }

}

public void HardWork() {  

    txtProgreso.append("Test Line" + "\n\n");

};

Thanks in advance.

seba123neo
  • 4,688
  • 10
  • 35
  • 53
  • Could you post some code that you attempted? I'm not sure I understand exactly what you are trying to do. – Corey Sunwold Nov 26 '10 at 01:52
  • OK, I edited the post and add the code sample... – seba123neo Nov 26 '10 at 02:52
  • @seba123neo pls refer to this link http://stackoverflow.com/questions/6898371/update-textview-in-oncreate-android i am also facing same proble can you help me pls..and provide me good solution...thanks – SRam Aug 02 '11 at 05:23

3 Answers3

5

If i understood you correctly
Use AsyncTask here, So you can update textview in onProgressUpdate method

private class SomeTask extends AsyncTask<String, String, String> {
    @Override
        protected String doInBackground(String... params) {
         while(condition) {
         //do more
         publishProgress("Some text value to you textview");
        }
         return null;
     }

     @Override
     protected void onProgressUpdate(String... values) {
         txtProgreso.append(values[0]);
     }


 }
Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
3

I think what you're trying to ask is "how do I force my TextView to show its updated contents without returning control to the runloop?" (ie, returning out of your function).

If so, I'm afraid you're out of luck, such a thing is impossible to do with Android's UI model. The nearest thing to this that is possible is to keep track of your loop state, and set a timer that will call a function for you to update the TextView.

Here's an example. Suppose you want to put the text "this is a test" into a TextView, one character at a time:

private Handler mHandler = new Handler();
private String desiredText = new String("This is a test");

private Runnable mUpdateTextView = new Runnable() {
  public void run() {
    TextView textView = (TextView)findViewById(R.id.myTextView);
    int lengthSoFar = textView.getText().length();
    if (lengthSoFar < desiredText.length()) {
      mTextView.setText(desiredText.substring(0, lengthSoFar + 1));
      mHandler.postDelayed(100, mUpdateTextView);
    }
  }
};

protected void onStart() {
  mHandler.postDelayed(100, mUpdateTextView);
}

I think some of my object visibility is incorrect here, and I don't have an Android environment handy to test on, but you get the general idea. Rather than a Handler you could also use a Timer, but it's a bit more heavyweight so it depends how often you want to be updating the UI.

rwat
  • 833
  • 5
  • 10
1

You get the textview by doing the following.

TextView myTextView = (TextView)findViewById(R.id.textViewsId);

Then afterwards, you can change the TextView using the functions. For example, you can set the text using the following code.

myTextView.setText("New text example");

Are there any other updates to the control you are trying to do?

dlongest
  • 462
  • 3
  • 3
  • thanks, I know that, the problem is that I run a function, this function writes the TextView several lines of text, but when I run this function freezes the UI and do not show me the text until it ends ... – seba123neo Nov 26 '10 at 02:09