Android layout not loaded on onStart() - android

While showing a background I want to run some operations (without using threads).
In my activity on onCreate() I have setContentView(R.layout.splash);
When the app fires onStart(), the layout is not loaded yet. Why?
UPDATE: LogCat gives me "Activity has leaked window"
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:src="#drawable/splash"
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" />
</LinearLayout>
onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
onStart
#Override
protected void onStart() {
super.onStart();
if (CBConstants.ASSETS_MANAGER){
String progressMsg = this.getResources().getString(R.string.progress_dialog_assets_upgrading_message);
ProgressDialog progressDialog = ProgressDialog.show(this, null, progressMsg);
progressDialog.show();
try {
[...]
} catch(Exception e) {
Log.d("appcheck", ""+e);
Intent intent = new Intent(SplashScreen.this, CBWebViewActivity.class);
startActivity(intent);
}
}
else {
Thread noAssetManager = new Thread() {
public void run() {
try {
while (splashActive && ms < splashTime) {
if(!paused)
ms=ms+100;
sleep(100);
}
} catch(Exception e) {
if (LOG) Log.d("appcheck", ""+e);
}
finally {
Intent intent = new Intent(SplashScreen.this, CBWebViewActivity.class);
startActivity(intent);
}
}
};
noAssetManager.start();
}
}

Related

making activity_main.xml as splash screen

i m new in android.
what if I want to make current view which I have made yet, of my app as splash screen for 5 seconds.
is it possible or not ?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#mipmap/background">
<ImageView
android:id="#+id/icon"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:contentDescription="TODO"
android:src="#mipmap/app_icon_app_store"/>
<TextView
android:id="#+id/firstLine"
android:layout_width="300dp"
android:layout_height="60dp"
android:text="EXPRESSIONS"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
android:textSize="50sp"
android:layout_below="#+id/icon"
android:gravity="center" />
</RelativeLayout>
Create a splash activity and you can set above layout using setContentView(R.layout.activity_main);
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// this is count down timer for 5 seconds
// 5000 milliseconds value is for 5 seconds display, 1000 milliseconds value is clock tick interval
new CountDownTimer(5000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
// Launch whatever activity screen you want to display when done with count down timer (i.e. 5 seconds in your case)
Intent intent = new Intent(SplashActivity.this,
YouNextActivity.class);//
startActivity(intent);
finish();
}
}.start();
}
}
you can do like this...
public class SplashScreenActivity extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/****** Create Thread that will sleep for 5 seconds *************/
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 5 seconds
sleep(SPLASH_TIME_OUT);
Intent i = new Intent(MainActivity.this, AnotherActivity.class);
startActivity(i);
//Remove activity
finish();
}
} catch (Exception e) {
}
}
};
// start thread
background.start();
}
}
Hope this will help you .
public class SplashActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_LENGTH = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
You need to make this activity to sleep for some seconds
and recall start activity function after the time:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread splashTimer = new Thread() {
public void run() {
try {
sleep(3000);
} catch (Exception e) {
Log.e("error", e.toString());
} finally {
startMain();
}
}
};
splashTimer.start();
}
public void startMain(){
Intent iMain = new Intent(this, MainActivity.class);
startActivity(iMain);
finish();
}
after startActivity() you need to call finish() to prevent this splash activity if back button pressed.

How to add a spinner to a splashscreen in android

I have implemented a Splash Screen on an android app I am building using Android Studio and I would like to add a spinner to the splash screen. Can anyone give me a hand on this:
Here is the code on my splashScreen.java Class:
package com.packagename.appname;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
/**
* Created by VAIO1 on 05/09/2014.
*/
public class splashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
Thread logoTimer = new Thread() {
public void run() {
try {
sleep(3000);
Intent splashIntent = new Intent("com.packagename.appname.SPLASH");
startActivity(splashIntent);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
super.onPause();
}
}
Here is the code for my 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"
android:orientation="vertical"
android:id="#+id/splashId">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="#drawable/screen"
android:cropToPadding="false"
android:scaleType="fitXY"/>
</LinearLayout>
Thank you for any feedback on how to achieve this.
use this code :-
public class splashScreen extends Activity {
ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
Thread logoTimer = new Thread() {
public void run() {
try {
sleep(3000);
progressDialog = new ProgressDialog(Sync_Screen_activity.this);
progressDialog.setMessage("Wait..");
progressDialog.setCancelable(false);
progressDialog.show();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent splashIntent = new Intent("com.packagename.appname.SPLASH");
startActivity(splashIntent);
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
super.onPause();
}
}

show an image for some seconds before of the setContentView of the main activity

I want to show a "boot logo" or image before of setting the final layout of the main activity. Actually i do this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.authors);
mContext = this;
showAuthors();
where showAuthors run this:
private void showAuthors()
{
setContentView(R.layout.authors);
Thread logoTimer = new Thread() {
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
};
logoTimer.start();
try {
logoTimer.join();
setContentView(R.layout.activity_main);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
the file authors.xml ( R.layout.authors ) is the same of activity_main, but clean, containing just a pair of strings with my name and email:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#raw/call2"
android:src="#raw/call2"
android:scaleType = "centerCrop"
tools:context=".MainActivity" >
<TextView
android:id="#+id/authorsTV"
android:textColor="#FF6600"
android:textSize="16.5sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="33dp"
android:text="#string/serviceStatus" />
</RelativeLayout>
the problem is: all works, but the screen is all white as an empty layout.
where i'm doing wrong? thank you all if consider to reply me!
You should not call thread.join from the main thread, because you can get an ANR crash.
Here's how to do it with minimal changes to your existing code. Remove everything after logoTimer.start(), and put this inside the try block, immediately after sleep(3000):
runOnUiThread(new Runnable(){
public void run(){
setContentView(R.layout.activity_main);
}
});
But it would be cleaner to rewrite that whole method like this:
private void showAuthors()
{
setContentView(R.layout.authors);
new Handler().postDelayed( new Runnable(){
public void run(){
setContentView(R.layout.activity_main);
}
}, 3000);
}
First you have two ways of showing a splash screen,
1.with activity
public class SplashActivity extends Activity implements Runnable
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
setContentView(R.layout.act_spalsh);
hideSplashActivity();
}
private void hideSplashActivity()
{
new Handler().postDelayed(this, 3000);
}
#Override //from runnable
public void run()
{
startActivity(new Intent(this, MainActivity .class));
finish();
}
2.with dialog
public class MainActivity extends Activity implements Runnable
{
Dialog splashDialog;
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
splashDialog = new Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
splashDialog.setContentView(R.layout.dlg_splash);
splashDialog.show();
hideSplashDialog();
setContentView(R.layout.act_main);
}
private void hideSplashDialog()
{
new Handler().postDelayed(this, 3000);
}
#Override //from runnable
public void run()
{
splashDialog.dismiss();
splashDialog = null;
}
I prefer using dialog unless you have some trouble with that

Android ProgressBar with threads

I am working on ProgressBar class in android, but I can't make it progress through 5 seconds and load the application. Everything works but the progress bar not progressing. Here is the code.
public class StartPoint extends Activity{
ProgressBar progressBar;
private int progressBarStatus = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
progressBar = (ProgressBar)findViewById(R.id.progressBar1);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
while(progressBarStatus < 5000){
progressBar.setProgress(progressBarStatus);
progressBarStatus += 1000;
}
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openMainList = new Intent(StartPoint.this, in.isuru.caf.MainList.class);
startActivity(openMainList);
}
}
};
timer.start();
}
protected void onPause(){
super.onPause();
finish();
}
}
And here is the layout file splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mary_mother_of_god" />
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.67" />
</LinearLayout>
You can't update a UI Widget from a different thread. You need to do something like:
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
while(progressBarStatus < 5000){
StartPoint.this.runOnUIThread(new Runnable(){
public void run()
{
progressBar.setProgress(progressBarStatus);
progressBarStatus += 1000;
}
});
}
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openMainList = new Intent(StartPoint.this, in.isuru.caf.MainList.class);
startActivity(openMainList);
}
}
};
timer.start();

Need help in debugging AsyncTask with layout view

I am using an Asynctask in the splash screen to add database items into database.
The insert process went well but the splash view just won't show up.
I am posting my code below, any advice would be appreciated. thx
splash.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splash">
</RelativeLayout>
Splash.java
public class Splash extends Activity {
private ChannelDB mDB;
private TextView loading;
private String channelS_TABLE;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new SetDB().execute();
}
private class SetDB extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
if (tabIsExist(channelS_TABLE) !=true){
**some database insert**
}else{
synchronized(this){
Log.i("Splash", "the table is there");
wait(3000);}
Intent i = new Intent();
i.setClassName("com.appkon.hdtvs",
"com.appkon.hdtvs.HDtvs");
finish();
startActivity(i);
}
}catch (Exception e) {
Log.i("Splash", "setDB exception");
Intent i = new Intent();
i.setClassName("com.appkon.hdtvs",
"com.appkon.hdtvs.HDtvs");
finish();
startActivity(i);
}
return null;
}
protected void onPreExecute(String load) {
synchronized (this) { try {
wait(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
protected void onPostExecute(String finish) {
Intent i = new Intent();
i.setClassName("com.appkon.hdtvs",
"com.appkon.hdtvs.HDtvs");
finish();
startActivity(i);
}
}
public boolean tabIsExist(String tableName){
boolean result = false;
if(tableName == null){
return false;
}
Cursor cursor= ChannelDB.check();
startManagingCursor(cursor);
try {
if(cursor.moveToNext()){
int count = cursor.getInt(0);
if(count>0){
result = true;
}
}
} catch (Exception e) {
Log.e(this.toString(),"error:"+e.toString());
Intent intent = new Intent(this,HDtvs.class);
startActivity(intent);
this.finish();
}
return result;
}
}
I changed the xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splash">
<ImageView android:id="#+id/bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/splash"/>
</LinearLayout>
still not working and the database insert is not workong too.
RelativeLayout is a container. Its background will only show when you have some elements / views inside it. I am not sure about it though. But you can try this - remove the image from the background of the RelativeLayout, and create an ImageView inside the RelativeLayout and set the ImageView's source image as your desired image. You should get the desired result.

Categories

Resources