I'm writing an Android application that plays different sounds when buttons are pressed. There will be 24 buttons on screen in a gridview. I have currently have two classes (and therefore 2 .java files); a class containing the oncreate method and a class extending BaseAdapter (to fill my gridview with buttons). The "onCreate()" class which extends Activity and implements OnClickListener is called PlayFrets. The class extending BaseAdapter is called ButtonAdapter. My questions are about how to fill all 24 elements in my gridview with unique buttons that each play a different sound file (using the soundpool).
My getView() method within ButtonAdapter.java looks like this:
public View getView(int position, View convertView, ViewGroup parent){
Button b;
if (convertView == null){
b = new Button(mContext);
b.setLayoutParams(new GridView.LayoutParams(7,100)); //width = 7dp, height = 100dp
b.setPadding(8, 8, 8, 8);
}
else{
b = (Button)convertView;
}
b.setId(position);
b.setHighlightColor(Color.BLUE); //button turns blue when highlighted
//should the listener be its own class or a method within PlayFrets?
b.setOnClickListener(new MyOnClickListenerPosition(position));
return b;
}
As you will notice from reading my comments, my first question is how to handle the OnClickListener. I know I can make a new class called MyOnClickListenerPosition that will execute code based on which position, ie which button, in the grid was clicked. My confusion comes from not knowing if this onclicklistener should be its own class, or if I just have an onclick() method within PlayFrets to handle this. Below is what my current onClick() method looks like, note that this is within PlayFrets.java after the onCreate() method:
@Override
public void onClick(View v) {
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
switch (v.getId()) {
case R.id.fx01:
mSoundPool.stop(mStream1);
mStream1= mSoundPool.play(mSoundPoolMap.get(SOUND_FX_01), streamVolume, streamVolume, 1, 0, 1f);
break;
//other cases for other 23 buttons go here
}
}
Can I call this method from my getView() method within ButtonAdapter? If so, how?
My next question involved how to set up my xml file(s) for a gridview with 24 buttons. I currently have one xml file with the contents below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fret_board2"
tools:context=".MainActivity" >
<GridView
android:id="@+id/frets"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:columnWidth="7dp"
android:gravity="center"
android:numColumns="6"
android:stretchMode="columnWidth" />
</RelativeLayout>
This instantiates my gridview with 6 columns. Do I need a separate xml file that instantiates my 24 buttons? Also, in my code for the onClick() method you will notice I'm using a switch statement to play a unique sound from the soundpool based on the value of View v's ID. How will I get the integer value of position to correspond to a unique v.getId() value that my switch statement will properly work and play the right sound?
Sorry for such a loaded question but I am new to Android so I'm really trying to broaden my understanding of Android App writing, the guides on the developer site only help so much to the inexperienced programmer. Thank you in advance for your help!
No comments:
Post a Comment