best practices for handling UI events
Asked Answered
M

4

10

I have put the all the binding code for UI events on OnCreate(). It has made my OnCreate() huge.

Is there pattern around implementing UI events in android ? Can I add methods in View xml file and then I can put all the handler code somewhere else.

In a nutshell , I think I am asking how can I implement MVVM pattern with android app code ?

Meaningless answered 1/5, 2011 at 21:8 Comment(3)
If you guys think MVVM pattern and what i asking is 2 differnt thing then let me know i will update my question.Meaningless
+1; This is more like a Java/event-driven programming question. Good luck with your quest! :)Jelena
MVVM framework: code.google.com/p/android-bindingAudacity
C
5

Stuff that I do:

  1. Keep all onClick functions in the XML. Avoids a lot of clutter in the Java code.
  2. Initialize event listeners as members of the activity class rather than keeping them in a function. I don't like too many curly braces in my code. Confuses the hell out of me.
  3. If my list adapters get too big I keep them in a separate class rather than as a member of the activity class and then keep all view listeners there in the adapter.
  4. To avoid creating too many onClick functions I sometimes keep one function like onNavigatonClick and then use view.getId() to see which button was clicked. As the XML is not checked for valid function calls, it leads to runtime errors if your function name is wrong.
  5. If a particular view needs a lot of UI interaction code, I create a custom view with a GestureDetector to handle UI interactions.

I guess this is still quite basic as I haven't had much experience with Java yet.

Celebes answered 1/5, 2011 at 21:27 Comment(0)
F
5

In 1.6 and later you can specify onClick methods in your layout XML file to trim a bit of the fat. I generally just hide it all away in a initUi() method that I have my onCreate method call. This way at least the onCreate is easier to read.

Fabrication answered 1/5, 2011 at 21:15 Comment(1)
you can find the code on the following link stackoverflow.com/questions/5848207/binding-events-on-oncreateMeaningless
C
5

Stuff that I do:

  1. Keep all onClick functions in the XML. Avoids a lot of clutter in the Java code.
  2. Initialize event listeners as members of the activity class rather than keeping them in a function. I don't like too many curly braces in my code. Confuses the hell out of me.
  3. If my list adapters get too big I keep them in a separate class rather than as a member of the activity class and then keep all view listeners there in the adapter.
  4. To avoid creating too many onClick functions I sometimes keep one function like onNavigatonClick and then use view.getId() to see which button was clicked. As the XML is not checked for valid function calls, it leads to runtime errors if your function name is wrong.
  5. If a particular view needs a lot of UI interaction code, I create a custom view with a GestureDetector to handle UI interactions.

I guess this is still quite basic as I haven't had much experience with Java yet.

Celebes answered 1/5, 2011 at 21:27 Comment(0)
T
1

Lots of good answers to this already. :)

If you're using Android 1.6 or later you might find the new fragments API helpful for organizing and partitioning your activities into several logical units.

Threepence answered 1/5, 2011 at 21:58 Comment(0)
E
0

onCreate is usually the best place for calling setContentView and setting up listeners, but the code for handling the user interractions normally goes in onClick, onTouch, onKey etc. routines.

Maybe if you posted your code we could see what you've done?

Epsilon answered 1/5, 2011 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.