Custom Control - ThresholdEditText

Quickly share your Android Code Snippets here...

Custom Control - ThresholdEditText

Postby odedo » Tue May 11, 2010 9:49 pm

I developed a custom view for my own use, and decided to share the community.

The view is extending the "standard" EditText control by adding a new event: OnThresholdTextChanged

This lets you start a long running process (like a search process) after the user has finished entering some input in the EditText view without the need to add another "Submit" button and handle its "Clicked" event.

The project is hosted on Google Code, so you may download the relevant two classes, or an example project that demonstrates this functionality in a very intuative manner.


Any comment on my coding practices will also be highly appriciated (as I am coming from the .NET world and pretty new to Java syntax & methodologies).


Link:
http://code.google.com/p/android-threshold-edittext


Hope you will like it!

Oded O.
odedo
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue May 11, 2010 9:37 pm

Top

Postby odedo » Wed May 12, 2010 7:10 pm

Here is a video demonstrating the view.
The application in the video is the sample application, downloadable from google code (the url attached to the first post)

http://www.youtube.com/watch?v=udIRwgBIi4Q

Regards,

Oded O.
Last edited by odedo on Thu May 13, 2010 8:07 pm, edited 1 time in total.
odedo
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue May 11, 2010 9:37 pm

Postby dmalis » Wed May 12, 2010 11:46 pm

Awesome - thanks for that man!
dmalis
Junior Developer
Junior Developer
 
Posts: 18
Joined: Tue May 11, 2010 7:34 am

Postby pskink » Thu May 13, 2010 6:40 am

pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby odedo » Thu May 13, 2010 9:08 am

Is it possible to initiate a network call, or a search query on local database instead of binding to a predefined ArrayAdapter?

lets say I want to implement a search engine for stocks (that was the original purpose of this control in my project), it's impossible for me to create a local collection of all available symbols and stock names.

I use this control to initiate a network query using the entered string, and get the top 15 relevant stocks.

Regards,

Oded O.
odedo
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue May 11, 2010 9:37 pm

Postby pskink » Thu May 13, 2010 9:16 am

odedo wrote:Is it possible to initiate a network call, or a search query on local database instead of binding to a predefined ArrayAdapter?


you can use any adapter that implements Filterable interface
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top

Postby odedo » Thu May 13, 2010 9:55 am

First off... it's good to know.

But the difference here, is that I don't want to extend the EditText with a drop-down of available values...

I want to populate a LinearLayout (located below that edittext view) with the search results (collection of non-primitive types)
Each result item is actually another custom view that displays the stock symbol, index and full name, and the user can scroll down the list and select/click on that view to open another activity.

I'll try to upload a screenshot/screencast of what I mean later today...


Oded O.
odedo
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue May 11, 2010 9:37 pm

Postby pskink » Thu May 13, 2010 10:06 am

anyway: don't use Timer in your View implementation - one Timer == one Thread

use Handler instead - send[Empty]MessageDelayed()
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby odedo » Thu May 13, 2010 12:40 pm

The Implementation is as follows:

Whenever the text in the EditText chenges, I start a timer to execute a method (the listener) in X milliseconds (threshold).
If the user continues to modify the content of the EditText before the timer was elapsed, I need to reset the timer and create a new timer with the original threshold provided...

so, at any given point, there is only one thread in addition to the UI thread.

in C#/.NET, what I would have done, is to add a background thread (or timer) sampeling a variable holding the next expected threshold timeout, and in the OnTextChanged event listener, I would update that variable to DateTime.Now.AddMilliseconds(threshold)... but this solution requires an additional thread as well.


How can it be done using Handlers?
odedo
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue May 11, 2010 9:37 pm

Postby pskink » Thu May 13, 2010 1:02 pm

odedo wrote:The Implementation is as follows:

Whenever the text in the EditText chenges, I start a timer to execute a method (the listener) in X milliseconds (threshold).
If the user continues to modify the content of the EditText before the timer was elapsed, I need to reset the timer and create a new timer with the original threshold provided...

so, at any given point, there is only one thread in addition to the UI thread.

How can it be done using Handlers?


suppose i have, say 10 your views on one screen - how many threads will be created?

ten

when using Handlers create an instance:

Handler mHandler = new Handler {
}

and ovveride its handleMessage method

then you can:

mHandler.sendEmptyMessageDelayed - sends empty message that will be delivered after some delay

mHandler.removeMessages - removes pending messages
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby odedo » Thu May 13, 2010 1:25 pm

Superb.

I'll give it a try, and update the project on google-code.

Thank you for the great input!


Oded O.
odedo
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue May 11, 2010 9:37 pm

Postby odedo » Thu May 13, 2010 7:51 pm

Hi all,

It took me some time to figure out how to use the handler's delayed messages (there are few examples online)
But I finally managed to update the project and I've uploaded it to google code.

pskink - thanks again!


Regards,

Oded O.
odedo
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue May 11, 2010 9:37 pm

Postby pskink » Thu May 13, 2010 8:10 pm

odedo wrote:Hi all,

It took me some time to figure out how to use the handler's delayed messages (there are few examples online)
But I finally managed to update the project and I've uploaded it to google code.
Oded O.


good, although when using Handlers i prefer not to use Runnables: if you have several actions to execute you need to create several inner anonymous classes implementing Runnable - i prefer using raw Messages which can have optional parameters - just ovveride handleMessage in custom Handler

but in your case there is only one action so Runnable is OK
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby odedo » Thu May 13, 2010 8:20 pm

I'll keep that in mind.

And as I promised, here is the screencast of the original usage of this view: stocks searching
http://www.youtube.com/watch?v=H9cYe6PVPHI
I guess this clarifies the need for this type of event/view.

Hope to keep on contributing.

Oded O.
odedo
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue May 11, 2010 9:37 pm

Postby pskink » Fri May 14, 2010 6:57 am

odedo wrote:I'll keep that in mind.

I guess this clarifies the need for this type of event/view.

Hope to keep on contributing.

Oded O.


one more thing: make it possible to specify all your properties (treshold etc) in layout xml file so if for example i want treshold not to be default one i dont need to call setTreshold() method in my code but rather it be called in your view ctor
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top
Next

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 6 guests