0

I have created an application which uses the fragment, I am opening a fragment on the click of the first fragment with custom animation, meanwhile the animation is going on I am able to click the button on the first fragment and it creates 2 fragments. how can I not click on my button while moving from one fragment to another, I just don't want double click of the same button.

can anyone help me?

6 Answers 6

2

Try below

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            view.setEnabled(false);
        }
    });
    // on animation complete, enable it
    // button.setEnabled(true); 
0

You could try set android:clickable in your XML layout to determine whether a button can be clicked.

0

You could implement the following method into your code and call it when needed.

public void myMethod(boolean isLoading){
    myButton.setEnabled(!isLoading);
}
0
button.setOnClickListener(new View.OnClickListener() 
{
        @Override
        public void onClick(View view) {view.setEnabled(false);}
});
0

Try using myButton.setEnabled(false) in your click callback function.

1
  • 2
    Your button will still be disabled if you ever come back to this fragment if it is pushed to the back stack. Aug 2, 2018 at 18:06
0

Try the below kotlin snippet

view.setOnClickListener {
    val tag = "my_dialog"
    val oldFragment: Fragment? = supportFragmentManager.findFragmentByTag(tag)
    if(!(oldFragment?.isAdded == true)) {
         val myDialogFragment = MyDialogFragment.newInstance()
         myDialogFragment.show(supportFragmentManager, tag)
    }
}

In case a fragment(with the tag specified) is already added to the activity then this code prevents new fragment creation and adding it to the activity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.