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();
}
}
Related
I was having a play with Android Studio to create a birthday card and have come to a slight problem with the audio file playing before MainActivity is even on screen. The audio file plays while the splash screen is still on the screen.
I have two Java files:
MainActivity.class
package com.example.android.happybirthday;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
MediaPlayer mySoundfile;
#Override
protected void onPause() {
super.onPause();
mySoundfile.release();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySoundfile = MediaPlayer.create(this, R.raw.music);
mySoundfile.setLooping(true);
mySoundfile.setVolume(100, 100);
mySoundfile.seekTo(0);
mySoundfile.start();
}
}
and Splash.class
package com.example.android.happybirthday;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Thread mythread = new Thread() {
#Override
public void run() {
try {
sleep(3000); // 3 second delay for cold start
Intent startMainApp = new Intent(getApplicationContext(), MainActivity.class); // initiate MainActivity
startActivity(startMainApp); // open MainActivity
finish(); // close this activity
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
mythread.start();
}
}
This is the AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.happybirthday">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Splash"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Could you kindly give some support?
Thanks :)
Thanks #Sergey and #You Kim, I managed to get it done via adding a delay in onStart within the MainActivity.class
you need to run the media player when the activity is started, not on the create.
try running media player on onStart() or onResume().
Called when activity resume is complete
protected void onPostResume()
When someone installs my app from the app store, the Open button (usually found next to the uninstall button upon installation completion) is grayed out, and the application cannot be found in their app drawer.
Also, when I execute from Eclipse choosing Default Activity, it says in the console that it installed the APK, but it does not start. If I select the Splash activity as the default activity in the run configuration, it runs without a hitch, but I still cannot find it in my app drawer afterwards. Help would be appreciated c:
Manifest:
<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.meteorfiber.gangnamstyle.Splash"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.meteorfiber.gangnamstyle.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.meteorfiber.gangnamstyle.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.meteorfiber.gangnamstyle.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Splash:
package com.meteorfiber.gangnamstyle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGTH = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
#Override
protected void onResume() {
super.onResume();
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
boolean isSplashEnabled = sp.getBoolean("isSplashEnabled", true);
if (isSplashEnabled) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Splash.this.finish();
Intent mainIntent = new Intent(Splash.this,
MainActivity.class);
Splash.this.startActivity(mainIntent);
}
}, SPLASH_DISPLAY_LENGTH);
} else {
finish();
Intent mainIntent = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(mainIntent);
}
}
}
MainActivity:
package com.meteorfiber.gangnamstyle;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
MediaPlayer ourSong;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameView(this));
ourSong = MediaPlayer.create(MainActivity.this, R.raw.song);
ourSong.start();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// preventing default implementation previous to
// android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
}
}
Change your intent filter for your starting activity to
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
The android launcher along with the settings activity try to find this intent-filter in your application in order to launch the activity. If it can't be found, your activity cannot be launched with a typical launcher and won't be displayed.
I have created this activity with two buttons
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
public class SelectOption extends Activity{
Button bu1,bu2,bu3;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.option_main);
bu1 = (Button)findViewById(R.id.button1);
bu2 = (Button) findViewById(R.id.button2);
bu1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myintent1 = new Intent(SelectOption.this, CheckBalance.class);
startActivity(myintent1);
}
});
bu2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myintent2 = new Intent(SelectOption.this, CheckAvailability.class);
startActivity(myintent2);
}
});
and there are 2 activities called CheckBalance.java and CheckAvailability.java also. but when I Run the program nothing happen on it. Does anyone have an idea what wrong with this??
Use this code in on click listener
Intent intent = new Intent();
intent.setClass(getApplicationContext(), CheckBalance.class);
startActivity(intent);
and also check weather you activities declare in manifest file and in Xml also....
This is My Manifest file (On behalf of Original question Asker(Anuradha))
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.people.oshada"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TheMainActivity"
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=".SelectOption"></activity>
<activity android:name=".CheckBalance"></activity>
<activity android:name=".CheckAvailability"></activity>
</application>
In ANDROID MAINFEST FILE u have to add
<activity android:name=". CheckBalance"/>
<activity android:name=". CheckAvailability"/>
I want to link my two activites by clicking on a button i have written the following code
public class IHBCAPTUREActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
ImageView iv;
TextView tx;
Button b1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv=(ImageView)findViewById(R.id.imageView1);
tx=(TextView)findViewById(R.id.textView1);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent calIntent;
**calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);**
startActivity(calIntent);
}
}
error cumes in this line
calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);
for LoginActivity.class that
LoginActivity cannot be resolved to a type . How to solve it?
here is the simple code how you can load one activity to another,
i have created two activities like this
FirstActivity.java
package com.rdc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button btnload = (Button) findViewById(R.id.btn);
btnload.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}
SecondActivity.java
package com.rdc;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
and my manifest file is
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
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=".FirstActivity"
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=".SecondActivity"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
try to implement this in your app and let me know if still any doubt is there.
Check whether you have declared LoginActivity in manifest file as:
<activity
android:name="your.package-name.LoginActivity" >
</activity>
You don't have anything wrong in code.
Its know issue that occurs randomly.
solution is to set the target to other API level in project->properties->android, then set it back.then clean you project once.
I think this will refresh the .classpath or some other files, not sure, but it works.
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);