Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Runnable Thread in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 164
    Comment on it

     

    In the below code I have created Runnable Thread program in android. Here first I have added TextView in activity_main.xml layout. In MainActivity I have used handler class then I have created a new Runnable interface , And I have also used onStart (),run() and update() method. You can see below example code it clearly describe you How to make Runnable Thread in android.

    Step(1)activity_main.xml layout-

    <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text=" Runnable Thread"
            android:textAlignment="center"
            android:textSize="20dp"/>
        <TextView
            android:id="@+id/one"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
    </LinearLayout>


    Step(2)- MainActivity-
     

    public class MainActivity extends AppCompatActivity {
            int i = 0;
            TextView raj;
    
            Handler handler = new Handler(){
    
                @Override
                public void handleMessage(Message msg) {
                    // TODO Auto-generated method stub
                    update_i();
                }
            };
    
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
                raj =(TextView)findViewById(R.id.one);
            }
    
            @Override
            protected void onStart() {
                // TODO Auto-generated method stub
                super.onStart();
    
                Thread myThread=new Thread(new Runnable() {
                    public void run() {
                        while(true){
                            try {
                                handler.sendMessage(handler.obtainMessage());
                                Thread.sleep(1000);
                            }
                            catch (Throwable t) {
                            }
                        }
                    }
                });
    
                myThread.start();
            }
    
            private void update_i()
            {
                i++;
                raj.setText(String.valueOf(i));
            }
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: