Search code examples
androidandroid-layoutandroid-intentcheckboxandroid-mediaplayer

Checkbox in menu Android


I am new into Android programming. I am currently integrating media player in my app. Via the menu, I want the user to turn off the sound, but also start it again if desired. Right now I have a checkbox that is checked when the music is played and the music to be shut off when not checked.

I have managed to insert checkbox in the menu and got it to be checked when the app starts, just as it should. But when I try to uncheck it, the app shuts down ...

Anyone have publisher? See obvious errors? Or just want to give tips?

MainActivity

@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        //Switch-sats i syfte om att det kommer tillkomma fler alternativ
        //Switchen bygger på att hämta rätt id ifrån användarens val
        switch (item.getItemId())
        {
        case R.id.action_help:
                //Bytar Activity till help
                Intent intent = new Intent(MainActivity1.this, help.class);
                startActivity(intent);          
            return true;

        case R.id.music:
            final CheckBox music = (CheckBox)findViewById(R.id.music);
            music.setChecked(true);
            music.setOnClickListener(new OnClickListener(){
                public void onClick(View v){

                if (((CheckBox) v).isChecked()) {
                    backsound = MediaPlayer.create(MainActivity1.this, R.raw.backsound);  
                    backsound.start();
                    backsound.setLooping(true);
                }else{
                    music.setChecked(false);
                    backsound.stop();
                }
            }});

        default:
            return super.onOptionsItemSelected(item);
        }}}

Main_activity.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

<item
    android:id="@+id/action_help"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/help"/>

<item
     android:id="@+id/music" 
     android:title="@string/musik" 
     android:checkable="true"
     android:checked="true" />


Solution

  • <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
         <PreferenceCategory
               android:summary="@string/summary_category"
               android:title="@string/title_category">
               <CheckBoxPreference
                     android:key="main"
                     android:defaultValue="true"
                     android:summary="@string/summary_main"
                     android:title="@string/title_main" 
              />
                      </PreferenceCategory>
    <!--Any other categories include here-->
    </PreferenceScreen>
    

    You can do this simply by setting android:dependancy to the key of the check box in which the respective check boxes must depend on.

    Now create a folder named xml in res folder and put your preferences xml file inside that. Then do the following.

     public class SettingsActivity extends PreferenceActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
    
    
        }
    
    }    
    

    You can also do this with fragments which is more recommended. But the above method is much more easy. If you want to do that with fragments, check this which contains everything you need to know about creating a Settings Activity.

    Hope this helps!!!!!!!