Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best practices for handling UI events

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 ?

like image 644
Gainster Avatar asked May 01 '11 21:05

Gainster


People also ask

How are GUI events handled?

How Events are handled? A source generates an Event and send it to one or more listeners registered with the source. Once event is received by the listener, they process the event and then return.

How do we handle events in Android?

You will use onClick() event handler to handle such event. This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. for one or more seconds. You will use onLongClick() event handler to handle such event.

How events are handled?

Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs. Java Uses the Delegation Event Model to handle the events.

How will you handle the events in Android explain with an example?

For Example, if a button is to reply to a click event it must register to look at the . onClickListener event listener and implement the corresponding onClick() callback method. In an application when a button click event is detected, the Android framework will call the onClick() method of that particular view.


2 Answers

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.

like image 60
Abhinav Avatar answered Sep 30 '22 03:09

Abhinav


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.

like image 36
Dan Avatar answered Sep 30 '22 03:09

Dan