I'm trying to create a splash screen for my app in android but it will not show up at all.
The code i'm using is 4 different files. Here it is:
Splash.java
package com.timchecklist;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread Timer = new Thread() {
public void run() {
try {
sleep(3000);
startActivity(new Intent("com.timchecklist.SPLASHNEW"));
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
finish();
}
}
};
Timer.start();
}
}
SplashNew.java
package com.timchecklist;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class SplashNew extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
}
}
Splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/pic1"
android:gravity="center"
android:orientation="vertical" >
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.timchecklist"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TimCheckListActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
</activity>
<activity
android:name=".SplashNew"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Please tell me what I'm doing wrong here. Any help would be appreciated. Thanks guys:)
i think Splash should be launcher activity first . and where you declare SPLASHNEW in AndroidManifest File ?.
See Splash Screen Example
You should add SplashNew in Manifest file.
Try below code
setContentView(R.layout.splash);
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(2000);
startActivity(new Intent(getApplicationContext(),EquipmentCategory.class));
finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
There is no entry of SplashNew in menifest file ......
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.timchecklist"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TimCheckListActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
There is better way to make a splash screen.
Declare an handler like this, which call the next activity to launch after a precised time:
private Handler splashHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
Intent intent = new Intent(Splash.this, OtherActivity.class);
startActivity(intent);
finish();
break;
}
super.handleMessage(msg);
}
};
Then execute the code after some delay like this in onCreate:
Message msg = new Message();
splashHandler.sendMessageDelayed(msg, SPLASHTIME);
Related
I want to show my splash.xml for 6 seconds and then activity_main.xml but it is not showing the activity_main.xml...
my splash.java code:
package com.example.ne;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class splash extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread()
{
public void run(){
try{
sleep(6000);
} catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent open =new Intent("com.example.ne.MAINACTIVITY");
startActivity(open);
}
}
};
timer.start();
}
}
and mainactivity.java code:
package com.example.ne;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add,sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add=(Button) findViewById(R.id.bAdd);
sub=(Button)findViewById(R.id.bSub);
display=(TextView)findViewById(R.id.tvdisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
counter++;
// TODO Auto-generated method stub
display.setText("Your total is" + counter);
}
});sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
counter--;
display.setText("Your total is" + counter);
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and manifest.xml code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ne"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.ne.splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity" android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.ne.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Not that you can't do it your way, but managing threads is trickier business than it may seem. That is why Android incorporated a number of helpers for it. In your case, you could use a Handler to post a delayed runnable that will act as your redirect.
new Handler().postDelayed(new Runnable(){
#Override
public void run(){
Intent open = new Intent(splash.this, MainActivity.class);
startActivity(open);
splash.this.finish();
}
}, 6000);
This will keep you from having to manage any threads and will close the splash activity after the intent is passed.
Second, if you will notice, the Intent instantiation, new Intent(splash.this, MainActivity.class), this is the way you should create Activity intents. This will provide context for the explicitly called intent.
Third, the class splash should be Splash, per Java naming conventions. Camel case is appropriate, but classes have an uppercase first letter, while methods and variables have a lowercase first letter.
And last, your manifest should look like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ne"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" />
</application>
</manifest>
EDIT
As recommended by #JHH it is also possible to stop the splash page from redirecting on things like back pressed or the home screen. What you can do for that is:
First, make the class variables mHandler and mRunnable and when you instantiate your objects, let the variables reference them.
private Handler mHandler;
private Runnable mRunnable;
#Override
public void onCreate(Bundle savedInstanceState){
...
mHandler = new Handler();
mRunnable = new Runnable(){...};
...
}
Next, let the handler post the runnable message with a delay in onResume. This will allow us to create the post message every time the splash activity comes to the front (remember that after the redirect the splash screen will finish so this won't be an issue).
#Override
public void onResume(){
super.onResume();
postDelayed(mRunnable, 6000);
}
Lastly, in onPause we can then stop the delayed post like so. This will keep the handler from triggering the intent which will make it so the redirect doesn't happen if the splash screen is in the background. In other words, if you hit the splash screen then hit the back/home button the runnable won't continue and open the activity anyway.
#Override
public void onPause(){
mHandler.removeCallbacks(mRunnable);
super.onPause();
}
Try instead
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
Make following changes in your code
Package com.example.ne;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class splash extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread()
{
public void run(){
try{
sleep(6000);
} catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent open =new Intent(splash.this,MainActivity.class);
startActivity(open);
}
}
};
timer.start();
}
}
My Application name is timepass
The below file is MainActivity.java
package com.example.timepass;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
int counter;
Button add, sub;
TextView Display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add= (Button) findViewById(R.id.badd);
sub=(Button) findViewById(R.id.bsub);
Display=(TextView) findViewById(R.id.tvdisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
Display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
Display.setText("Your total is " + counter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
The below file is activity_main.xml which is realted to the above file MainActivity.java
<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"
tools:context=".MainActivity"
android:background="#drawable/pic_two">"
<Button
android:id="#+id/badd"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvdisplay"
android:layout_alignTop="#+id/bsub"
android:text="#string/but1"
android:textSize="20sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvdisplay"
android:layout_alignRight="#+id/tvdisplay"
android:text="#string/hello_world"
android:textSize="#dimen/asa" />
<TextView
android:id="#+id/tvdisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:gravity="center"
android:text="#string/str1"
android:textColor="#layout/activity_main"
android:textSize="30sp" />
<Button
android:id="#+id/bsub"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tvdisplay"
android:layout_below="#+id/tvdisplay"
android:layout_marginRight="38dp"
android:layout_marginTop="23dp"
android:text="#string/but2"
android:textSize="20sp" />
</RelativeLayout>
Then I have created another activity which is below which is Splash.java
package com.example.timepass;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity
{
#Override
protected void onCreate(Bundle tpsavedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(tpsavedInstanceState);
setContentView(R.layout.splash);
Thread timer =new Thread()
{
public void run()
{
try
{
sleep(5000);
}
catch(InterruptedException e)
{ {
e.printStackTrace();
}
finally
{
Intent openMainActivity =new Intent("com.example.timepass.MAINACTIVITY");
startActivity(openMainActivity);
}
}
};
timer.start();
}
}
Then I have created Xml file related to Splash.java which is 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"
android:background="#drawable/pic_background">
</LinearLayout>
NOW I WANT TO RUN FIRST Splash.java file then MainActivity.java file so I have make some changes in Androidmanifest.xml file
here are the changes, and after running my program I get error "Unfortunately timepass has stopped" where timepass is my app name . please solve this issue
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timepass"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.timepass.Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.timepass.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.timepass.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
//just do it like this
Intent openMainActivity =new Intent(this,MainActivity.class);
startActivity(openMainActivity);
instead of
Intent openMainActivity =new Intent("com.example.timepass.MAINACTIVITY");
startActivity(openMainActivity);
MainActivity in your manifest is used in two places. In one place it is in fully capital letter. Change that and give a try
You have an extra { before e.printStackTrace(); in your Splash.java. Remove that and you'll be good to go.
Also post log for furthur errors in the future
I have edited your AndroidManifest.xml update your code after your application work properly.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timepass"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.timepass.Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.timepass.MainActivity"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
change your code in splash.java
Intent i = new Intent(splash.this, MainActivity.class);
startActivity(i);
My new android app can download from the playstore but doesn't appear in my downloaded apps. When i download it from the play store usually there is an option to 'open' the app aswell as 'uninstall', only the uninstall button is visible. (http://i.imgur.com/jNTvJq2.png notice the missing open button)
here's the my manifest, there were no errors during all the tests i ran.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jackattackapps.bigl"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/iconimage"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock" >
<activity
android:name="com.jackattackapps.bigl.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.jackattackapps.bigl.Splashscreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SPLASHSCREEN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>
And this is the activity that is supposed to load on the start of the application.
package com.jackattackapps.bigl;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splashscreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
Thread timer = new Thread(){
public void run(){
try{
MediaPlayer ourSong = MediaPlayer.create(Splashscreen.this, R.raw.splashsound);
ourSong.start();
sleep(2300);
}
catch (InterruptedException e){
e.printStackTrace();
} finally {
Intent openMainActivity = new Intent("android.intent.action.MAINACTIVITY");
startActivity(openMainActivity);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
if more information is needed just comment, help would be appreciated greatly.
Your Launcher activity should have this Intent Filter
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
Im getting the error in the title when just messing around with android programming:
Code :
package co.uk.mypchealth.whatami;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread logoTimer = new Thread(){
public void run(){
try {
sleep(3000);
Intent menuIntent = new Intent("co.uk.mypchealth.whatami.JAVAMENU");
startActivity(menuIntent);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
and the manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.uk.mypchealth.whatami"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="co.uk.mypchealth.whatami.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".JavaMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="co.uk.mypchealth.whatami.JAVAMENU" />
<category android:name="android.intent.category.DEFUALT" />
</intent-filter>
</activity>
</application>
</manifest>
Im Hoping its something simple i mean jeeze the app does nothing yet lol.... i have the intent declared and and all the names i have checked and double checked ... just cant see the wood from the trees. Any help would be great.
Try,
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent menuIntent = new Intent(MainActivity.this, JavaMenu.class);
startActivity(menuIntent);
}
}, 3000);
try this :
<activity
android:name=".JavaMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="co.uk.mypchealth.whatami.JAVAMENU" />
<category android:name="android.intent.category.DEFUALT" />
</intent-filter>
</activity>
you have defined the app name in both activities try to change this activity declaration to :
<activity android:name="co.uk.mypchealth.whatami.JavaMenu" > </activity>
When I run the splash screen without thread code the splash screen background appears
but when I launch the background image via a thread, the splash screen does not show up.
my splash.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="travis.com"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Splash"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TheNewBostonActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.thenewboston.travis.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
splash.java:
package travis.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle TravisLoveBeacon) {
// TODO Auto-generated method stub
super.onCreate(TravisLoveBeacon);
setContentView(travis.com.R.layout.splash);
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
}
catch(InterruptedException e) {
e.printStackTrace();
}
finally {
Intent openstatingpoint = new Intent("com.thenewboston.travis.STARTINGPOINT");
startActivity(openstatingpoint);
}
}
};
timer.run();
}
}
try to use
Thread splashTimer = new Thread() {
public void run() {
try {
sleep(5000);
// Advance to the next screen.
startActivity(new Intent(SplashActivity.this,
HomeActivity.class));//}
} catch (Exception e) {
Log.e("ex", e.toString());
} finally {
finish();
}
}
};
splashTimer.start();
try below code in your finally block:
finish();
Intent mainIntent = new Intent().setClass(Splash.this, nextscreen.class);
startActivity(mainIntent);