Skip to content Skip to sidebar Skip to footer

Only Select One Checkbox Of Splash Preference Checkedboxs

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

Solution 1:

"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 = newIntent(MainActivity.this, Prefs.class);
        startActivity(p);
        break;

This is the Prefs.class

publicclassPrefsextendsPreferenceActivity {
CheckBoxPreference splash;
CheckBoxPreference splash_music;
CheckBoxPreference no_splash_music;


@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stubsuper.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(newOnPreferenceChangeListener() {

        @OverridepublicbooleanonPreferenceChange(Preference preference,
                Object newValue) {
            // TODO Auto-generated method stub

            splash.setChecked(true);
            splash_music.setChecked(false);
            no_splash_music.setChecked(false);

            returntrue;
        }

    });

    splash_music
            .setOnPreferenceChangeListener(newOnPreferenceChangeListener() {

                @OverridepublicbooleanonPreferenceChange(Preference preference,
                        Object newValue) {
                    // TODO Auto-generated method stub

                    splash.setChecked(false);
                    splash_music.setChecked(true);
                    no_splash_music.setChecked(false);

                    returntrue;
                }

            });

    no_splash_music
            .setOnPreferenceChangeListener(newOnPreferenceChangeListener() {

                @OverridepublicbooleanonPreferenceChange(Preference preference,
                        Object newValue) {
                    // TODO Auto-generated method stub

                    splash.setChecked(false);
                    splash_music.setChecked(false);
                    no_splash_music.setChecked(true);

                    returntrue;
                }

            });

   }

 }

And don't forget to add it to the Manifest:

<activityandroid:name="com.lordmarty.testforlools.Prefs"android:label="@string/app_name" ><intent-filter><actionandroid:name="com.lordmarty.testforlools.PREFS" /><categoryandroid: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();

    Threadtimer=newThread(){

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.javaelseif(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() { 
            publicvoidrun() { 
                try { 
                    sleep(2000); 
                } catch (InterruptedException e) { 
                    e.printStackTrace(); 
                } finally { 
                    Intent intent = new Intent(Splash.this, 
                            MainActivity.class); 
                    startActivity(intent); 
                    finish(); 


                } 
            } 
        }; 
        timer.start(); 

Solution 2:

You should just be able to change it to

booleanwithout_splash_screen= getPrefs.getBoolean("without_splash_screen", true);
    booleansplash= getPrefs.getBoolean("splash", true);
    booleansplash_music= getPrefs.getBoolean("splash_music", true);

    if (without_splash_screen)
    {   
        ...
    }
    elseif (splash)
    {
         ...
    }
    elseif (splash_music)
    {
        ....
    }

Solution 3:

I implemented Radio buttons inside preference activity that looks like check boxes as follows..

enter image description here

Only one can be selected at a time. I done this using 2 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"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_checked="false"android:state_enabled="true"android:drawable="@android:drawable/checkbox_off_background" /><itemandroid:state_checked="true"android:state_enabled="true"android:drawable="@android:drawable/checkbox_on_background" /></selector>

radiochecks.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><RadioGroupandroid:id="@+id/radioGroup1"android:layout_width="wrap_content"android:layout_height="wrap_content" ><RadioButtonandroid:id="@+id/radio0"android:layout_width="wrap_content"android:layout_height="wrap_content"android:button="@drawable/check_dyn"android:checked="true"android:text="RadioButton1" /><RadioButtonandroid:id="@+id/radio1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:button="@drawable/check_dyn"android:text="RadioButton2" /><RadioButtonandroid: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,

<Preferenceandroid:layout="@layout/radiochecks"
    />

Note: I don't prefer a practice like this, since it is hard to get the individual values.

I prefer ->

From settingsenter image description here

Post a Comment for "Only Select One Checkbox Of Splash Preference Checkedboxs"