package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity {
private Intent myintent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
myintent = new Intent(this, MainActivity.class);
splashScreen(1000); }
public void splashScreen (final int x)
{
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(x);
} catch (InterruptedException e) {
e.printStackTrace();
}
startActivity(myintent);
finish();
}
}).run();
}
}
There's the code, and here is the problem : the SplashScreen do not get the content view of the splash XML layout file... Now, I have my suspicions that it is a thread problem and that somehow the thread is executed before the setContentView method although that method is located before the run method of the Thread in code, so it's illogical that I'm thinking this way but I'm like running out of reasons for this Splash Screen not to work
Change thread.run() to thread.start(): http://www.javafaq.nu/java-article1131.html
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(x);
} catch (InterruptedException e) {
e.printStackTrace();
}
startActivity(myintent);
finish();
}
}).start();
A better way to implement Splash:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
myintent = new Intent(this, MainActivity.class);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
startActivity(myintent);
finish();
}
}, 1000);
}
package com.echo.myatlsnookpaid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(3500);
// sleep(100);
}
catch(InterruptedException e){
e.printStackTrace();
} finally {
Intent openMain = new Intent(Splash.this, MainActivity.class);
startActivity(openMain);
finish();
}
}
};
timer.start();
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
and in your manifest, give the
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
filter to he splashactivity.
Related
I make a splashscreen, but my splashscreen don't show up. After 4 seconds the second splashscreen will show up.
I want show up my splashscreen for 4 seconds.
This is my code:
package com.geven.headsoccer.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
startActivity(new Intent("com.geven.headsoccer.LIBGDX_GAME"));
}
}
If you want to show your SplashScreen for 4 seconds, why don't you use Handler?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
finish();
}
}, 4000);
}
You have to use a handler.
new Handler().postDelayed(new Runnable() {
// Using handler with postDelayed called runnable run method
#Override
public void run() {
startActivity(new Intent("com.geven.headsoccer.LIBGDX_GAME"));
// close this activity
finish();
}
}, 4*1000); // wait for 5 seconds
How can I start an activity in another class. It's in eclipse. It is just for start Main activity after a while.
public class FirstShow extends ActionBarActivity {
#SuppressLint("ShowToast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_show);
Timer tm;
tm = new Timer();
ttask task = new ttask();
tm.schedule(task, 30000);
}
}
class ttask extends TimerTask {
#Override
public void run() {
// TODO Auto-generated method stub
FirstShow.class.startActivity());
}
}
I want to start another activity after a couple of seconds. How can I do that?
package com.example.teststart;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent i=new Intent(FirstActivity.this,secondactivity.class);
startActivity(i);
}
}, 3000);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
}
package com.example.teststart;
import android.app.Activity;
import android.os.Bundle;
public class secondactivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactvity);
}
}
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.teststart.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="com.example.teststart.secondactivity"
android:label="#string/app_name" />
</application>
Friend if you want normally want to start activity after few second than use my code:----->
Thread thread = new Thread() {
public void run() {
try {
sleep(3*1000);
Intent i=new Intent(getBaseContext(),ManuList.class);
startActivity(i);
finish();
} catch (Exception e) {
}
}
};
thread.start();
}
public class FirstShow extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_show);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent i=new Intent(FirstShow.this,secondactivity.class);
startActivity(i);
}
}, 3000);
}
}
I am new to android. I have two activities Splash and MainActivity. When I launch my application the Splash activity starts(as its supposed to be), it plays the sound but the background image does not appear and some seconds later my MainActivity starts. Thanks in advance!
code for MainActivity class
Package com.example.button;
import android.app.Activity;
import android.os.Bundle;
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;
Button sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add=(Button)findViewById(R.id.button1);
sub=(Button)findViewById(R.id.button2);
display=(TextView)findViewById(R.id.textView1);
}
#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;
}
public void add(View view)
{
counter=counter+1;
display.setText("your total is"+counter);
}
public void sub(View view)
{
counter--;
display.setText("your total is"+counter);
}
}
code for Splash class
package com.example.button;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splash extends Activity{
MediaPlayer ourSong;
Thread timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ourSong=MediaPlayer.create(Splash.this,R.raw.addicted);
ourSong.start();
timer=new Thread();
timer.start();
run();
//{
//};
}
public void run()
{
try {
timer.sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}finally {
Intent openStartingPoint=new Intent("com.example.button.MAINACTIVITY");
startActivity(openStartingPoint);
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
}
}
code for 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/feather">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
I think you have not correctly used Thread. change these three lines of your code
timer=new Thread();
timer.start();
run();
to
timer=new Thread(new Runnable(){
public void run() {
try {
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}finally {
Intent openStartingPoint=new Intent("com.example.button.MAINACTIVITY");
startActivity(openStartingPoint);
}
}
});
timer.start();
and remove run method from your activity.
by this way you let the splash activity to appear for 5 seconds on screen.
in your splash.xml take a imageview and put your image file there.
e.g android:src="file.gif"
hope this helps you`
use this code as your splash activity:
public class SplashActivity extends Activity {
int SPLASH_DISPLAY_LENGHT = 1000;
#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(R.layout.splash_form);
InitilizeUi();
}
private void InitilizeUi() {
// play your sound
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this, AboutActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
I believe I have my code set up correctly but when I try to debug it, after it transitions from the splash screen it just goes right to a black screen. I know I imported the layout correctly but it still goes black.
This is the code for the splash screen
package com.example.equate.jones;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class EJ_Splash extends Activity {
protected boolean _active = true;
protected int _splashTime = 3000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ej__splash);
// thread for displaying the SplashScreen
Thread splashTread = new Thread() {
#Override
public void run() {
try {
synchronized(this){
wait(4000);
}
}
catch(InterruptedException e) {
// do nothing
} {
finish();
Intent i = new Intent(getApplicationContext(),EJ_Board.class);
startActivity(i);
}
}
};
splashTread.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_ej__splash, menu);
return true;
}
}
This is the code for the screen it is supposed to transition to.
package com.example.equate.jones;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class EJ_Board extends Activity {
private ImageView button1;
final MediaPlayer mp = MediaPlayer.create(this, R.raw.warm);
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ej_board);
button1=(ImageView)findViewById(R.id.imageView1);
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
mp.start();
}
});
}
}
This is the xml for EJ_Board
<?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/ic_launcher" />
</LinearLayout>
I think your problem is with the ImageView. You need to add an image to your drawable folder, then change your android:src="#drawable/ic_launcher" to the name of the image you saved. This will give you the image you need for your button. Hope that helps
Edit:
For your splash screen, try something like this:
public class SplashActivity extends Activity {
private long splashDelay = 5000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
TimerTask task = new TimerTask()
{
#Override
public void run() {
finish();
Intent homeIntent = new Intent().setClass(SplashActivity.this, HomeActivity.class);
startActivity(homeIntent);
}
};
Timer timer = new Timer();
timer.schedule(task, splashDelay);
}
}
Then in your home activity you can set your menu:
public class HomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.locationButton:
Intent locationIntent = new Intent(this, LocationActivity.class);
startActivity(locationIntent);
return true;
case R.id.diningButton:
Intent diningIntent = new Intent(this, DiningActivity.class);
startActivity(diningIntent);
return true;
case R.id.topXXVButton:
Intent topIntent = new Intent(this, DiningActivity.class);
startActivity(topIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Try this:
public class SplashActivity extends Activity {
private long splashDelay = 5000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
TimerTask task = new TimerTask()
{
#Override
public void run() {
finish();
Intent mainIntent = new Intent().setClass(EJ_Splash.this, EJ_Board.class);
startActivity(mainIntent);
}
};
Timer timer = new Timer();
timer.schedule(task, splashDelay);
}
}
I have the following three classes:
When I try to show a progressDialog when the WorkingThread is running, the ProgressDialog only shows up after the WorkingThread is done. What am I doing wrong?
I am not interested in using an AsyncTask!
-StartActivity:
public class StartActivity extends Activity implements OnClickListener
{
public ProgressDialog pgd;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgv = (ImageView)findViewById(R.id.imageView1);
tv = (TextView)findViewById(R.id.textview);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
}
public void onClick(View v)
{
pgd = ProgressDialog.show(StartActivity.this, "", "Loading picture"); // Start ProgressDialog before starting activity
Intent ActivityIntent = new Intent(this, FirstActivity.class);
startActivityForResult(ActivityIntent, 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK)
{
pgd.dismiss(); //Stop ProgressDialog when FirstActivity is "done"
}
}
}
-
-FirstActivity:
public class FirstActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
WorkingThread wt = new WorkingThread();
wt.start();
try
{
wt.join();
Intent ActivityIntent = getIntent();
setResult(RESULT_OK, ActivityIntent);
finish();
}
catch (Exception e)
{
}
}
}
-WorkingThread:
public class WorkingThread extends Thread
{
#Override
public void run()
{
super.run();
try
{
Thread.sleep(5000);
}
catch (Exception e)
{
}
}
}
The problem is ProgressDialog always need current Activity context for display.But in your case ProgressDialog is little unfortunate
The reason is as soon as you fire ProgressDialog the next couple of lines take out Context from Current activity and starts Next Activity i.e FirstActivity.So your progressDialog gets no chance to present itself on the Screen.
Use an AsyncTask. Here's an example:
package com.example.test;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
ProgressDialog activityProgressDialog;
private Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new TestAsyncTask().execute();
}
});
}
private class TestAsyncTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
//Called before doInBackground.
//Initialize your progressDialog here.
activityProgressDialog = ProgressDialog.show(context,
"Test",
"Doing heavy work on background...", false,
false);
}
#Override
protected Void doInBackground(Void... v) {
//Do your work here
//Important!!! Update any UI element on preExcecute and
//onPostExcecute not in this method or else you will get an
//exception.
//The below code just make the thread inactive for 5 seconds.
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().notify();
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void v) {
//Dismiss the progessDialog here.
activityProgressDialog.dismiss();
}
}
}
I just show your edit that you don't want to use an AsyncTask. I will not delete this answer (unless you want me to) since it is another way of doing what you wanted to do.
try
{
wt.join();
Intent ActivityIntent = getIntent();
setResult(RESULT_OK, ActivityIntent);
finish();
}
here is your problem. The UI thread is waiting the Working Thread to finish becaouse of the join().