Q:

Change different android layouts using Thread.sleep

I want to change the layout of the main activity after 10 secs of sleep. I have created two xml files along with activity_main.xml(linear.xml,relative.xml).When I first call the method on button click, the last layout that is set is only displayed. I want linear layout (linear.xml) to be displayed and then after 10 sec it should change to relative layout(relative.xml).
I tried using a array to store layouts and then use a variable to switch the contents by passing the variable in setContentView() method ,but that did not work (neither layout was loaded).
Can you specify a solution or a way to accomplish this ?
Thanks 
Following is MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void change(View v)
{

    try
    {
        Thread.sleep(5000);
        Toast.makeText(this,"linear layout",Toast.LENGTH_LONG).show();
        setContentView(R.layout.linear);

    }
    catch(InterruptedException e)
    {
        e.printStackTrace();
    }

    try
    {
        Thread.sleep(5000);

       Toast.makeText(this,"relativelayout",Toast.LENGTH_LONG).show();
        setContentView(R.layout.relative);

    }
    catch(InterruptedException e)
    {
        e.printStackTrace();
    }

   }
}

Following is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.androidcodes.layoutsapp.MainActivity">

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="170dp"
    android:text="Button"
    android:onClick="change"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

A:

Try using a Handler
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        viewLinear = getLayoutInflater().inflate(R.layout.layout1, null);
        viewRelative = getLayoutInflater().inflate(R.layout.layout2, null);
        this.handler = new Handler();
        this.handler.postDelayed(runnable, 5000);

    }//onCreate

    private final Runnable runnable = new Runnable() {
        public void run() {
            setContentView(viewLinear);//update
            this.handler.postDelayed(runnable, 5000);
        }
    };//runnable

