Search code examples
androidandroid-viewandroid-custom-view

Android onClick method doesn't work on a custom view


I've started working on an app. I build the menu yesterday but the onClick method doesn't work! I created a class that extends View and called her MainMenuObject - that class is for any object in the main menu (buttons, logos etc). I've created a special class for them because I'm doing an animation when the menu starts. After I've built the MainMenuObject class I've built another class (OpeningTimesView) that extends View and will have all the buttons of the main menu in it, and will function as the main activity's layout.

Everything was good, the animation went very well and I wanted to put listeners on my buttons, so I've added an implemention of onClickListener to the OpeningTimesView class, and overrided the onClick method. Then I've added the listener to the buttons with setOnClickListener(this) and setClickable(true), but it doesn't work! I've tried everything! Please help me figure out what I'm doing wrong. I've added a toast to the onClick method that doesn't depend on any "if" but it's won't show neither.

(BTW is there any way to define the screen width and height as variable that all classes can access? it can't be static because you get the height and width from a display object but there must be another way)

this is the code:

public class OpeningTimesView extends View implements OnClickListener{
    private MainMenuObjectView searchButton;
    private MainMenuObjectView supportButton;
    private MainMenuObjectView aboutButton;
    private int screenWidth;
    private int screenHeight;
    public OpeningTimesView(Context context, Display dis) {
        super(context);

        this.screenWidth = dis.getWidth();
        this.screenHeight = dis.getHeight();

        searchButton = new MainMenuObjectView(context, 200, MovingMode.RIGHT, R.drawable.search, dis);
        supportButton = new MainMenuObjectView(context, 400, MovingMode.LEFT, R.drawable.support, dis);
        aboutButton = new MainMenuObjectView(context, 600, MovingMode.RIGHT, R.drawable.about, dis);

        searchButton.setClickable(true);
        supportButton.setClickable(true);
        aboutButton.setClickable(true);

        searchButton.setOnClickListener(this);
        supportButton.setOnClickListener(this);
        aboutButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view){
        Toast.makeText(getContext(), "Search button pressed", Toast.LENGTH_SHORT).show();
        if(view == searchButton){
            Toast.makeText(getContext(), "Search button pressed", Toast.LENGTH_SHORT).show();
        }
        else if(view == supportButton){
            Toast.makeText(getContext(), "Support button pressed", Toast.LENGTH_SHORT).show();
        }
        else Toast.makeText(getContext(), "About button pressed", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onDraw(Canvas canvas)
    {
        // Drawing the buttons
        this.searchButton.onDraw(canvas);
        this.aboutButton.onDraw(canvas);
        this.supportButton.onDraw(canvas);
    }

Thanks in advance, Elad!


Solution

  • I've got a solution! It's not really a solution for this specific issue, but a whole new approach. I sent this thread to somebody I know and he told me to use the Animation SDK the android has (like Wireless Designs mentioned), so instead of doing the main menu page with 4 classes, I'm doing it only with one class that extends Activity, and the Animation class offers many animation options. I want to thank you both for helping me, you are great. I'm adding the code if someone will encounter this thread with the same problem or something:

    package elad.openapp;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.HapticFeedbackConstants;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.view.animation.Animation;
    import android.view.animation.AnimationSet;
    import android.view.animation.ScaleAnimation;
    import android.view.animation.TranslateAnimation;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class OpeningTimes extends Activity implements OnClickListener{
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        // Disabling the title bar..
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
    
        // Create the buttons and title objects
        ImageView title = (ImageView)findViewById(R.id.title_main);
        ImageView search = (ImageView)findViewById(R.id.search_button_main);
        ImageView support = (ImageView)findViewById(R.id.support_button_main);
        ImageView about = (ImageView)findViewById(R.id.about_button_main);
    
        // Setting the onClick listeners
        search.setOnClickListener(this);
        support.setOnClickListener(this);
        about.setOnClickListener(this);
    
        setButtonsAnimation(title, search, support, about);
    
    }
    
    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.search_button_main){
            v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
            startActivity(new Intent(this,SearchPage.class));
        }
        else if(v.getId()==R.id.support_button_main){
            v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
            Toast.makeText(this, "Coming soon...", Toast.LENGTH_LONG).show();
        }
        else if(v.getId()==R.id.about_button_main){
    
            v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
            Toast.makeText(this, "Coming soon...", Toast.LENGTH_LONG).show();
        }
    }
    
    // Setting the animation on the buttons
    public void setButtonsAnimation(ImageView title, ImageView search, ImageView support, ImageView about){
        // Title animation (two animations - scale and translate)
        AnimationSet animSet = new AnimationSet(true);
    
        Animation anim = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
        anim.setDuration(750);
        animSet.addAnimation(anim);
    
        anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        anim.setDuration(750);
        animSet.addAnimation(anim);
    
        title.startAnimation(animSet);
    
        // Search button animation
        anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.5f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        anim.setDuration(750);
    
        search.startAnimation(anim);
    
        // Support button animation
        anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.5f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        anim.setDuration(750);
    
        support.startAnimation(anim);
    
        // About button animation
        anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 3f, Animation.RELATIVE_TO_SELF, 0.0f);
        anim.setDuration(750);
    
        about.startAnimation(anim);
    }
    }