0

So I am trying to delay the textview (tvMessage3) 2 times in my code when the button is pressed, but it doesn't work. Therefore all I see is "exiting good bye" text but not "Dog Found" text. Also I am unable to change the button text, once it is clicked. Any suggestions? Plus no errors in logcat.

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

    <SurfaceView 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"        
        android:id="@+id/surface_camera" />

    <teaonly.droideye.OverlayView
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:background="#00000000" 
        android:id="@+id/surface_overlay"/>

    <FrameLayout
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">

        <LinearLayout 
            android:id="@+id/layout_setup" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:gravity="center|bottom" 
            android:orientation="vertical">

           <LinearLayout
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent"
                android:gravity="center"
                android:background="#88333333"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/tv_message2"
                    android:visibility="gone"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft = "5dip"
                    android:layout_marginRight = "5dip"
                    android:gravity="center"
                    android:textColor="#FFFFFFFF"
                    android:textSize="24dip"/>



            </LinearLayout>

            <TextView
                android:id="@+id/tv_message1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dip"
                android:layout_marginRight="5dip"
                android:gravity="center"
                android:textColor="#FFFFFFFF"
                android:textSize="24dip" />

            <LinearLayout
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent"
                android:gravity="center"
                android:background="#88333333"
                android:orientation="horizontal">
                <!--
                <Button 
                    android:id="@+id/btn_setup"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_margin = "8dip"
                    android:textSize="24dip"
                    android:textStyle="bold"            
                    android:text="@string/action_setup"/>
                -->
                <Button 
                    android:id="@+id/btn_exit"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_margin = "8dip"
                    android:textSize="24dip"
                    android:textStyle="bold"            
                    android:text="@string/action_exit"/>
                <TextView
                    android:id="@+id/tv_message3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="9dip"
                    android:layout_marginRight="9dip"
                    android:gravity="center"
                    android:textColor="#FFFFFFFF"
                    android:textSize="25dip" />

            </LinearLayout>

        </LinearLayout>

     <!--    <TextView
            android:id="@+id/tvLocation"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:gravity="top|left"
            android:textColor="#FFFFFFFF"
            android:textSize="24dip" /> -->

    </FrameLayout>    

</FrameLayout>

Here is my src file:

public void onButtonPress() {

btnExit.setText("Recognizing ...");
tvMessage1.setVisibility(View.GONE);
btnExit.setVisibility(View.GONE);
tvMessage3 = (TextView)findViewById(R.id.tv_message3);
tvMessage3.setTextColor(Color.RED);
tvMessage3.setText("Recognizing ........");

       try{
          Thread.sleep(5000);
       }catch(InterruptedException ex){
          ex.printStackTrace();
       }
       tvMessage3.setTextColor(Color.GREEN);
       tvMessage3.setText("Dog Found");

       android.os.SystemClock.sleep(5000);
       tvMessage3.setText("exiting good bye");

}

 private OnClickListener exitAction = new OnClickListener() {
@Override
public void onClick(View v) {
    onButtonPress();
    onPause();
}   
};

   @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);    
//win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

setContentView(R.layout.main);

//setup adView
LinearLayout layout = (LinearLayout)findViewById(R.id.layout_setup);
adView = new AdView(this, AdSize.BANNER, "a1507f940fc****");
layout.addView(adView);
adView.loadAd(new AdRequest());

btnExit = (Button)findViewById(R.id.btn_exit);
btnExit.setVisibility(1);
btnExit.setOnClickListener(exitAction);
tvMessage1 = (TextView)findViewById(R.id.tv_message1);
tvMessage2 = (TextView)findViewById(R.id.tv_message2);

1 Answers1

0

The problem is that the following instruction

      tvMessage3.setText("exiting good bye");

is writing other the result of your previous instruction

      tvMessage3.setText("Dog Found");

So the problem is not that the program doesn't write to your textview, but that you are writing over the content of your textview.

If you need to show once the first text and then the second text I suggest you to use asynctask that is the simplest way to avoid problem with UI thread.

If you need this do something like this

  private class ExampleTask extends
        AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {

            tvMessage3.setText("Dog Found"); 

        }

        @Override
        protected Void doInBackground(final Void... params) {

            Thread.sleep(5000);

        }

        @Override
        protected void onPostExecute(final Void result) {

            tvMessage3.setText("exiting good bye");

        }

  }

Hope it's clear;)

axl coder
  • 739
  • 3
  • 19
  • I am not a great coder and learning this as a part of my job. If my class is already extending Activity how can I extend AsyncTask? – user3836681 Jul 14 '14 at 15:01
  • Is not as difficult as it seems, take a look at this: http://developer.android.com/reference/android/os/AsyncTask.html. Anyway, just put the code I have pasted inside your class and when you need call it as follow: new ExampleTask() .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); – axl coder Jul 14 '14 at 15:24