Recent

Author Topic: Android Module Wizard : Try to get string from strings.xml  (Read 7226 times)

thierrydijoux

  • New Member
  • *
  • Posts: 21
Android Module Wizard : Try to get string from strings.xml
« on: October 19, 2014, 09:52:08 am »
Hi,

i'm trying to get string from strings.xml.
in And_jni_bridge.pas i added this code :

Code: [Select]
function  jSysInfo_GetResString        (env:PJNIEnv;this:jobject;context : jObject; AResName: string): string;
Const
 _cFuncName = 'getResString';
 _cFuncSig  = '(Landroid/content/Context;Ljava/lang/String;)Ljava/lang/String;';
Var
 _jMethod : jMethodID = nil;
 _jParams : Array[0..1] of jValue;
 _jString : jString;
 _jBoolean: jBoolean;
begin
 jClassMethod(_cFuncName,_cFuncSig,env,gjClass,_jMethod);
 _jParams[0].l := context;
 _jParams[1].l := env^.NewStringUTF(env, pchar(AResName) );
 _jString      := env^.CallObjectMethodA(env,this,_jMethod,@_jParams);
 env^.DeleteLocalRef(env,_jParams[1].l);
 Case _jString = nil of
  True : Result    := '';
  False: begin
          _jBoolean := JNI_False;
          Result    := String( env^.GetStringUTFChars(Env,_jString,@_jBoolean) );
         end;
 end;
end;

in AndroidWidget.pas i added

Code: [Select]
function jApp.GetResString(AResName: string): string;
begin
  Result:= jSysInfo_GetResString(Self.Jni.jEnv, Self.Jni.jThis, Self.Jni.jActivity, PChar(AResName));
end;

in Controls.java i added this code

Code: [Select]
public String getResString(android.content.Context context, String resName) {

int id = context.getResources().getIdentifier(resName, "string", context.getPackageName());
    String value = id == 0 ? "" : (String) context.getResources().getText(id);

// int id = getApplicationContext().getResources().getIdentifier("hello_world", "string", getApplicationContext().getPackageName());
// int id = getResources().getIdentifier("hello_world", "id", getPackageName());
// String value = id == 0 ? "" : (String) getApplicationContext().getResources().getString(id);
 
return value;
}   

When i call jApp.GetResString(AResName: string): string; from a jbutton, app crash.
if i define a public variable in jApp Class in the init procedure like that, i works.

Code: [Select]
TestRes:=jSysInfo_GetResString(env, this, activity, PChar('rxml'));

How can i access the resource string dynamicly from the module (in my button) ?

ps : sorry for my english

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #1 on: October 19, 2014, 01:53:58 pm »
Hi,

Quote
...When i call jApp.GetResString(AResName: string) ....

How?

TestRes:= jApp.GetResString.... //error!

or

TestRes:= gApp.GetResString.... //ok!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

thierrydijoux

  • New Member
  • *
  • Posts: 21
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #2 on: October 19, 2014, 01:56:23 pm »
getting TestRes is working (when it's placed into app.init)
when calling function jApp.GetResString(AResName: string): string; app crash

thierrydijoux

  • New Member
  • *
  • Posts: 21
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #3 on: October 19, 2014, 02:01:15 pm »
It's the same when you try to call GetAppPath (from app class).
Create a public function which calls   jSysInfo_PathApp(Self.Jni.jEnv, Self.Jni.jThis, Self.Jni.jActivity, PChar(FAppName){gjAppName}); and it also crash

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #4 on: October 19, 2014, 02:05:38 pm »
Ok,

Please, send me the unit1.pas...
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

thierrydijoux

  • New Member
  • *
  • Posts: 21
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #5 on: October 19, 2014, 02:14:46 pm »
The project is attached.

I joined the And_jni_Bridge.pas and androidwidget.pas.
The java file is also modified (in d2\src\fr\test\tdi\d2)

Try to find getResString.

Thanks

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #6 on: October 19, 2014, 02:38:58 pm »
Please, try this:  "jSysInfo_GetResString2"

Code: [Select]
function  jSysInfo_GetResString2(env:PJNIEnv;this:jobject;context : jObject; AResName: string): string;
var
 
  cls: jClass;
 _jMethod: jmethodID;
 _jParams : Array[0..1] of jValue;
 _jString : jString;
 _jBoolean: jBoolean;
begin
  cls := env^.GetObjectClass(env, this);
  _jMethod:= env^.GetMethodID(env, cls, 'getResString', '(Landroid/content/Context;Ljava/lang/String;)Ljava/lang/String;');
 _jParams[0].l := context;
 _jParams[1].l := env^.NewStringUTF(env, pchar(AResName) );
 _jString      := env^.CallObjectMethodA(env,this,_jMethod,@_jParams);
 env^.DeleteLocalRef(env,_jParams[1].l);
 Case _jString = nil of
  True : Result    := '';
  False: begin
          _jBoolean := JNI_False;
          Result    := String( env^.GetStringUTFChars(Env,_jString,@_jBoolean) );
         end;
 end;
end;
« Last Edit: October 19, 2014, 02:41:11 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

thierrydijoux

  • New Member
  • *
  • Posts: 21
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #7 on: October 19, 2014, 02:53:21 pm »
Thanks,

from jbutton1 :
shomessag(App.TestRes); OK

ShowMessage(App.GetResString('rxml')); KO

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #8 on: October 19, 2014, 03:35:20 pm »
Ok,

In many implementations in "And_jni_Bridge.pas"  you can see a new [re-]implementation indicated by "2"

I needed do that for support to Android API > 13 ....

You can follow this pattern!

Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

thierrydijoux

  • New Member
  • *
  • Posts: 21
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #9 on: October 19, 2014, 05:30:48 pm »
Thanks,

i tried using you instructions, but it looks like it only work when you call the function in the jApp.Init procedure.
I must find a workaround to get those string values and have the multilanguage android way...

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #10 on: October 19, 2014, 07:24:32 pm »
Please, Try this:

Code: [Select]
function jApp.GetResString2(AResName: string): string;
begin
  Result:= jSysInfo_GetResString(Self.Jni.jEnv, Self.Jni.jThis, gApp.GetContext, PChar(AResName));
end;


Another [preferable] solution:

in "Controls.java"
Code: [Select]
public String getResString(String resName) {
    Context context = this.activity;
    int id = context.getResources().getIdentifier(resName, "string", context.getPackageName());
    String value = id == 0 ? "" : (String) context.getResources().getText(id);
    return value;
}

in "And_jni_Bridge.pas"
Code: [Select]
function  jSysInfo_GetResString(env:PJNIEnv;this:jobject;context : jObject; AResName: string): string;
var
  cls: jClass;
 _jMethod: jmethodID;
 _jParams : Array[0..0] of jValue;
 _jString : jString;
 _jBoolean: jBoolean;
begin
  cls := env^.GetObjectClass(env, this);
  _jMethod:= env^.GetMethodID(env, cls, 'getResString', '(Ljava/lang/String;)Ljava/lang/String;');
 _jParams[0].l := env^.NewStringUTF(env, pchar(AResName) );
 _jString      := env^.CallObjectMethodA(env,this,_jMethod,@_jParams);
 env^.DeleteLocalRef(env,_jParams[0].l);
 Case _jString = nil of
  True : Result    := '';
  False: begin
          _jBoolean := JNI_False;
          Result    := String( env^.GetStringUTFChars(Env,_jString,@_jBoolean) );
         end;
 end;
« Last Edit: October 19, 2014, 08:02:06 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

thierrydijoux

  • New Member
  • *
  • Posts: 21
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #11 on: October 20, 2014, 06:26:50 pm »
Hi jmpessoa,

it works ! The faulting code was the java one. I just modify the code with your in controls.java and the job was done :-)
Now, the same way, we can access other resources !

Thanks for your help !

Just a question, do you mind if i fork the previous android module wizard (before the new form designer) ?

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #12 on: October 20, 2014, 06:48:31 pm »
Yes, i can fork it!

But it is quite outdated!

The "form designer"  release has many improvements [and fix!] that are not there!

In any case, thanks for your interest!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

thierrydijoux

  • New Member
  • *
  • Posts: 21
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #13 on: October 20, 2014, 06:51:16 pm »
ok, so the best to do is maybe send you some new code to include in the "official AMW"

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Android Module Wizard : Try to get string from strings.xml
« Reply #14 on: October 20, 2014, 06:55:52 pm »
Yes, send it for me!

His contributions will be always welcome and utilized!

Thank you!

if you need: jmpessoa_hotmail_com

PS. You can see there!
Code: [Select]
//thanks to  thierrydijoux
function jApp.GetStringResourceId(_resName: string): integer;
begin
   Result:= jApp_GetStringResourceId(Self.Jni.jEnv, Self.Jni.jThis, PChar(_resName));
end;

function  jApp.GetStringResourceById(_resId: integer): string;
begin
  Result:= jApp_GetStringResourceById(Self.Jni.jEnv, Self.Jni.jThis, _resId);
end; 
« Last Edit: October 20, 2014, 10:58:16 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

 

TinyPortal © 2005-2018