I have a clown honking app. When the user presses the imagebutton that takes up the whole screen, it plays a mediaplayer sound that lasts for half a second.
I want it to play toasts that say HONK, HONK!!!, and HONK!!!!!!! randomly, as well as keep a counting variable that toasts AMAZING HONKS when that count reaches a (%10 == 0) and/or a (%100 == 0) statement.
I added an on touch and an onbuttonlistener, and Im geting nowhere with my count. I also want to have the imagebutton take the whole screen and have a textview on top of the imagebutton...
Posting my MAIN and my XML:
public class MainActivity extends Activity implements OnTouchListener{
public void amazinghonks(View view)
{
MediaPlayer mp = MediaPlayer.create(this, R.raw.bikehorn);
mp.start();
}
private SoundPool soundPool;
private int soundID;
private int soundID2;
boolean loaded = false;
boolean songloaded = false;
int honkcount = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.toptext);
view.setOnTouchListener(this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.bikehorn, 1);
soundID2 = soundPool.load(this, R.raw.clown, 1);
Toast.makeText(MainActivity.this,
"HONK!!!", Toast.LENGTH_LONG).show();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
// Is the sound loaded already?
if (loaded && (honkcount < 3)) {
honkcount++;
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
if (songloaded && (honkcount > 10)) {
honkcount = 0;
soundPool.play(soundID2, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
}
return false;
}
public void addListenerOnButton() {
ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
double randomnumber = Math.random()*101;
//WILL HAVE THREE KINDS OF HONKS!
if(randomnumber < 33)
{
Toast.makeText(MainActivity.this,
"HONK!", Toast.LENGTH_SHORT).show();
}
if((randomnumber < 66)&&(randomnumber > 33))
{
Toast.makeText(MainActivity.this,
"HONK!!!", Toast.LENGTH_SHORT).show();
}
if((randomnumber < 101)&&(randomnumber > 66))
{
Toast.makeText(MainActivity.this,
"HONK!!!!!!!!!!!!!!!!!!!!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/toptext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#303030"
android:text="@string/toptext"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#33B5E5" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.62"
android:contentDescription="@string/honktext"
android:onClick="amazinghonks"
android:src="@drawable/big_clown" />
</LinearLayout>
As far as I can tell, you never call your addListenerOnButton()
method, so it's never executed. Try calling it in your onCreate()
method.
For the ImageButton
to take up the whole view, with a TextView
on top (as in greater z-index, right?) You'd want to use a RelativeLayout
instead of LinearLayout
. And try setting the height of your ImageButton
to "fill_parent" and not 0dip. A height of 0 will effectively hide it, making it impossible to click.