I have created an android application which should run on my Android based phone. As soon as the application is installed on the phone it proceeds with the task it is designed for. I want that as application is installed it should first open a welcome page displaying information about the product and on clicking OK button it should proceed to it's task.
To do this you have to create a Splash Screen layout and associated activity. You have to use a Timer or Thread to handle it. First create a splash.xml file and also create an activity SplashActivity.java.
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash.xml);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
startHomeActivity(); // start home after 3 seconds
}
}, 3000); // three seconds wait, you can change it
}
}
Hope it will help you.
Since you are very much new to android development, i suggest you to learn more on android from Android developer website, you will get all api level explanation with examples. And here the link to know step by step implementation of creating splash screen in you android application. Keep leaning, try to search from website, you will get plenty of references. :)
Related
What I want to do:
I want to have multiple activities each prefaced with a page explaining to the user what the activity is about.
What I'm currently doing:
So my main class BaseModuleActivity extends Activity and I am trying to write a function called showTutorial() which will explain the next steps to the users.
Here is my attempt in doing so:
public void showTutorial(String title, String explanation){
setContentView(R.layout.tutorial_screen);
TextView tv1 = (TextView)findViewById(R.id.tutoTextTitle);
tv1.setText(title);
TextView tv2 = (TextView)findViewById(R.id.tutoTextExplanation);
tv2.setText(explanation);
findViewById(R.id.tutoButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
//remove the tutorial's view
findViewById(R.id.tutoLayout).setVisibility(View.GONE);
}
});
}
And this method is called in the following:
public class myFirstActivity extends BaseModuleActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
//First show tuto
super.showTutorial(getString(R.string.upTitle),getString(R.string.upExplanation));
//TODO then actually do the activity stuff
/*
(findViewById(R.id.next_button)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
*/
}
}
Problem:
I think the problem is mainly conceptual. I don't know the best approach to do this and the approach I'm taking is not working.
What I'm doing is not working because the view just become empty. I thought setting the visibility of the linearLayout to gone would make it disappear and the actual activity will able to take place.
What I need:
I need to understand if I can do what I want with my approach or not? Or what approach should I take.
I found some similar questions. However, the answer to these questions didn't seem to fit my problem.
I also looked into layout inflater and fragment, but layout inflater seem to be more for listView and fragment uses layout inflater.
Well, there are some approaches to show a guide for your activity (or application).
First one, and probably the easiest, is to show a dialog/TextView when user enters an activity and explain the activity guide in that dialog/TextView using plain text. From your explanation, I think this one is what your are trying to do.
Second one is to use something like slides with pictures to explain about your activity (like Google Sheets application).
Third one is to explain each control in your activity separatly by highlighting them (similar to how Go Launcher explains its feature on first launch)
You can find more info in below links:
How to implement first launch tutorial like Android Lollipop apps: Like Sheets, Slides app?
Android - first launch interactive tutorial
Seems that what you want is actually an introduction. Take a look at this project:
https://github.com/rubengees/introduction
From each introduction page you can launch the correspondent activity.
I downloaded the splash screen example from the Xamarin website:
http://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/
I compiled it and ran it on my phone:
http://www.gsmarena.com/samsung_galaxy_fresh_s7390-5841.php
It was working fine when holding my phone in portrait mode (vertical). The splash screen became directly visible and after a few seconds, the view with the button became visible. When closing and restarting the application, it was still working fine.
After that, I closed it again and hold my phone in landscape (horizontal) mode. Now, I started the application again. My phone was frozen for a few seconds, the splash did not become visible. After that, I saw my view with the button.
When you try to reproduce this issue, make sure that you:
Do not try to reproduce it on a virtual device (the behavior is different).
Make sure that the sleep takes at least 10 seconds, then you really see what the problem is: a frozen application instead of a splash screen.
If you do not have the Samsung Trend Lite, you try it on another small smart phone. I find it hard to imagine that this could be a "Samsung Trend Lite only" issue.
How do I fix this?
The Xamarin sample that you linked has big problem within it:
[Activity(Theme = "#style/Theme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Thread.Sleep(10000); // Simulate a long loading process on app startup.
StartActivity(typeof(Activity1));
}
}
The OnCreate() method of an Activity is executed on the UI thread therefore calling a Thread.Sleep() inside it will lock up the main thread, possibly generating an Application Not Responding (ANR) to be display to the user.
This is fault in the Xamarin docs, you should not run a Thread.Sleep() on the UI thread, especially within one of the core lifecycle callbacks for an activity.
Fix this by using a background thread to execute the sleep and then call back into the splash activity to launch the next activity:
[Activity(Theme = "#style/Theme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
System.Threading.Tasks.Task.Run( () => {
Thread.Sleep(10000); // Simulate a long loading process on app startup.
StartActivity(typeof(Activity1));
});
}
}
I just discovered another solution. It is a bit strange to use a timer but it solves the problem.
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
System.Timers.Timer t = new System.Timers.Timer(1);
t.Elapsed += (o, e) =>
{
t.Stop();
Thread.Sleep(10000); // Simulate a long loading process on app startup.
StartActivity(typeof(Activity1));
};
t.Start();
}
I'm using Crouton im my project.In app i have few activities.
In sub activities i want do some operations after finishing a process i make redirect to main activity and display Crouton message about result in previous activity.
But hier i have problems...Crouton closes quickly after redirect. I found some solution:
1) Create intent for sub activity
2) close main activity
3) start sub activity
do some work...
4) Create intent for main activity
5) Put info in extra about changes
5) open main activity
and in main activity, in onCreate, i trying get extra about changes. I tried get info from extra in onStart, onResume..but still the same..Crouton message closes quickly, faster than needed.
What i am doing wrong?
Maybe my algorithm with displaying Crouton is wrong..Pls correct me.
Thx a lot!
You have two options:
1) You can open your second activity with startActivityForResult() and than handle the result in your MainActivity to show the crouton. In the Android docs you can find a good tutorial for it: Android developer After ending the second activity you handle the result code and show the right text.
2) You can send it as you mentioned in a Extra with startActivity() and pass the in the intent:
start activity
Hope it helps!
I found solution to my question:
#Override
protected void onResume() {
super.onResume();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
displayEvents();
}
}, 500);
}
private void displayEvents(){
//Проверяю какое уведомление необходимо показать
if(intent.hasExtra("event")){
if(intent.hasExtra("event") && intent.getStringExtra("eventType").equals("confirm")) {
Crouton.makeText(MainActivity.this, intent.getStringExtra("event"), Style.CONFIRM).show();
getIntent().removeExtra("event");
getIntent().removeExtra("eventType");
}
}
}
done!
I'm having trouble putting this problem into searchable terms. I'm working on an Android application, and specifically the splash screen for my app. The app needs to fetch data from an external web service (a blocking function call), while it does this the user gets a nice title, image and progress bar. When the data arrives the user is redirected to the main menu. Its a simple screen, everything being defined in the xml layout file, my problem is that I just get a black screen for a few seconds and then the main menu. If I press back I get the splash screen with the progress bar spinning away happily.
Here is what I have so far:
public class SplashActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
#Override
public void onStart(){
super.onStart();
DatabaseManager db = new DatabaseManager(this.getBaseContext());
db.fetchExternCatalog(); //doesnt return until data arrives
Intent intent = new Intent().setClass(this, MainMenuActivity.class);
startActivity(intent);
}
}
It seems the screen isnt actually drawn until the activity is running (after onCreate(), onStart(), etc). I thought onStart() would be the perfect place to put this, but apparently not.
So how do I draw everything on the screen and make my blocking function call after so the user actually sees the splash screen while the data is downloaded?
You're going to be locking up the UI thread which is why i believe you are seeing a black screen. Use an AsyncTask or create your own thread pool for DB operations.
As far as hitting the back button and seeing your old activity, You need to tell android not to store the activity in it's stack. This should help:
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
you need to use the ProgressDialog class to build the dialog, and then run the blocking method inside a thread.
I'll post an example in a minute (gotta get near a PC :p)
private void showSplash(){
progressDialog = ProgressDialog.show(this, "Hello! ima title", "Im the message you see.");
progressDialog.show();
Thread t = new Thread(new Runnable(){
public void run(){
// Put your blocking method here.
// You may need to build in a "hey, im done downloading" variable to get it to close down right
progressDialog.dismiss();
}
});
t.start();
}
I have a simple application, it starts, loads xml feed from the net, you can browse a list of news and then read details for a chosen news item. What I would like to do is have a splash screen, meaning as soon as you click application, it should display an image (app name in my case) and then display news list only after they've loaded.
I read about similar (I think) problems, and usually people say to use FrameLayout, but I can't really sort it out. I'm not sure if this can be done in the first activity that is launched, maybe I should just display this splash image in one activity and only then call activity displaying my news list?
I know that on iPhone you can set splash screen in app settings while developing, would be nice to have this functionality in android's app's manifest...
Android suggests you take advantage of using a splash screen when performing lengthy calculations on start up. Here's an excerpt from the Android Developer Website - Designing for Responsiveness:
"If your application has a time-consuming initial setup phase, consider showing a splash screen or rendering the main view as quickly as possible and filling in the information asynchronously. In either case, you should indicate somehow that progress is being made, lest the user perceive that the application is frozen." -- Android Developer Site
You can create an activity that shows a Progress Dialog while using an AsyncTask to download the xml feed from the net, parse it, store it to a db (if needed) and then start the Activity that displays the News Feeds. Close the splash Activity by calling finish()
Here's a skeleton code:
public class SplashScreen extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// set the content view for your splash screen you defined in an xml file
setContentView(R.layout.splashscreen);
// perform other stuff you need to do
// execute your xml news feed loader
new AsyncLoadXMLFeed().execute();
}
private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute(){
// show your progress dialog
}
#Override
protected Void doInBackground(Void... voids){
// load your xml feed asynchronously
}
#Override
protected void onPostExecute(Void params){
// dismiss your dialog
// launch your News activity
Intent intent = new Intent(SplashScreen.this, News.class);
startActivity(intent);
// close this activity
finish();
}
}
}
hope that helps!
I know this is old but for those of you who are still facing this problem, you can use this simple android-splash library to show your splash screen.
SplashBuilder
.with(this, savedInstanceState)
.show();
You can set SplashTask that will execute while the splash screen is displayed.