Only select one checkbox of splash preference checkedboxs - android

I have app starting depend on user preferance with three different checkedbox :
1- start app without splash and music .
2- start app with splash only .
3- start app with spalsh and music .
with the below code its work perfectly .
But still two point to be achieved :
FIRST only one checkbox should be checked .
SECOND after you checked any one of checkboxs as you prefer then go back to mainactivity , here you can exit the app either by use back button or exit button which i already have it in my option menu , the problem is that either using back button or exit button it doesn't respond to first click , i have to click twice to exit the app.
but i cant achieve it ,
any help please will be appreciated .
public class Splash extends Activity{
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
if (without_splash_screen == true)
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
boolean splash = getPrefs.getBoolean("splash", true);
if(splash == true) {
setContentView(R.layout.splash);
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
SharedPreferences getPrefs1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean music = getPrefs1.getBoolean("splash_music", true);
if (music == true)
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(2000); }
catch (InterruptedException e){
e.printStackTrace(); }
finally{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent); }}
};
timer.start(); }
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
In xml folder :prefs.xml
<?xml version="1.0" encoding="utf-8" ?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference android:title="without splash screen"
android:defaultValue="true"
android:key="without_splash_screen"
android:summary="Start app without splash"/>
<CheckBoxPreference android:title="splash screen"
android:defaultValue="true"
android:key="splash"
android:summary="Start app with splash only" />
<CheckBoxPreference android:title="splash screen music"
android:defaultValue="true"
android:key="splash_music"
android:summary="Start app with splash and music" />
</PreferenceScreen>
UPDATE:
i tried also the below code but it doesn't change any thing , still all chechboxs can be checked :
public class Splash extends Activity{
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
if (without_splash_screen == true)
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
boolean splash = getPrefs.getBoolean("splash", true);
if(splash == true) {
setContentView(R.layout.splash);
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
SharedPreferences getPrefs1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean music = getPrefs1.getBoolean("splash_music", true);
if (music == true)
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(2000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
public void getPrefs() {
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
boolean splash = getPrefs.getBoolean("splash", false);
boolean music = getPrefs.getBoolean("splash_music", false);
if (without_splash_screen == true){
splash = false;
music = false;
}else if (splash == true){
music = false;
without_splash_screen = false;
}else if (music == true){
without_splash_screen = false;
splash = false;
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
UPDATE:
i tried also another code but it doesn't change any thing , still all chechboxs can be checked :
public class Splash extends Activity{
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
if (without_splash_screen == true)
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
else {
getPrefs.getBoolean("splash", false);
getPrefs.getBoolean("splash_music", false);
}
boolean splash = getPrefs.getBoolean("splash", true);
if(splash == true) {
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(2000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
else
{
getPrefs.getBoolean("without_splash_screen", false);
getPrefs.getBoolean("splash_music", false);
}
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
boolean splash_music = getPrefs.getBoolean("splash_music", true);
if (splash_music == true) {
ourSong.start();
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(2000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
else
{
getPrefs.getBoolean("without_splash_screen", false);
getPrefs.getBoolean("splash", false);
}
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
Any advice , thanks.
UPDATE 3 (WHOLE PROJECT):
1- one checkbox checked achieved perfectly .
2- either using back button or exit button in MainActivity , doesn't respond to first click , i have to click twice or three times to exit the app.
Splash.java
public class Splash extends Activity{
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle DrTsn) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// TODO Auto-generated method stub
super.onCreate(DrTsn);
setContentView(R.layout.splash);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
if (without_splash_screen == true)
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
boolean splash = getPrefs.getBoolean("splash", true);
if(splash == true) {
setContentView(R.layout.splash);
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
SharedPreferences getPrefs1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean music = getPrefs1.getBoolean("splash_music", true);
if (music == true)
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(2000);
}
catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
Prefs.java
public class Prefs extends PreferenceActivity {
CheckBoxPreference splash;
CheckBoxPreference splash_music;
CheckBoxPreference no_splash_music;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
splash = (CheckBoxPreference) findPreference("splash");
splash_music = (CheckBoxPreference) findPreference("splash_music");
no_splash_music = (CheckBoxPreference) findPreference("without_splash_screen");
splash.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
// TODO Auto-generated method stub
splash.setChecked(true);
splash_music.setChecked(false);
no_splash_music.setChecked(false);
return true;
}
});
splash_music
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
// TODO Auto-generated method stub
splash.setChecked(false);
splash_music.setChecked(true);
no_splash_music.setChecked(false);
return true;
}
});
no_splash_music
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
// TODO Auto-generated method stub
splash.setChecked(false);
splash_music.setChecked(false);
no_splash_music.setChecked(true);
return true;
}
});
}
}
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.cool_menu, menu);
getLayoutInflater().setFactory(new Factory() {
public View onCreateView(String name, Context context,AttributeSet attrs) {
if (name .equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try {
LayoutInflater li = LayoutInflater.from(context);
final View view = li.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
((TextView) view).setTextSize(25);
((TextView) view).setTextColor(Color.RED);
}
}
);
return view;
}
catch (InflateException e) {
}
catch (ClassNotFoundException e) {
}
}
return null;
}
}
);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.aboutUs:
Intent i = new Intent("com.example.checkbox.ABOUT");
startActivity(i);
break;
case R.id.preferences:
Intent p = new Intent("com.example.checkbox.PREFS");
startActivity(p);
break;
case R.id.exit:
finish();
break;
}
return false;
}
#Override
public void onBackPressed() {
finish();
}
}
AboutUs.java
public class AboutUs extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.about);
Button button = (Button)findViewById(R.id.about_button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish(); }
}
);}
}

"FIRST only one checkbox should be checked ."
Not sure how to make this a short and easy answer, but hang with me, this works.
Create a preference activity that you call from your activity.
Use something like this in your onOptionSelected:
case R.id.prefs:
Intent p = new Intent(MainActivity.this, Prefs.class);
startActivity(p);
break;
This is the Prefs.class
public class Prefs extends PreferenceActivity {
CheckBoxPreference splash;
CheckBoxPreference splash_music;
CheckBoxPreference no_splash_music;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
splash = (CheckBoxPreference) findPreference("splash");
splash_music = (CheckBoxPreference) findPreference("splash_music");
no_splash_music = (CheckBoxPreference) findPreference("without_splash_screen");
splash.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
// TODO Auto-generated method stub
splash.setChecked(true);
splash_music.setChecked(false);
no_splash_music.setChecked(false);
return true;
}
});
splash_music
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
// TODO Auto-generated method stub
splash.setChecked(false);
splash_music.setChecked(true);
no_splash_music.setChecked(false);
return true;
}
});
no_splash_music
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
// TODO Auto-generated method stub
splash.setChecked(false);
splash_music.setChecked(false);
no_splash_music.setChecked(true);
return true;
}
});
}
}
And don't forget to add it to the Manifest:
<activity
android:name="com.lordmarty.testforlools.Prefs"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.lordmarty.testforlools.PREFS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The prefs.xml file is the same as the one you posted.
Let me know if you get any errors. I've tested it, and it works fine here.
"SECOND"
Not sure why you have to click twice. But it might be caused by the splash not finishing, just calling a new intent.
Try this where ever you start your mainclass from the splash:
finally{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
finish();
}
Now, I've looked over your project and found out why it where so buggy.
Main reason:
if (music == true)
ourSong.start();
Thread timer = new Thread(){
Here you are missing a brackets for the if statement, causing the thread to run everytime. Therefore you have two MainActivities running at the same time.
Other notes: You use "if"'s for all the values. I suggest you use "else if".
And I did a small cleanup.
Here u go. Enjoy:
Link to Splash.java
You can solve the last problem in many ways. One of them being this:
// add this below the other if else if statements in the splass.java
else if(without_splash_screen == false && splash == false && music == false){
//put whatever you want to happen if none of them are checked. This is just an example.
setContentView(R.layout.splash);
setContentView(R.layout.splash);
Thread timer = new Thread() {
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent(Splash.this,
MainActivity.class);
startActivity(intent);
finish();
}
}
};
timer.start();

You should just be able to change it to
boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
boolean splash = getPrefs.getBoolean("splash", true);
boolean splash_music = getPrefs.getBoolean("splash_music", true);
if (without_splash_screen)
{
...
}
else if (splash)
{
...
}
else if (splash_music)
{
....
}

I implemented Radio buttons inside preference activity that looks like check boxes as follows..
Only one can be selected at a time.
I done this using 2 more xml files
One inside drawable(check_dyn.xml) and another inside layout(radiochecks.xml) .
check_dyn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:state_enabled="true"
android:drawable="#android:drawable/checkbox_off_background" />
<item android:state_checked="true"
android:state_enabled="true"
android:drawable="#android:drawable/checkbox_on_background" />
</selector>
radiochecks.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/check_dyn"
android:checked="true"
android:text="RadioButton1" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/check_dyn"
android:text="RadioButton2" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/check_dyn"
android:text="RadioButton3" />
</RadioGroup>
</LinearLayout>
Now, inside the preference,(here prefs.xml), add a preference and set its layout property. ie,
<Preference android:layout="#layout/radiochecks"
/>
Note: I don't prefer a practice like this, since it is hard to get the individual values.
I prefer ->

Related

Use Multiple Android Activities

Anyone can help me to debug where I m going wrong. Test Code may get from:
https://drive.google.com/file/d/0Bz1lc03pNQm6Qkw3bE93dWxjdXc/view?usp=sharing
Scenario, I have 3 activities
Main, Splash and menu. First I call Splash activity for 5 seconds after 5 seconds I call menu activity its not working from menu I want to call Main activity. If I skip Menu activity than its working fine.
Splash Activity
public class Splash extends Activity{
MediaPlayer ourSong;
#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.dj);
ourSong.start();
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent start = new Intent("com.example.test.menu");
startActivity(start);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
Menu Activity
public class menu extends ListActivity{
String classes[] = {"MainActivity", "example1", "example1", "example1", "example1"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(menu.this, android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try
{
Class ourclass = Class.forName("com.example.test."+cheese);
Intent ourIntent = new Intent(menu.this, ourclass);
startActivity(ourIntent);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
Main Activity
public class MainActivity extends Activity {
int counter;
Button add, subtruct;
TextView textarea;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0 ;
add = (Button)findViewById(R.id.button1);
subtruct = (Button)findViewById(R.id.button2);
textarea = (TextView)findViewById(R.id.textView1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
textarea.setText("Your total is"+counter);
}
});
subtruct.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
textarea.setText("Your total is"+counter);
}
});
}
}
Your code:
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent start = new Intent("com.example.test.menu");
startActivity(start);
}
}
};
timer.start();
Right code:
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent start = new Intent(Splash.this, menu.class);
startActivity(start);
}
}
};
timer.start();
Tip: Don't use lowerCase for Class Names

How to detect if screen is touched in onTouchEvent()

I need to understand on how to detect if user has touched the screen.
Expected Result:-Whenever user touches screen it should skip Splash Screen and move to main activity.
Problem:-Whenever user touches the screen Splash Screen is skipped but in the background sleep(10500) in try block keeps running and as it elapses the Main Activity starts again i.e. it opens two times.
What have I done so far:-I tried do while loop and gave a condition,if the condition is satisfied(of Touch) then break.But I don't seem to get the correct working condition.
Splash Screen Code:-
#Override
protected void onCreate(Bundle splashState) {
// TODO Auto-generated method stub
super.onCreate(splashState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread() {
public void run() {
do
{
try {
//if(onTouchEvent(null))
// break;
sleep(10500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
startActivity(new Intent("com.first.MAINACTIVITY"));
}
}while(false);
}
};
timer.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startActivity(new Intent("com.first.MAINACTIVITY"));
finish();
ourSong.release();
}
return super.onTouchEvent(event);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
ourSong.release();
}
If Statement is provided in the try block so if condition is satisfied it would break.But the condition is unknown to me.Need help with the condition.
Thanks.
private boolean isSplashRunning = true;
#Override
protected void onCreate(Bundle splashState) {
// TODO Auto-generated method stub
super.onCreate(splashState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread() {
public void run() {
do
{
try {
//if(onTouchEvent(null))
// break;
sleep(10500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if(isSplashRunning)
startActivity(new Intent("com.first.MAINACTIVITY"));
}
}while(false);
}
};
timer.start();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isSplashRunning = false; //or in onPause
startActivity(new Intent("com.first.MAINACTIVITY"));
finish();
ourSong.release();
}
return super.onTouchEvent(event);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
isSplashRunning = false;
super.onPause();
finish();
ourSong.release();
}

How to make the application close if splash activity is closed in Android

I have a splash activity that is displayed for 2 seconds before opening the main activity.
If the user presses the back button while the splash activity is being displayed, the splash activity closes. But a short time later, the main activity [which was triggered by the splash activity] opens up.
I don't want this to happen. I want the entire application to close if the back button is pressed while the splash screen is being displayed.
How do I accomplish this?
Edit:
below is my code for splash activity:
public class Splash2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// fading transition between activities
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.activity_splash2);
Thread timer = new Thread() {
public void run() {
try {
sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent open = new Intent(
"com.example.puzzletimer.HOMESCREEN");
startActivity(open);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
Just add this code to your SplashActivity...
#Override
public void onBackPressed() {
super.onBackPressed();
android.os.Process.killProcess(android.os.Process.myPid());
}
or maintain one flag to determine to start Activity or not in Thread...
public class Splash2 extends Activity {
private volatile boolean interrupt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// fading transition between activities
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.activity_splash2);
Thread timer = new Thread() {
public void run() {
try {
sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (!interrupt) {
Intent open = new Intent(
"com.example.puzzletimer.HOMESCREEN");
startActivity(open);
}
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
#Override
public void onBackPressed() {
super.onBackPressed();
interrupt = true;
}
}
Try this-
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Another way to do it is with runnable:
Declare Handler and Runnable in your Splash2 activity:
private Handler handler;
private Runnable startMain;
In onCreate(), initialize them and set the runnable to fire in 1.5 secs:
handler = new Handler();
startMain = new Runnable() {
#Override
public void run() {
Intent open = new Intent("com.example.puzzletimer.HOMESCREEN");
startActivity(open);
}
};
handler.postDelayed(startMain, 1500);
Override onBackPressed() and simply cancel runnable:
#Override
public void onBackPressed() {
handler.removeCallbacks(startMain);
super.onBackPressed();
}
That's it. If back key is pressed, it cancels runnable and your homescreen won't run.

How do I interrupt a splash screen using onTouchListener or onTouchEvent?

public class Splash extends Activity {
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle Samiloveschicken) {
// TODO Auto-generated method stub
super.onCreate(Samiloveschicken);
setContentView(R.layout.splashscreen);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
final Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}
finally{
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(openMainActivity);
Splash.this.finish();
overridePendingTransition(R.anim.mainfadein, R.anim.splashfadeout);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
ourSong.release();
super.onPause();
finish();
}
}
I'm a beginner and I've tried onClickListener, and other various things to no avail.
I can't seem to know where to put this method. I was to be able to interrupt the sleep(5000) when the screen is touched.
Maybe you can set a global flag called interruptThread = false;
Then in on the onTouchListener() you can set interruptThread = true;
and before your sleep(5000); do a check
if(interruptThread) {
thread.interrupt();
} else {
sleep(5000);
}
Try this code, this may help you.......
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_UP) {
ontouch = true;
startActivity(new Intent(your next activity));
finish();
return true;
}
return false;
};

Activity will run, but not show up (Android)?

I'm just trying to test some stuff with a splash screen. The strangest thing happens when I run the app though. I can see my Log messages in the LogCat, but the activity itself won't show up. Once the loop finishes, it starts the next activity, which does in fact show up. If I comment out the UIThread, it will show up though. I know I'm doing something simple wrong, but I'm not sure what it is. Ideas?
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#000000">
<ImageView
android:id="#+id/logoIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:paddingTop="50dp"
android:src="#drawable/logoa"
/>
Java:
public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
final ImageView logo = (ImageView) findViewById(R.id.logoIV);
final int[] anim = new int[6];
anim[0]=R.drawable.logoa;
anim[1]=R.drawable.logob;
anim[2]=R.drawable.logoc;
anim[3]=R.drawable.logod;
anim[4]=R.drawable.logoe;
anim[5]=R.drawable.logof;
runOnUiThread(new Runnable() {
int img = 0, counter=0;
boolean up = true;
public void run() {
while(counter<21){
logo.setImageResource(anim[img]);
if(up){
img++;
if(img>=5)
up=false;
}else{
img--;
if(img<=0)
up=true;
}
try{
Thread.sleep(150);
}catch (InterruptedException e){
e.printStackTrace();
}
counter++;
Log.e("Tag",Integer.toString(counter));
}
if(counter>=21){
Intent creditsIntent = new Intent(Splash.this, TitlePage.class);
creditsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Splash.this.startActivity(creditsIntent);
}
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
change your oncreate method like this
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
final ImageView logo = (ImageView) findViewById(R.id.logoIV);
final int[] anim = new int[6];
anim[0] = R.drawable.logoa;
anim[1] = R.drawable.logob;
anim[2] = R.drawable.logoc;
anim[3] = R.drawable.logod;
anim[4] = R.drawable.logoe;
anim[5] = R.drawable.logof;
Thread t = new Thread(new Runnable()
{
int img = 0, counter = 0;
boolean up = true;
#Override
public void run()
{
while (counter < 21)
{
runOnUiThread(new Runnable()
{
public void run()
{
logo.setImageResource(anim[img]);
}
});
if (up)
{
img++;
if (img >= 5)
up = false;
}
else
{
img--;
if (img <= 0)
up = true;
}
try
{
Thread.sleep(150);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
counter++;
Log.e("Tag", Integer.toString(counter));
}
if (counter >= 21)
{
runOnUiThread(new Runnable()
{
public void run()
{
Intent creditsIntent = new Intent(Splash.this, TitlePage.class);
creditsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Splash.this.startActivity(creditsIntent);
}
});
}
}
});
t.start();
}
instead of calling runOnUiThread directly do the following:
new Timer().schedule(new TimerTask()
{
#Override
public void run()
{
runOnUiThread(new Runnable()
{
int img = 0, counter=0;
.....
}
}
}, 1000);
I'm not sure if your animation will work, but surely your activity will show up.

Categories

Resources