Android: Renaming gui elements vs. new layout
I'm new into android and i'm dealing with the following problem. I need to create a button which groups another two buttons of similar features (sort of submenu). So let's say we have a button called "search", by clicking on it the search button should disappear and the two buttons (e.g. "google" and "bing") should be seen on the screen.
So, my idea was to manage all three buttons programmatically in the same layout instead of creating a new (temp)layout just to show and handle the two buttons. Like a state machine. More precisely it would be like this:
We press on the "search" button. onClick(View) determines wheter we are on the main screen (mode=mainmenu), renames the search button to "google" and creates the second button named "bing", or (mode=submenu) then call the function of the button due to the search button is already renamed to "google"...
By pressing the back button onBackPressed() checks开发者_运维知识库 if we are in the submenu (mode=submenu) then hide the "bing" button and rename "google" to "search", otherwise we are in the main menu (mode=mainmenu), finish the activity.
Does this make sense at all? Besides this will produce lots of code with rising count of buttons and (sub)menus and worse maintainability for further changes.
Thanks!
P.S. Sorry if my english sucks (not my native language) ;-)
That's too much code and too much debugging will be needed, so I think, a better way is just creating a new layout.
Well if you have performance issues or expecting performance issues can arise (due to complexity of you GUI) then may be this make sense. But if not, code simplicity is more important than slight increase in performance.
You can create an button that remain invisible(gone, to be exact) until the "search" button is pressed, and rename the original search button. It works, and it will be easier to implement if you set up layout in xml. It also give you a preview that shows what it looks like. To show widget programmatically will have code that hard to maintain.
However, the design that change button text is not a good design. First, when the "search" is pressed, changing its text is very confusing. I personally suggest you to pop-out a selection dialog. Or just show both search button at first place, unless you really do not have place for two button.
精彩评论