Note that there are some explanatory texts on larger screens.

plurals
  1. POOnly select one checkbox of splash preference checkedboxs
    primarykey
    data
    text
    <p>I have app starting depend on user preferance with three different checkedbox :</p> <p>1- start app without splash and music .</p> <p>2- start app with splash only .</p> <p>3- start app with spalsh and music .</p> <p>with the below code its work perfectly .</p> <p><strong>But still two point to be achieved :</strong></p> <p><strong>FIRST</strong> only one checkbox should be checked .</p> <p><strong>SECOND</strong> after you checked any one of checkboxs as you prefer then go back to mainactivity , here you can exit the app either by use <strong>back button</strong> or <strong>exit button</strong> which i already have it in my option menu , the problem is that either using <strong>back button</strong> or <strong>exit button</strong> it doesn't respond to first click , i have to click twice to exit the app.</p> <p>but i cant achieve it ,</p> <p>any help please will be appreciated .</p> <pre><code>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(); } } </code></pre> <p>In xml folder :<strong>prefs.xml</strong></p> <pre><code> &lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;CheckBoxPreference android:title="without splash screen" android:defaultValue="true" android:key="without_splash_screen" android:summary="Start app without splash"/&gt; &lt;CheckBoxPreference android:title="splash screen" android:defaultValue="true" android:key="splash" android:summary="Start app with splash only" /&gt; &lt;CheckBoxPreference android:title="splash screen music" android:defaultValue="true" android:key="splash_music" android:summary="Start app with splash and music" /&gt; &lt;/PreferenceScreen&gt; </code></pre> <p><strong>UPDATE:</strong> </p> <p>i tried also the below code but it doesn't change any thing , still all chechboxs can be checked :</p> <pre><code>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(); } } </code></pre> <p><strong>UPDATE: i tried also another code but it doesn't change any thing , still all chechboxs can be checked :</strong></p> <pre><code> 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(); } } </code></pre> <p><strong>Any advice , thanks.</strong></p> <p><strong>UPDATE 3 (WHOLE PROJECT):</strong> </p> <p><strong>1- one checkbox checked achieved perfectly .</strong></p> <p><strong>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.</strong></p> <p><strong>Splash.java</strong></p> <pre><code> 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(); } } </code></pre> <p><strong>Prefs.java</strong></p> <pre><code> 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; } }); } } </code></pre> <p><strong>MainActivity.java</strong></p> <pre><code> 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(); } } </code></pre> <p><strong>AboutUs.java</strong></p> <pre><code> 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(); } } );} } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload