B4A Library Reflection library

There will be lots to come in future versions of Basic4Android as Erel adds more of Androids capabilities. In the mean time there will be things that would be nice to have but are not actually implemented. This library will help to solve at least some of those requirements.

This is the Android equivalent of the Basic4ppc Door library that turned out to be so useful. There is a demo for the library that adds onFocus and onTouch events to views that don't implement them, accesses objects fields and runs methods.

A separate demo shows how to "Linkify" text so that URLs in the text become active hyperlinks.

EDIT:- Version 1.1 posted. See post #17 for details.

EDIT:- Version 1.2 posted. Bug fix. See post#19 for details.

EDIT:- Version 1.3 posted. See post#20 for details.

EDIT:- Version 1.4 posted. See post#21 for details.

EDIT:- Version 1.5 posted. See post #25 for details.

EDIT:- Version 1.6 posted. See post #31 for details.

EDIT:- Version 1.7 posted. See post #32 for details.

EDIT:- Version 1.8 posted. See post #33 for details.

EDIT:- Version 1.9 posted. See post #48 for details.

EDIT:- Version 2.0 posted. See post #51 for details.

EDIT:- Version 2.1 posted. See post #53 for details.

EDIT:- Version 2.2 posted for Basic4android 2.0 or later. Version 2.1 remains for earlier versions. See post #57 for details.

EDIT:- Informatix has produced Version 2.3 in post #317

EDIT:- Version 2.4 posted. See post #91 for details.
 

Attachments

  • Reflection2.1.zip
    33.2 KB · Views: 1,902
  • Reflection2.4.zip
    33 KB · Views: 4,146
Last edited:

stevel05

Expert
Licensed User
Longtime User
Hi Andrew,

I would like to use the reflection library's OnKeyListener on an edittext with external keyboards, and would like to be able to access the KeyEvent object to check for ctrl/shift keys etc. I noticed that it is available when using the Proxy method, but also noticed your warning so have steered clear of that.

Is it possible to expose the KeyEvent from the library please? Either using reflection as it stands, or with a new method.

Thanks

Steve
 

agraham

Expert
Licensed User
Longtime User
Look closer at the demo. It uses SetOnKeyListener without problem (as far as I know) on EditText1 at line 24. It is only when trying to use a Proxy on EditText2 that I found I got an Exception. I added the Proxy to cope with "non-standard" SetOnXXXX methods but the base View "OnXXXX" ones were alreadt separately exposed.
 

stevel05

Expert
Licensed User
Longtime User
Thanks for the quick reply, yes the standard SetOnKeyListener works fine on an EditText, but the underlying KeyEvent is not available for querying for additional info, like isCtrlPressed() pressed etc.

The returning sub only has access to the keycode and viewtag.
 

agraham

Expert
Licensed User
Longtime User
Ah, right - missed that! You can't get the existing KeyEvent by Reflection as it is buried in an anonymous method. I have a couple of bug fixes and enhancements that I haven't posted so I'll add the KeyEvent object as an additional parameter to the called Basic4android OnKey event Sub. This will break any existing code but I don't think that will worry too many people.

While I'm at it I'll also add the MotionEvent object as a parameter to the Basic4android parameter list for the OnTouchListener.

I'll probably do it tomorrow unless I get fed up with what I'm playing with today.
 

stevel05

Expert
Licensed User
Longtime User
That's great, Thanks very much.

Steve
 

agraham

Expert
Licensed User
Longtime User
Version 1.9 now posted with a few changes it has accreted since version 1.8.

Added new shorthands for primitive type specification to save typing. In fact they are the same as the Java internal class names that are used for primitive types and arrays of primitive types.

Added an explanation of the syntax needed to reflect on arrays to the help.

Changed the exception handling of GetB4AClass because NoClassDefFoundError exception couldn't be trapped by Basic4android code Try..Catch blocks.

Corrected a bug in both SetStaticField and SetStaticField2.

Added a KeyEvent and a MotionEvent parameter to OnKey and OnTouch event Sub parameter lists. Note that OnKey events only work reliably for physical keyboards. For the more usual virtual keyboards the OnKey event Sub is called for some keys and not others. This apparently is a known Android limitation.
 

stevel05

Expert
Licensed User
Longtime User
Thank you very much and the shorthands are a nice touch, It always takes me two goes to get the string right :)
 

ChrShe

Member
Licensed User
Longtime User
Hi,
I'm building some different views that contain EditText boxes that could contain multiple types of things that need to be Linkified.

i.e. The box may contain a URL, Phone number, Street Address, Email address...OR any combination.

Any thoughts on how I can get a single EditText box to have multiple types of Links? I've tried various ways but haven't been able to make it happen.

Thanks!!!!
 

agraham

Expert
Licensed User
Longtime User
Version 2.0 now posted with modified SetOnKeyListener and SetOnLongClickListener methods.

Later versions of Android seem to raise an OnKey event twice, rather than once, when the Back key (and maybe other keys) is/are pressed. Unfortunately the second event arises when the Activity is closed and this caused a null pointer exception in the Reflection library code which is now detected and ignored in these two listeners.
 
Last edited:

AllanD

Member
Licensed User
Longtime User
Replection Library how to install it

Hello,
I downloaded the reflection library but do not know where to place the files and include them into my b4a app. I see references to the demo, but I am not sure where the link to the demo is?

Please help.:sign0085:

Thanks,
Allan
 

agraham

Expert
Licensed User
Longtime User
Version 2.1 now posted has a small change to RunMethod. Previously when searching for methods RunMethod bailed out before checking Object. It now checks Object and so can pick up getClass() which it couldn't before. The significance of this is that it is now possible to reflect on the Class instance of any object, including Basic4android Types.

If you need it the Class name of Type is

package.modulename$_typename

The demo which was inadvertently omitted in version 2.0 is included again.
 

agraham

Expert
Licensed User
Longtime User
Did you had a chance to test it with v2.00? BA class was refactored a bit to add support for classes.
No, is there something you think might have broken with the changes? The only access the library makes to BA is to BA.activityBA in GetActivity() and to BAprocessBA in GetProcessBA().
 

pbmi

Member
Licensed User
Longtime User
Hi Andrew,

Would it be possible to post an example of how to get a list or array of field info from a class using the new rReflection 2.2 / RunMethod / getClass capability.

(Sorry but I'm not familiar with Java reflection...)

I would like to get a list of field name, type and value - much like .Net reflection.

Many thanks
 

agraham

Expert
Licensed User
Longtime User
We are getting a bit deep into the voodo arts here. Here's an example reflecting on a Type and a Class. Class is more confusing that Type because the naming is different and there is extra stuff lying around.
 

Attachments

  • ClassReflection.zip
    6.3 KB · Views: 492
Top