Android: Keyboard shown in the splash screen - android

I wrote a splash screen. But the problem is as the splash screen shown in the screen a keyboard also invoked. What could be the possible reason for this??
Please find the image below
and the code gos as below for the activity
public class SplashActivity extends Activity{
private final int SPLASH_DISPLAY_LENGHT = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent splash2 = new Intent(SplashActivity.this,SplashActivityRed.class);
SplashActivity.this.startActivity(splash2);
SplashActivity.this.finish();
overridePendingTransition(R.anim.fadein,R.anim.fadeout);
}
}, SPLASH_DISPLAY_LENGHT);
}
}
and the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="#+id/imageViewSplash" android:layout_height="fill_parent" android:layout_width="fill_parent" android:background="#drawable/splash1" android:src="#drawable/splash1"></ImageView>
</LinearLayout>
PS: Sorry I had to hide the text and logo as they come under non-disclosure policy of the company I work for.

Please Remove the imageView from the Layout and add the following line the Linear Layout element
android:background="#drawable/splash1"
Like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background:"#drawable/splash1">
</LinearLayout>
Hope this helps
Also Change the implementation of the SplashScreen with above layout. To change the time line change the value of welcomeScreenDisplay to wotever you want. Currently it is 4 seconds i.e. 4000 mili seconds.
public class SplashScreen extends Activity {
String status, subscriber;
Intent i = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("FIRST");
setContentView(R.layout.splash);
System.out.println("in HOME SCREEN");
/** set time to splash out */
final int welcomeScreenDisplay = 4000;
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
#Override
public void run() {
try {
super.run();
/**
* use while to get the splash time. Use sleep() to increase
* the wait variable for every 100L.
*/
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
/**
* Called after splash times up. Do some action after splash
* times up. Here we moved to another main activity class
*/
finish();
}
}
};
welcomeThread.start();
}
}

Use this method:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_‌​HIDDEN);

probably you need to disable the touch event for image. However as remove the imageView from the Layout and add background image to the linear layout element
android:background="#drawable/splash1"

Related

Android - ImageView on splash screen doesn't work

I wanna make splash screen while loading the main activity.
So, I added Splash activity.
package com.originerd.tau;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread logoTimer = new Thread(){
#Override
public void run() {
// TODO Auto-generated method stub
try {
Intent i = new Intent(Splash.this, Main.class);
startActivity(i);
sleep(4500);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
}
and activity_splash xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash" />
But when I executed it, the screen shows just black window and shows main activity after 3 seconds. I think it is becuse of image loading time of splash activity. So I added sleep(1000); before Intent i = new Intent(Splash.this, Main.class); line. And it works, but I think it is not that good solution.
I wanna know what is good solution at this situation. The purpose is showing up image while main activity is preparing the contents(It takes around 3 seconds). If there are any solutions(Loading image) instead of splash screen, please let me know.
Instead of using sleep method in Thread you can do same easily using Handler.postDelayed as:
Handler handler = new Handler();
handler.postDelayed(runnable, 1000);
Runnable runnable = new Runnable() {
#Override
public void run() {
Intent i = new Intent(Splash.this, Main.class);
startActivity(i);
handler.removeCallbacks(runnable);
}
};
How large (bytes) is your image? Perhaps the delay could be minimized by shrinking the image size/complexity with graphics tools. As a test, you could try a single color image which should be quite small. That would at least tell you whether the delay is a function of image size.

Add the loading screen in starting of the android application

My app is loading the start page in 10 seconds. In that time of 10 sec android screen is blank.
In that time I want to add the loading screen. How to add it?
And tell me in app how to know the starting page is loading? And tell me how to do in my app?
use ProgressDialog.
ProgressDialog dialog=new ProgressDialog(context);
dialog.setMessage("message");
dialog.setCancelable(false);
dialog.setInverseBackgroundForced(false);
dialog.show();
hide it whenever your UI is ready with data. call :
dialog.hide();
You can use splash screen in your first loading Activity like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread welcomeThread = new Thread() {
#Override
public void run() {
try {
super.run();
sleep(10000); //Delay of 10 seconds
} catch (Exception e) {
} finally {
Intent i = new Intent(SplashActivity.this,
MainActivity.class);
startActivity(i);
finish();
}
}
};
welcomeThread.start();
}
Hope this code helps you.
Please read this article
Chris Stewart wrote there:
Splash screens just waste your time, right? As an Android developer,
when I see a splash screen, I know that some poor dev had to add a
three-second delay to the code.
Then, I have to stare at some picture for three seconds until I can
use the app. And I have to do this every time it’s launched. I know
which app I opened. I know what it does. Just let me use it!
Splash Screens the Right Way
I believe that Google isn’t contradicting itself; the old advice and
the new stand together. (That said, it’s still not a good idea to use
a splash screen that wastes a user’s time. Please don’t do that.)
However, Android apps do take some amount of time to start up,
especially on a cold start. There is a delay there that you may not be
able to avoid. Instead of leaving a blank screen during this time, why
not show the user something nice? This is the approach Google is
advocating. Don’t waste the user’s time, but don’t show them a blank,
unconfigured section of the app the first time they launch it, either.
If you look at recent updates to Google apps, you’ll see appropriate
uses of the splash screen. Take a look at the YouTube app, for
example.
You can create a custom loading screen instead of splash screen. if you show a splash screen for 10 sec, it's not a good idea for user experience. So it's better to add a custom loading screen. For a custom loading screen you may need some different images to make that feel like a gif. after that add the images in the res folder and make a class like this :-
public class LoadingScreen {private ImageView loading;
LoadingScreen(ImageView loading) {
this.loading = loading;
}
public void setLoadScreen(){
final Integer[] loadingImages = {R.mipmap.loading_1, R.mipmap.loading_2, R.mipmap.loading_3, R.mipmap.loading_4};
final Handler loadingHandler = new Handler();
Runnable runnable = new Runnable() {
int loadingImgIndex = 0;
public void run() {
loading.setImageResource(loadingImages[loadingImgIndex]);
loadingImgIndex++;
if (loadingImgIndex >= loadingImages.length)
loadingImgIndex = 0;
loadingHandler.postDelayed(this, 500);
}
};
loadingHandler.postDelayed(runnable, 500);
}}
In your MainActivity, you can pass a to the LoadingScreen class like this :-
private ImageView loadingImage;
Don't forget to add an ImageView in activity_main.
After that call the LoadingScreen class like this;
LoadingScreen loadingscreen = new LoadingScreen(loadingImage);
loadingscreen.setLoadScreen();
I hope this will help you
public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 3000; //set your time here......
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
If the application is not doing anything in that 10 seconds, this will form a bad design only to make the user wait for 10 seconds doing nothing.
If there is something going on in that, or if you wish to implement 10 seconds delay splash screen,Here is the Code :
ProgressDialog pd;
pd = ProgressDialog.show(this,"Please Wait...", "Loading Application..", false, true);
pd.setCanceledOnTouchOutside(false);
Thread t = new Thread()
{
#Override
public void run()
{
try
{
sleep(10000) //Delay of 10 seconds
}
catch (Exception e) {}
handler.sendEmptyMessage(0);
}
} ;
t.start();
//Handles the thread result of the Backup being executed.
private Handler handler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
pd.dismiss();
//Start the Next Activity here...
}
};
Write the code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread welcomeThread = new Thread() {
#Override
public void run() {
try {
super.run();
sleep(10000) //Delay of 10 seconds
} catch (Exception e) {
} finally {
Intent i = new Intent(SplashActivity.this,
MainActivity.class);
startActivity(i);
finish();
}
}
};
welcomeThread.start();
}

Splash Screen on Android not working

I'm developing an Android app.
The app should show a Splash Screen at startup while checking if a file is updated. If the file is not updated, it launches an Async Task to update the file.
The problem is, the image of the Splash Screen only shows when the file actually needs updating. Else, a black screen shows while performing the check.
My SplashScreen activity:
public class SplashActivity extends Activity
{
private final static String placesFile = "places";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
}
#Override
protected void onResume()
{
super.onResume();
if(!isFileUpdated()){
new PlacesService(this).execute();
}else{
intentAndFinish();
}
}
private void intentAndFinish() {
finish();
Intent mainIntent = new Intent(this, MainActivity.class);
startActivity(mainIntent);
}
/**
*
* #return false if Places Data is too old
*/
private boolean isFileUpdated() {
int daysOld = 0;
File f = new File(this.getFilesDir().getAbsolutePath() +"/"+placesFile);
if(f.exists()){
System.out.println("existe");
}
Date d = new Date();
Date currentDate = new Date(System.currentTimeMillis());
d.setTime(f.lastModified());
if(currentDate.compareTo(d)>0)
daysOld = determineDifferenceInDays(d, currentDate);
return daysOld < Consts.PLACES_DAYS_OLD_QTY_PERMITTED?true:false;
}
private static int determineDifferenceInDays(Date date1, Date date2) {
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
long diffInMillis = calendar2.getTimeInMillis() - calendar1.getTimeInMillis();
return (int) (diffInMillis / (24* 1000 * 60 * 60));
}
public void onResultFromAsyncTask(boolean finished) {
if(finished){
intentAndFinish();
}
}
}
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:src="#drawable/splash_es"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
You are killing your Splash before it be on the Screen. Because you are killing it inside the onResume method. Look this piece of documentation:
The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.
You can use a handler, it is more elegant than Thread to solve this problem, but the main ideia is the same of #sheetal.
On onCreate method:
handler = new Handler();
On onResume method:
handler.postDelayed(new Runnable() {
public void run() {
if(!isFileUpdated()){
new PlacesService(this).execute();
}else{
intentAndFinish();
}
}
}, 1000);
If your file is updated You are closing your activity in onResume and switching to main activity and the process will take fraction of milliseconds to do that..so it is very much possible you will never see your spash screen..If you want you can use
private void intentAndFinish() {
Thread.sleep(10000)
finish();
Intent mainIntent = new Intent(this, MainActivity.class);
startActivity(mainIntent);
}
Just to catch up with your splashScreen.
See #bruno 's post...We should use a handler
You call to isFileUpdated() takes time put it in background
You asyntask should be something like this
public class PlacesService extends Asynctask<Void,Void,Void>{
boolean flag=false
public Void doInBackground(){
flag=isFileUpdated()
if (!flag){
return
}else {
// Do your file update
}
return
}
public onPostExcecute(){
intentAndFinish();
}
}

Frame by frame animation in splash screen - Android 2.1

I am writing a splash screen in android 2.1. I want 20 png images to be shown in an order at the splash screen. I tried to use animation-list but couldn't manage to do it.
Here is my code. Now, there is an image called firstLoadingImage.png. Only that is shown for 5000ms. Then the myappactivity starts. However, I couldn't manage to update the image source during this waiting time. How do you think I can do this?
SplashScreenActivity
package myapp.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class SplashScreenActivity extends Activity {
protected boolean _active = true;
protected int _splashTime = 5000; // time to display the splash screen in ms
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
Thread splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while(_active && (waited < _splashTime)) {
sleep(100);
if(_active) {
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
finish();
startActivity(new Intent(SplashScreenActivity.this, MyAppActivity.class));
stop();
}
}
};
splashTread.start();
}
}
splashscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/linearLayout"
android:background="#drawable/loading_screen_background" >
<ImageView
android:id="#+id/firstLoadingImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/loading100" />
</LinearLayout>
Frame animations can be achieved by setting it to an AnimationDrawable to the background of an ImageView.
Example:
final AnimationDrawable anim = new AnimationDrawable();
anim.addFrame(Context.getResources().getDrawable(R.drawable.resource_id_of_frame1, durationInMs);
anim.addFrame(Context.getResources().getDrawable(R.drawable.resource_id_of_frame2, durationInMs);
anim.addFrame(Context.getResources().getDrawable(R.drawable.resource_id_of_frame3, durationInMs);
...
anim.addFrame(Context.getResources().getDrawable(R.drawable.resource_id_of_framen, durationInMs);
anim.setOneShot(false);
ImageView myImageView = getImageViewToSet();
myImageView.setBackgroundDrawable(anim);
myImageView.post(new Runnable(){
public void run(){
anim.start();
}
}
So make your splash screen a simple layout with an ImageView or create one and add it to the layout. Set the AnimationDrawable to it and run.
My guess would be to use the function runOnUiThread() after calling sleep in your splash thread. Make a runnable class to change the ImageView background, and run in this function perhaps?
Activity.runOnUiThread()
You could find the tutorial for it
here

Update GUI when a boolean in a class changes

I'm trying to have my Activity update an ImageView whenever a boolean value changes in another class.
Therefore i have some sort of timer that starts on my first onCreate()
private void startTimer() {
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 2000); // first run after 2 secs
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
connectionControl.checkNetworkState();
System.out.println("online?: " + connectionControl.isOnline());
mHandler.postDelayed(this, 15000); // repeat every x/1000 secs
}
};
My class ConnectionControl has a boolean, that gets set to either true or false whether my app can reach a specific http-host.
Now my question is: how can i achieve a automatic change on the ImageView in my Activity to display the boolean's value?
I already looked at Observer-Pattern, EventListener, BroadcastReciever but I'm stuck at finding the right solution.
Also, i want to use the listening in other Activities.
What has worked so far was starting/stopping the timer-thing for each activity and have the ImageView update inside the run() method. But my guess is, there has to be a way around the redundancy.
I think you are in search of AsyncTask , by using it you can made a call to webservice asynchronously and after the successful call to the webservice, we can process the fetched data.
Just go through this article: http://developer.android.com/resources/articles/painless-threading.html.
I think you needs this: (from android.com)
public void onClick(View v) {
new DownloadImageTask().execute("http://example.com/image.png");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
protected void onPostExecute(Bitmap result) {
mImageView.setImageBitmap(result);
}
}
For more info about AsyncTask , here is link: http://developer.android.com/reference/android/os/AsyncTask.html
Here is how I did it.
I made a layout file for the custom titlebar thing, that has an ImageView.
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView android:id="#+id/title_statusimg"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="#drawable/connected" android:layout_centerVertical="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
</merge>
I include this in every activities layout file i want to use it.
<include layout="#layout/titlebar" />
Also, i made a StatusActivity that extends Activity. It receives a Broadcast that is send from whenever an background thread detects a change in the connectivity to the webserver.
public class StatusActivity extends Activity {
private String CONNECTION_STATUS = "app.CONNECTION_STATUS";
private ImageView conn;
#Override
protected void onResume() {
super.onResume();
IntentFilter iFilter = new IntentFilter(CONNECTION_STATUS);
registerReceiver(mBroadcastReceiver, iFilter);
conn = (ImageView) findViewById(R.id.title_statusimg);
if (ConnectionControl.isOnline()) {
conn.setImageResource(R.drawable.connected);
} else {
conn.setImageResource(R.drawable.nconnected);
}
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(mBroadcastReceiver);
}
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (ConnectionControl.isOnline()) {
conn.setImageResource(R.drawable.connected);
} else {
conn.setImageResource(R.drawable.nconnected);
}
}
};
}
Every Activity that shall use this Status-Image extends StatusActivity instead of Activity.
Hope i covered everything, atleast for me it's working.

Categories

Resources