3

Hello guys I'm trying to update the UI after a short delay and for this I'm using handler's postdelayed method. The following code initially sets my textview text to "Processing" and the code that's included in the the handler's runnable gets executed but doesn't update the UI? Please note that this is done in a Fragment

TextView progressText=(TextView)parent.findViewById(R.id.inProgressText);
progressText.setText("Processing");

getActivity().getMainLooper()).postDelayed(new Runnable() {
    @Override
    public void run() {
        TextView progressText=(TextView)parent.findViewById(R.id.inProgressText);
        progressText.setText("Request completed");              
    }
}, 3000);

Thanks for your help

Adi
  • 197
  • 5
  • 16
  • whats wrong with that code? – pskink Jan 29 '14 at 14:29
  • @pskink This code initially sets my textview text to "Processing" but the code thats included in the the handler runnable gets executed but doesnt update the UI? – Adi Jan 29 '14 at 14:32
  • Could it be once your using `parent.findViewById(R.id.inProgressText);` and the next time your using `uiObject.findViewById(R.id.inProgressText);`? – TMH Jan 29 '14 at 14:35
  • @ Tom Hart sorry that's just a typo. Ive just updated my post – Adi Jan 29 '14 at 14:38

3 Answers3

4

Change

getActivity().getMainLooper()).postDelayed(new Runnable()

to:

final Handler handler = new Handler();

Then

handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        final TextView progressText=(TextView)findViewById(R.id.inProgressText);
        progressText.setText("Request completed");              
    }
}, 3000);
Melquiades
  • 8,496
  • 1
  • 31
  • 46
  • Initially started with the same which didn't work and later changed my code to what you see right now. – Adi Jan 29 '14 at 14:41
1

Use this code

public class MainActivity extends Activity {
TextView progressText = null;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_main, container, false);

    progressText = (TextView) rootView.findViewById(R.id.inProgressText);
    progressText.setText("Processing");
    progressText.postDelayed(new Runnable() {
        @Override
        public void run() {
            progressText.setText("Request completed");
        }
    }, 3000);
       return rootView;
}
}
Rajan
  • 1,069
  • 1
  • 9
  • 17
0

Please try this way : I checked in Fragement and it is working perfectly.

View parent= inflater.inflate(R.layout.your_fragement, container, false);
TextView progressText=(TextView)parent.findViewById(R.id.inProgressText);
progressText.setText("Processing");

            progressText.postDelayed(new Runnable() {
                @Override
                public void run() {
                    progressText.setText("Request completed");
                }
            }, 3000);
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39