Java Question how to get id of xml file?

DonManfred

Expert
Licensed User
Longtime User
To set the defaults in the firebase remoteconfig we need to use for example a xml with the content...

Asume that the file is placed in res/xml/ and it is named remoteconfigdefaults.xml

it contains:
<?xml version="1.0" encoding="utf-8"?>
<!-- START xml_defaults -->
<defaultsMap>
<entry>
<key>price_prefix</key>
<value>Your price is $</value>
</entry>
<entry>
<key>loading_phrase</key>
<value>Checking your price…</value>
</entry>
<entry>
<key>is_promotion_on</key>
<value>false</value>
</entry>
<entry>
<key>price</key>
<value>100</value>
</entry>
<entry>
<key>discount</key>
<value>0</value>
</entry>
</defaultsMap>
<!-- END xml_defaults -->


How do i get the id of the xml to use it in the wrapper?

B4X:
    /**
     * Sets defaults in the default namespace,
     * using an XML resource.
     * @param resourceId
     */
    public void setDefaults (String resource){
         BA.Log("Calling SetDefaults... "+resource);
        int resourceId=BA.applicationContext.getResources().getIdentifier(resource, "string", BA.packageName);
         BA.Log("resourceID="+resourceId);
        getObject().setDefaults(resourceId);
    }

i tried it with
B4X:
 int resourceId=BA.applicationContext.getResources().getIdentifier(resource, "string", BA.packageName);
with
B4X:
 int resourceId=BA.applicationContext.getResources().getIdentifier(resource, "xml", BA.packageName);

and i tried it with remoteconfigdefaults and remoteconfigdefaults.xml as string in the methodcall.

All does not work and results in a resourceId of 0

How do i get the id of an xml resource? Where do i need to place the xml?
 

Johan Schoeman

Expert
Licensed User
Longtime User
To set the defaults in the firebase remoteconfig we need to use for example a xml with the content...

Asume that the file is placed in res/xml/ and it is named remoteconfigdefaults.xml

it contains:



How do i get the id of the xml to use it in the wrapper?

B4X:
    /**
     * Sets defaults in the default namespace,
     * using an XML resource.
     * @param resourceId
     */
    public void setDefaults (String resource){
         BA.Log("Calling SetDefaults... "+resource);
        int resourceId=BA.applicationContext.getResources().getIdentifier(resource, "string", BA.packageName);
         BA.Log("resourceID="+resourceId);
        getObject().setDefaults(resourceId);
    }

i tried it with
B4X:
 int resourceId=BA.applicationContext.getResources().getIdentifier(resource, "string", BA.packageName);
with
B4X:
 int resourceId=BA.applicationContext.getResources().getIdentifier(resource, "xml", BA.packageName);

and i tried it with remoteconfigdefaults and remoteconfigdefaults.xml as string in the methodcall.

All does not work and results in a resourceId of 0

How do i get the id of an xml resource? Where do i need to place the xml?
Just guessing here but try and change "string" to "values"
 

DonManfred

Expert
Licensed User
Longtime User
the content is not a string resource. the code above will work fine for strings in a strings.xml. But it must be RESOURCES

i dont think it will help to use values/string here as getIdentifier does expect a TYPE there. string, id, drawable works... But dont know how to get an id of an unknown xml.
 

Johan Schoeman

Expert
Licensed User
Longtime User

DonManfred

Expert
Licensed User
Longtime User
Never mind... I can use a Map to set the defaults... So i changed the method to use the map instead of an xml....

for now it is ok... i´ll check the other methods now...

If it works then i wrote my first wrap using an aar library ;-)

I´ve made a wrap for Firebase RemoteConfig...
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
All does not work and results in a resourceId of 0

How do i get the id of an xml resource? Where do i need to place the xml?
Were you able to get id of resource ?
I have a similar problem but I am trying to get drawable id .
resourceid is always 0
I changed "id" to "drawable" and got an int like 2130837505 but I always get android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
B4X:
int resourceId =ba.applicationContext.getResources().getIdentifier(imagename, "id", ba.applicationContext.getPackageName());
 

DonManfred

Expert
Licensed User
Longtime User
Were you able to get id of resource ?
No. As i wrote above i realized that i could use a map. I used a map then.


This is how i do it always if i need to get a drawabled from the resourcefolder.

B4X:
  public Drawable getDrawable(final BA ba, String stringResource) {
         int resourceId=BA.applicationContext.getResources().getIdentifier(stringResource, "drawable", BA.packageName);
         return BA.applicationContext.getResources().getDrawable(resourceId);
  }
in this case the drawable must be referenced by its file name without extension.

B4X:
#additionalres: ..\res
the drawable must be inside the folder res\drawable\

Hope it helps
 
Top