ImageView in SplashScreen is not showing - android

I am very new to android and trying to add a splash screen I am half way succeeded. But a weird think happening that surely an easy one. here i have tried :-
I wanted to stop the SplashScreen for some time. This splash screen layout contain a ImageView that show the appLogo.
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.SplashScreen);
ImageView splashScreenImage = FindViewById<ImageView>(Resource.Id.appLogo);
splashScreenImage.SetImageResource(Resource.Drawable.splLogo);
Thread.Sleep(30000);
StartActivity(typeof(MainActivity));
}
}
Actualy my splash screen waiting stopping for some time but ImageView is not showing it come out at the last moment when new activity is going to start.
Why is this happening ? any help is appreciated :)

problem:
Thread.Sleep(30000);
It is not showing because you are blocking your UI thread to process the SetContentView to display in the screen thus it is not showing.
What it is really doing is that it will wait for 30 second without the display/black screen and change activity.
Solution:
Use a timer or Handler instead of sleeping the thread.

Related

Xamarin Splash screen example does not work in landscape mode on a phone. How to fix it?

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();
}

Android AnimationDrawable start

I'm using AnimationDrawable to show missing network connection.
Show/hide logic is linked to network status change receiver. It works fine.
But when start activity knowing status and try to start animation - animated drawable shows and freezes on first frame. I've read in documentation - 'do not start animation in OnCreate'.
So I wrote code in onResume, but animation still not playing - only shows first frame.
Starting from button or event works fine.
Tried to start with separate thread and wait some time - but this doent sounds good.
Any idea?
This code works when called from net status change handler
private void _NetStatus(boolean start)
{
if (start)
{
m_NetStatus.setVisibility(View.VISIBLE);
m_NetStatusFrameAnimation.start();
}
else
{
m_NetStatusFrameAnimation.stop();
m_NetStatus.setVisibility(View.INVISIBLE);
}
}
Hmm. After trying some samples advising me to use new Runnable at end of onCreate - I tried to start animation when activity shows on screen:
#Override
public void onWindowFocusChanged(boolean hasFocus)
Works fine for now.

Android Dev: Android Layout not being drawn in time

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();
}

how to show progress dialog in oncreate in android app

In onCreate() of the activity, I'm force changing the orientation of the activity from portrait to landscape. This is working but there is a small delay of about a sec in the emulator. This activity is actually one of the tabs. So when the user clicks on the tab he would expect an immediate response. So I want to show a progress dialog until the view is completely loaded. How do I achieve this? I tried to do the below but it doesn't work,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chart);
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Loading...");
dialog.show();
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
.
.
.
dialog.dismiss();//after the view is loaded completely }
Any suggestion is appreciated.
Actually, I don't think your application is visiable yet .. not at the onCreate(). Check the eventing lifecycle ... onResume or maybe onStart ... where ever your Acitivity is actually visiable is where you should do this.

How to make a splash screen (screen visible when app starts)?

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.

Categories

Resources