0

I am trying to create a gridView with ImageButtons inside them, so when pressed-a sound is emitted. So far I have my GridView_layout.xml, a layout for my button.xml. My gridAdapter to populate the grid with the buttons. With my current code the grid gets populated with the buttons, but when pressed no sound is emitted.

Here is part of my button activity which extends activity, the following code block is inside the onCreate() method:

        final Context context = getApplicationContext();
        final int duration = Toast.LENGTH_SHORT;
        final ImageButton one = (ImageButton) findViewById(R.id.imageButton);

gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {

                one.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {


                        final MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.pears);

                        if (mp.isPlaying()) {
                            mp.stop();
                            mp.reset();
                        }

                        if (mp == null) {
                            Toast toast = Toast.makeText(context, "button error", duration);
                            toast.show();
                        } else {

                            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                                @Override
                                public void onCompletion(MediaPlayer mediaplayer) {
                                    mp.stop();
                                    mp.release();
                                }
                            });
                            mp.start();
                        }
                    }
                });
            }
        });

getView() method from gridAdapter as requested:

public View getView(int position, View convertView, ViewGroup parent) {

    ImageButton imageButton;
    if (convertView == null) {

        // if it's not recycled, initialize some attributes
        imageButton = new ImageButton(mContext);
        imageButton.setLayoutParams(new GridView.LayoutParams(190, 190));
        imageButton.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageButton.setBackgroundColor(white);

    } else {
        imageButton = (ImageButton) convertView;
    }

    imageButton.setImageResource(mThumbIds[position]);
    return imageButton;
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Noonmoon
  • 21
  • 2
  • 9

1 Answers1

0

If you want to play a sound while pressing an item in gridview change your code to this:

gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {

   MediaPlayer  mPlayer = MediaPlayer.create(this, R.raw.pears);
   mPlayer .start();
        }
    });

If you want to play a sound while clicking on a button inside one of the grid items, then use this code inside the getview method in your adapter:

    one.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
         MediaPlayer  mPlayer = MediaPlayer.create(this, R.raw.pears);
         mPlayer .start();
       }
       });
Jossy Paul
  • 1,267
  • 14
  • 26
  • I tried your latter suggestion by remove: gridview.setOnItemClickListener with one.setOnClickListerner(), but it doesn't work, I get a NullPointerException error – Noonmoon Mar 27 '15 at 03:09
  • are you sure that you set the onclick listener after initializing the image button. Could you provide the error log – Jossy Paul Mar 27 '15 at 06:13
  • The onClickListener is placed after the ImageButton is initialized. From logcat I get this error: Caused by: java.lang.NullPointerException at com.non.nonabona.sounds.SoundButtonsActivity.onCreate(SoundButtonsActivity.java:44) specifically at line one.setOnClickListener. – Noonmoon Mar 27 '15 at 15:37