Fluffhead
Fluffhead

Reputation: 845

Android button onClick not working

I have avery simple program which uses MediaPLayer. I am new to java and xml and for the life of me can't figue out why the resume button I created won't respond to a click event. It does sit on top of a larger imageButton, which should not repsond in this state, but I think the top level button should regardless, no?

package com.whynoceros.tonguechi;

 import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;

public class ArtistsActivity extends Activity {

Button restart, resume, stop;
ImageButton play;
MediaPlayer audio;
boolean playing;
boolean paused;
boolean starting;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.tonguechi);
    audio = MediaPlayer.create(ArtistsActivity.this, R.raw.tongue_chi_mix1);

    playing = false;
    paused = false;
    starting = true;
    play = (ImageButton) findViewById(R.id.playbutton);
    stop = (Button) findViewById(R.id.stopbutton);
    resume = (Button) findViewById(R.id.resumebutton);
    restart = (Button) findViewById(R.id.restartbutton);



}
/*      play.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            audio.start();
            play.setBackgroundResource(R.drawable.pausebutton2x);
        }

    });
   */ 

public void playNow(View view) {
    if(paused){

    }

    if(playing){
        audio.pause();
        //play.setImageResource(R.drawable.playbutton2x);
        Animation slideA = AnimationUtils.loadAnimation(this, R.anim.stopani);
        stop.startAnimation(slideA);
        Animation slideB = AnimationUtils.loadAnimation(this, R.anim.resumeani);
        resume.startAnimation(slideB);
        Animation slideC = AnimationUtils.loadAnimation(this, R.anim.restartani);
        restart.startAnimation(slideC);
        playing=false;
        paused=true;
    }
    if(starting){
    audio.start();
    play.setImageResource(R.drawable.pausebutton2x);
    playing=true;
    starting=false;
    paused=false;
    Log.i("play","");


}
}

public void resumeNow(View v) {
    audio.start();
    Animation slideD = AnimationUtils.loadAnimation(this, R.anim.stopani);
    stop.startAnimation(slideD);
    Animation slideE = AnimationUtils.loadAnimation(this, R.anim.resumeani_rev);
    resume.startAnimation(slideE);
    Animation slideF = AnimationUtils.loadAnimation(this, R.anim.restartani_rev);
    restart.startAnimation(slideF);
    play.setEnabled(true);
    playing=true;
    paused=false;
    Log.i("resume","");

}

@Override
protected void onDestroy() {
    super.onDestroy();
    // TODO Auto-generated method stub
        audio.release();
    }

}





<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/playback2x"
android:orientation="vertical">

<ImageButton
    android:id="@+id/playbutton"
    android:layout_width="320dp"
    android:layout_height="131dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="34dp"
    android:scaleType="fitCenter"
    android:background="@android:color/transparent"
    android:src="@drawable/playbutton2x"
    android:contentDescription="button"
    android:onClick="playNow"/>

<Button
    android:id="@+id/resumebutton"
    android:layout_width="75dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="resume" android:layout_marginTop="-60dp"
    android:onClick="resumeNow"/>

<Button
    android:id="@+id/restartbutton"
    android:layout_width="75dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="restart" android:layout_marginTop="-60dp"/>

<Button
    android:id="@+id/stopbutton"
    android:layout_width="75dp"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="stop" android:layout_marginTop="-60dp"/>



<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/playbutton"
    android:layout_centerHorizontal="true"
    android:src="@drawable/tips2x" />


<ScrollView
android:id="@+id/SCROLLER_ID"
android:layout_width="280dp"
android:layout_height="240dp"
android:layout_alignTop="@+id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="53dp"
android:scrollbars="vertical"
android:fillViewport="true">
    <TextView
        android:id="@+id/editText1"
        android:layout_width="280dp"
        android:layout_height="240dp"
        android:layout_alignTop="@+id/imageView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="53dp"
        android:inputType="textMultiLine"
        android:text="@string/tips_text"
        android:textColor="#6F372D"
        android:textSize="14dp"
        android:textStyle="normal"
        android:typeface="serif" />

Upvotes: 0

Views: 4404

Answers (2)

Fluffhead
Fluffhead

Reputation: 845

I managed to solve my own problem using the following approach:

I replaced all the xml animation with java TranslateAnimation.

I created two separate xml layouts, one for the position of the buttons prior to animation, the other for after. In both layouts, I reference the same buttons, ids, etc...

I created a setAnimationListener each time I begin an animation and then in the onAnimationEnd method, I apply the other xml layout. "setContentView(R.layout.layout2);" Also, immediately after setting the contentView, I had to re-instantiate the buttons, in order to get the click to work.

Now, once the button stops moving, it's actual location is updated to the final location of it's appearance, vis-a-vis the animation.

Upvotes: 0

5hssba
5hssba

Reputation: 8079

android:onClick is for API level 4 onwards, so if you're targeting < 1.6, then you can't use it... instead you should use onclicklistener.. i think you are targeting android 1.5

Upvotes: 1

Related Questions