Scalified / fab

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

Both buttons show up when I only want one at a time #8

Closed kenny0202 closed 9 years ago

kenny0202 commented 9 years ago

I have a edit and save button. When editButton is clicked the editButton hides and the saveButton shows. The problem I am having is when the activity is first loaded saveButton.hide() does not hide the button. I made a if statement to switch between the buttons but they dont work the way i want it to. I have began to make the code more redundant and nothing still works. It seems to me that my logic is correct, but the application is not doing what I want it to do.

ActionButton editButton = (ActionButton) findViewById(R.id.edit_button);
ActionButton saveButton = (ActionButton) findViewById(R.id.save_button);
editButton.show();
saveButton.hide(); // saveButton.hide(); does not work initially on load up

editButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editButton.hide(); saveButton.show(); if(saveButton.isHidden()) { saveButton.show(); } } });

vbaidak commented 9 years ago

Hi kenny0202,

Excellent! This is a kind of an issue.

The problem here is that hide() method checks whether the action button is shown before hiding it by calling the inherited isShown() method of the android.view.View class. However, calling this isShown() method from the onCreate() method returns false even if your button is shown (this is how actually isShown method of the View class works). That is why hide() method does not actually do what you want in the onCreate() method.

As a temporary solution you have two options:

  1. Call setVisibility(View.INVISIBLE) for your saveButton in the onCreate() method:

    ActionButton editButton = (ActionButton) findViewById(R.id.edit_button);
    ActionButton saveButton = (ActionButton) findViewById(R.id.save_button);
    editButton.show();
    saveButton.setVisibility(View.INVISIBLE); 
  2. Leave the instantiation of the action buttons in the onCreate() method, but call hide() method for your saveButton in the onStart() method:

    private ActionButton editButton;
    private ActionButton saveButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    editButton = (ActionButton) findViewById(R.id.edit_button);
    saveButton = (ActionButton) findViewById(R.id.save_button);
    
    editButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               editButton.hide();
               saveButton.show();
           }
       });
    }
    
    @Override
    protected void onStart() {
       super.onStart();
       saveButton.hide();
    }

I would suggest you to use the second one.

I will fix this issue in version 1.0.4 today-tomorrow and you can simply move this part of the code into onCreate() method.

So, check for updates soon Have a nice day :)

kenny0202 commented 9 years ago

sweet thank you. Excellent work on the library by the way.

vbaidak commented 9 years ago

Thank you!

BTW, I forgot the 3rd option you can also use:

If you just need to hide this button at the start of your activity and then to control its visibility at runtime, you can just set the visibility=invisible for your saveButton in the XML layout resource:

android:visibility="invisible"
kenny0202 commented 9 years ago

ahh yes. I just noticed that seems like overriding the onStart method does not work.

vbaidak commented 9 years ago

Hi,

Issue was fixed in version 1.0.4

Please check