Custom Control - ThresholdEditText

Quickly share your Android Code Snippets here...

Postby odedo » Fri May 14, 2010 3:14 pm

Hi pskink,

I've made it possible to change the "extra" properties from the layout xml, but I came across two major flaws:

1. even after adding the xmlns declaration to the layout.xml page, I do not have any intellisens/autocompletion of the new properties (I would expect to have the same "autocomplete" feature that the original EditText attributes has)

2. I found a post that describes the second issue perfectly: http://www.anddev.org/custom_xml_attrib ... t2830.html

I still have not committed those changes to the google code project due to these flaws... If there is a way to fix it, I will commit them.

these are the changes:

attr.xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <resources>
  5.  
  6.  
  7.  
  8.         <declare-styleable name="ThresholdEditText">
  9.  
  10.                 <attr name="Threshold" format="integer"/>
  11.  
  12.                 <attr name="DisableThresholdOnEmptyInput" format="boolean"/>
  13.  
  14.         </declare-styleable>
  15.  
  16.  
  17.  
  18. </resources>
  19.  
  20.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


ThreshildEditText.java (some more changes were made, that aren't here):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         /**
  3.  
  4.          * Load properties values from xml layout
  5.  
  6.          */
  7.  
  8.         private void initAttributes(AttributeSet attrs) {
  9.  
  10.                 if (attrs != null) {
  11.  
  12.                         // Get the styles attributes
  13.  
  14.                         TypedArray res = getContext().obtainStyledAttributes(attrs,R.styleable.ThresholdEditText);
  15.  
  16.                        
  17.  
  18.                         // Load values to local members
  19.  
  20.                         this.threshold = res.getInteger(R.styleable.ThresholdEditText_Threshold, 500);
  21.  
  22.                         this.disableThresholdOnEmptyInput = res.getBoolean(R.styleable.ThresholdEditText_DisableThresholdOnEmptyInput, true);
  23.  
  24.                 }
  25.  
  26.                 else {
  27.  
  28.                         // Default threshold value is 0.5 seconds
  29.  
  30.                         threshold = 500;
  31.  
  32.                        
  33.  
  34.                         // Default behaviour on emptied text - no threshold
  35.  
  36.                         disableThresholdOnEmptyInput = true;
  37.  
  38.                 }
  39.  
  40.         }
  41.  
  42.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


and the layout.xml:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
  4.         xmlns:ma="http://schemas.android.com/apk/res/com.MobileAnarchy.ThresholdEditText"
  5.  
  6.    android:orientation="vertical"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent"
  11.  
  12.    >
  13.  
  14.  
  15.  
  16.         <LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_margin="10dip">
  17.  
  18.                 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="10px" android:text="@string/UserInput"></TextView>
  19.  
  20.                 <com.MobileAnarchy.ThresholdEditText.ThresholdEditText
  21.  
  22.         android:layout_height="wrap_content"
  23.  
  24.         android:layout_width="fill_parent"
  25.  
  26.         android:id="@+id/EditTextInput"
  27.  
  28.         android:lines="1"
  29.  
  30.         ma:Threshold="500"
  31.  
  32.         ma:DisableThresholdOnEmptyInput="true" />
  33.  
  34.         </LinearLayout>
  35.  
  36.  
  37.  
  38. <!-- The rest of of the content was ommited -->
  39.  
  40.  
  41.  
  42.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4



Thanks again,

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

Top

Postby pskink » Fri May 14, 2010 4:20 pm

i faced the same problem but:

ad 1 - sorry but its ridiculous, so i'll skip this
ad 2 - make your attributes in attrs.xml lowercase (first letter only), just for compatibility, and:

namespace="http://schemas.android.com/apk/res/com.MobileAnarchy.ThresholdEditText"

attrs.getAttributeIntValue(namespace, "treshold", 500)
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby odedo » Fri May 14, 2010 5:17 pm

Sorry about the casing issues... as I said, I'm coming from C#, where PascalCasing is the way to go... anyway, I fixed it.

Why to you think that expecting attributes names to have autocompletion is ridiculous?
If I'm adding my namespace, and typing "ma:th..." I would expect the IDE to suggest the value "ma:threshold".

In WPF's XAML, once you add the namespace to the xml file, you have intellisense to both attributes names and their values (if applicable).

I made the changes you suggested, and commited to google code.

Thanks again, its been a great learning expiriecne.


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

Postby pskink » Fri May 14, 2010 5:32 pm

odedo wrote:Sorry about the casing issues... as I said, I'm coming from C#, where PascalCasing is the way to go... anyway, I fixed it.

Why to you think that expecting attributes names to have autocompletion is ridiculous?
If I'm adding my namespace, and typing "ma:th..." I would expect the IDE to suggest the value "ma:threshold".

In WPF's XAML, once you add the namespace to the xml file, you have intellisense to both attributes names and their values (if applicable).

I made the changes you suggested, and commited to google code.

Thanks again, its been a great learning expiriecne.


Oded O.


ok, maybe its not ridiculous but not so important

1. if someone is using your view as a jar file how ide is supposed to know your attributes?

2. ok maybe together with your jar someone still has to add your attrs.xml to his/her attrs.xml (i dont remember if its necessary) and maybe goggle will improve in the future their eclipse ADT plugin so it will reckognize your attributes

3. there are many ppl not using eclipse but netbeans for example

4. good work!!!
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Re: Custom Control - ThresholdEditText

Postby plusminus » Thu May 20, 2010 11:03 am

Hi odedo,

I'd flattr this ( announces/flattr-t14199.html )
But you don't have an account yet :P

Best Regards,
Nicolas
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top
Previous

Return to Code Snippets for Android

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests