Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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(); }
                      }
                  );}
                 }
like image 787
androidqq6 Avatar asked Oct 21 '22 05:10

androidqq6


1 Answers

"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(); 
like image 131
LordMarty Avatar answered Nov 15 '22 07:11

LordMarty