0

is it possible to add sound to navigation drawer buttons? I managed to add sound to normal buttons by doing this;

final MediaPlayer sound = MediaPlayer.create(getApplicationContext(), R.raw.buttonsound)
Button button = (button) findViewById(R.id.button);
ok.setOnClickListener(new View.onClickListener(){
public void onClick(View arg0){
sound.start();
}
});
Android
  • 171
  • 1
  • 4
  • 16

1 Answers1

0

You must have a callback in your Navigation-drawer fragment. This callback interface should be implemented in your Main Activity. Then you can override the onClick.

From the Navigation Drawer :

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(
                R.layout.fragment_navigation_drawer, container, false);
        mDrawerListView = (ListView) rootView.findViewById(R.id.listView);
        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //A very basic callback, ideally you would want to perform null-checks
        mCallbacks.onNavigationDrawerItemSelected(position);
            }
        });

The Callback interface :

public static interface NavigationDrawerCallbacks {
    /**
     * Called when an item in the navigation drawer is selected.
     */
    void onNavigationDrawerItemSelected(int position);
    void onNotificationClicked();
}

Implement this interface in your MainActivity :

public class MyActivity extends FragmentActivity implements

        NavigationDrawerFragment.NavigationDrawerCallbacks,.......

Finally Overrride the onClick:

    @Override
    public void onNavigationDrawerItemSelected(int position) {

        switch (position) {

            case 0 : playMusicAndHandleClick();
    }
}
Dexter
  • 1,710
  • 2
  • 17
  • 34