Select only one checkbox with splash settings

I have the launch of the application depends on the preference of the user with three different flags:

1- run the application without splashes and music.

2- run the application with a splash only.

3- launch the application with sleep and music.

with the code below its working fine.

But two more points:

FIRST should only have one checkbox selected.

SECOND after you checked, if you want, go back to mainactivity, here you can exit the application either using the back button or button , which I already have in my option menu, the problem is that either using the button back , or the exit button, it does not respond to the first click, I need to double-click to exit the application.

but I can’t reach it,

Any help please would 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 the 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 the code below as well, but it doesn’t change anything, but all the boxes 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 one more code, but it doesn’t change anything, all the checkboxes 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 (THE WHOLE PROJECT):

1- one checkbox is set perfectly.

2- either using the "Back" or "Exit" button in MainActivity, does not respond to the first click, I have to click twice or three times to exit the application.

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(); } } );} } 
+4
source share
3 answers

"FIRST only one flag should be checked."

Not sure how to make this short and simple answer, but hang with me, it works.

Create a preference activity that you invoke 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 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 tested it and it works great here.

"SECOND"

Not sure why you need to double click. But this may be due to the fact that the spray does not end, just calling a new intention. Try this when you start your main class from a splash:

  finally{ Intent intent = new Intent(Splash.this, MainActivity.class); startActivity(intent); finish(); } 

Now I looked at your project and found out why it is so buggy.

The main reason:

  if (music == true) ourSong.start(); Thread timer = new Thread(){ 

Here you are missing parentheses for the if statement, forcing the thread to start every time. This way you are launching two MainActivities at the same time.

Other notes: you use "if" for all values. I suggest you use "else if".

And I did a little cleaning.

Here we go. Enjoy:

Link to Splash.java

You can solve the last problem in many ways. One of them:

 // 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(); 
+2
source

You can just 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) { .... } 
0
source

I implemented the Radio buttons inside the preference function, which look like checkboxes as shown below.

enter image description here

Only one can be selected at a time. I did this using two more xml files

enter image description here

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, in preference (here prefs.xml), add preference and set its layout property. i.e,

 <Preference android:layout="@layout/radiochecks" /> 

Note. I do not prefer such a practice, since it is difficult to obtain individual values.

I prefer β†’

From settingsenter image description here

0
source

Source: https://habr.com/ru/post/1491228/


All Articles