1

Is it possible in Android to draw a widget in an Activity only if a boolean value equals true? Basically, I'd like to hide a Button if the currently logged in user isn't an administrator, but display if they are.

Is something like this possible? I haven't noticed a widget property that could provide the functionality I seek, but I'm hoping I'm overseeing something. A less desirable alternative would be to create an Intent to a completely separate administrator focused Activity, and load an administrator specific layout.xml, but that immediately creates an significant amount of duplicate code.

Display name
  • 1,109
  • 1
  • 15
  • 31

2 Answers2

1

Take a look at this question, it shows you how to add a button dynamically.

You can also take a look at Fragments. Fragments provide additional set of API to customize Activity by assembling fragments of UI and behavior. It might be a good choice if you decided to add whole lot of features to administrator. Using Fragments, your code will be cleaner, separating 'admin' UI components from the rest.

Community
  • 1
  • 1
sperumal
  • 1,499
  • 10
  • 14
1

I think what you're looking for is android:visibility

You can set the value either to visible (self-explanatory), invisible (not visible but still takes up space in the layout, or gone (it's not included in the layout at all).

Relevant links: http://developer.android.com/reference/android/view/View.html#setVisibility(int) http://developer.android.com/reference/android/view/View.html#attr_android:visibility

Junseok Lee
  • 705
  • 5
  • 18