Scalified / fab

Floating Action Button Library for Android
Apache License 2.0
849 stars 164 forks source link

onTouchEvent to handle onClickListener #5

Closed samuelmgalan closed 9 years ago

samuelmgalan commented 9 years ago

Hi.

How is onTouchEvent supposed to work? I can't find the correct way to handle onClick events. I've tried setOnClickListener and setOnTouchListener without luck. And I can't cast FloatingActionButton to Button because it gives an error.

Can you provide an example? Thanks!

vbaidak commented 9 years ago

Hi samuelmgalan,

  1. Try this one to handle onTouchEvent call:
@Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
            // Your code goes here
        return true; // if handled, otherwise false
    }

So you just need to call super.onTouchEvent(event) and return true if your button click is handled, otherwise false

  1. FloatingActionButton extends directly the android.view.View class, so it can't be casted to android.widget.Button class
vbaidak commented 9 years ago

Here is how it is done in sample:

Declaration inside XML:

<com.software.shell.fab.FloatingActionButton
            android:id="@+id/fab_activity_action_button"
            style="@style/fab_action_button_style"
            android:onClick="onActionButtonClick"
            fab:type="DEFAULT"
            />

and then onActionButtonClick method in code:

public void onActionButtonClick(View v) {
        if (hideAndShowOnClickCheckBox.isChecked()) {
            actionButton.hide();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    actionButton.show();
                }
            }, ACTION_BUTTON_SHOW_DELAY_MS);
        }
    }

I will provide the full demo sample a bit later today

samuelmgalan commented 9 years ago

Thanks for your examples.

My problem is that onTouchEvent() cannot be overriden inside a Fragment, but that's another problem.

Thanks again!

vbaidak commented 9 years ago

What about setting the click listener to this button? Did you manage to make it work?

samuelmgalan commented 9 years ago

It doesn't work. Never gets called and I think it should be, but i have no idea why not.

vbaidak commented 9 years ago

Could you please send me the code here: com.software.shell@gmail.com

I'll try to help

vbaidak commented 9 years ago

great

From: samuelmgalan [mailto:notifications@github.com] Sent: Monday, February 16, 2015 2:32 PM To: shell-software/fab Cc: Shell Software Subject: Re: [fab] onTouchEvent to handle onClickListener (#5)

Closed #5 https://github.com/shell-software/fab/issues/5 .

— Reply to this email directly or view it on GitHub https://github.com/shell-software/fab/issues/5#event-236286527 .