1

In Android, is there a way to bind a screen element's visibility to a boolean property within an activity? I've done a lot of work with Backbone and Ember where this was easily doable. I'm following the standard methodology of toggling button states based on applicable responses from the server, but my code feels overly verbose and clumsy compared to what I was able to achieve with web frameworks.

Is there a nice way for a view to watch a property, and have it update its visibility based on the value of that property?

Basically I would like to have my API calls update a property in my activity (or an associated data model), and have the visibility logic just flow automatically, without manually toggling the visibility of elements myself.

Community
  • 1
  • 1
Gopherkhan
  • 4,317
  • 4
  • 32
  • 54

2 Answers2

1

So it looks like I would want to use something like The Android Binding library. Here's the github project.

Gopherkhan
  • 4,317
  • 4
  • 32
  • 54
0

In fact i'm not quite sure what you want to achieve, since the code for toggling visibility is already very simple, like this.

boolean isShow;
View mView;

// after you changed isShow, you should also update the view states.
mView.setVisibility(isShow ? View.VISIBLE : View.GONE);

And since you are trying to do this after a remove request, and code that update UI cannot be put in any other threads except the main thread. You can achieve this with a Handler or AsyncTask. AsyncTask is better for your problem.

faylon
  • 7,360
  • 1
  • 30
  • 28
  • 1
    Admittedly, this might not be the best example, but I'm thinking of something like this: http://emberjs.com/api/classes/Ember.Binding.html Essentially, you specify the property you want to watch, and the framework updates the ui as that value changes. You don't have to explicitly toggle anything yourself, because it's all done for you by the framework. See: http://www.techopedia.com/definition/15652/data-binding Basically it keeps your code cleaner, and frees you to focus on the logic of an operation rather than the presentation of the result. – Gopherkhan Sep 04 '13 at 18:52