I'm new to AndroidStudio and am having trouble finding a way to change views without requiring action from the user. More specifically, I'm trying to make my app display a title screen when it opens, then switch to the main interface after a few seconds. I found this code for changing views from the current view to the DisplayMessageActivity class in the android tutorials:
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
I tried to use this code in my onCreate method of my title screen Activity as such:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title_screen);
try {
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
}
Then I set up the second view in the new Activity. I also tried to just use two separate views in the onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title_screen);
try {
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
setContentView(R.layout.main_menu);
}
However, in both cases, it only displays the second view. Where am I going wrong?
You are using same layout in both activities, fix that (R.layout.....)
Avoid Thread.sleep as it will block the app itself. you have to introduce delay when calling the second activity. pls refer https://www.youtube.com/watch?v=LCLO7q2uhOs
Your following code should be put in a handler block with the time delay you want
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);