B4A Library AndroidResources

There's a few threads on the forums about accessing the build in Android resources but i couldn't find any code that would allow me to get a built in Android resource drawable using the resource's name - all posted code required that you already know the resource id.

So after a bit of research i created this method:



The documentation for the android.R.drawable class can be found here: R.drawable | Android Developers

Another thing i've been trying to do recently is simply load an icon on a device with high density display but not have the Android OS automatically upscale the icon to a larger size and leave me with a pixelated poor quality icon.
I don't want the icon upscaled...

A solution is to add the icon to my project's Objects\res\drawable-nodpi - creating that folder if it doesn't exist.
But now how can i access that icon?

A new method derived from Barx's recent post here:



Drawables in the drawable-nodpi folder are NOT rescaled on low or high density devices.

While researching all of this i found that the android.R.string class contains localised versions of commonly used application text such as language versions of 'paste' and 'cancel'.
Another new method:

AndroidResources
Version:
1.6
  • AndroidResources
    Methods:
    • GetAndroidDrawable (ResourceName As String) As Drawable
      Get an android resource drawable by ResourceName.
      Returns Null if the Drawable is not found.
    • GetAndroidDrawableNames As List
      Get a List of all available android Drawable resource names.
    • GetAndroidDrawables (ResourceNames As Map) As Map
      Get one or more android resource Drawables.
      Pass a Map to this method where each Map key is an android Drawable resource name.
      The method will return the same Map object where the Map values are the android resource Drawables defined by the Map keys.
      If an android resource Drawable is not found the Map value will not be changed.
    • GetAndroidString (ResourceName As String) As String
      Get an android resource string by ResourceName.
      Returns Null if the String is not found.
    • GetAndroidStringNames As List
      Get a List of all available android String resource names.
    • GetAndroidStrings (ResourceNames As Map) As Map
      Get one or more android resource Strings.
      Pass a Map to this method where each Map key is an android String resource name.
      The method will return the same Map object where the Map values are the android resource Strings defined by the Map keys.
      If an android String is not found the Map value will not be changed.
    • GetApplicationDrawable (ResourceName As String) As Drawable
      Get an application resource Drawable by ResourceName.
      Returns Null if the Drawable is not found.
    • GetApplicationDrawables (ResourceNames As Map) As Map
      Get one or more application resource Drawables.
      Pass a Map to this method where each Map key is an application Drawable resource name.
      The method will return the same Map object where the Map values are the application resource Drawables defined by the Map keys.
      If an application resource Drawable is not found the Map value will not be changed.
    • GetApplicationRawResource (ResourceName As String) As InputStreamWrapper
      Get an application raw resource as an InputStream.
      The InputStream will not be initialized if the raw resource cannot be found.
    • GetApplicationResourceNames (ResourceType As String) As List
      Returns a List of Strings which are the application resource names for the ResourceType.
      ResourceType must be one of the android R class nested class names.
      ResourceType values such as color, drawable, integer, layout and string are valid.
      All nested R class classes are documented here:
      http://developer.android.com/reference/android/R.html
    • GetApplicationString (ResourceName As String) As String
      Get an application resource String by ResourceName.
      Returns Null if the String is not found.
    • GetApplicationStrings (ResourceNames As Map) As Map
      Get one or more application resource Strings.
      Pass a Map to this method where each Map key is an application String resource name.
      The method will return the same Map object where the Map values are the application resource Strings defined by the Map keys.
      If an application resource String is not found the Map value will not be changed.

The String returned by this new method will be in the language that the device has selected in Settings > Language & keyboard settings.
The documentation for the android.R.string class can be found here: R.string | Android Developers.

So i've created the library AndroidResources for everyone to use and if any library writers want to use any of these methods in their libraries then please do!



For some reason the forum would not accept the code snippets so check out the attached AndroidResources.txt file for the complete post!

Martin.
 

Attachments

  • AndroidResources.txt
    3 KB · Views: 1,948
  • AndroidResourcesDemo.zip
    28 KB · Views: 1,275
  • AndroidResources_v1_60.zip
    5.2 KB · Views: 1,864
Last edited:

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I got tired of looking these up when I needed them, so for those of you who want to use the Phone library's GetResourceDrawable method to retrieve a BitmapDrawable, here is a mnemonic constant list compiled from the Resource Drawable developer page cited in the first post.
 

Attachments

  • RD.bas
    10.7 KB · Views: 257

phukol

Active Member
Licensed User
Longtime User
Good day everyone. I want to change the color used in my CustomerListView. Its using the list_selector_background which is used in gingerbread and lower versions. I want to access the attribute "list_pressed_holo_dark" using Android Resources. How can i do this? The code is:
B4X:
idPressed = r.GetStaticField("android.R$drawable",  "list_selector_background")

i want it to use "list_pressed_holo_dark" but it will not work even if i type it directly and ive read i can access it using AndroidResources library
 

DonManfred

Expert
Licensed User
Longtime User
Any examples for using Formatting strings with parameters ?

Use GetAndroidString and do the formatting by yourself (replace %1 with value a, %2 with value b....
  • GetAndroidString (ResourceName As String) As String
    Get an android resource string by ResourceName.
    Returns Null if the String is not found.
 

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
Try this:

B4X:
'RESOURCES

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals name="message">
        <item quantity="one">You have one new message</item>
        <item quantity="other">You have %d new messages</item>
    </plurals>
</resources>

'JAVA

#if Java

public int getResourceId(String type, String name)
    {
        return BA.applicationContext.getResources().getIdentifier(name,    type, BA.packageName);
    }

    public String FormatPlural(int id, int count) {

        return BA.applicationContext.getResources().getQuantityString(id,  count, count);
    }

#End If

'B4A
Sub Activity_Create(FirstTime As Boolean)

   Private NativeMe As JavaObject
   NativeMe.InitializeContext
   Dim id As Int = NativeMe.RunMethod("getResourceId", Array("plurals","message"))
 
   Dim s As String = NativeMe.RunMethod("FormatPlural", Array(id,1))
   Log(s)

   Dim s As String = NativeMe.RunMethod("FormatPlural", Array(id,2))
   Log(s)

End Sub

'RESULTS
'You have one new message
'You have 2 new messages
 
Last edited:

Laurent95

Active Member
Licensed User
Longtime User
Hello,

I know it's an old thread and an old library.
But still working even on B4A v10.60 !
I used it in an old project, but today when i tried to adapt it with xChart lib from Klaus i have an issue.
the example, if i use:
xChart1.YAxisName = AndroidResources3.GetApplicationString("chart_yAxis")
xChart1.XAxisName = AndroidResources3.GetApplicationString("chart_xAxis")
If ever there is a typo or an xml file isn't updated (outdated) it returns Null and give an error with other libraries.
It's possible to change that for an empty string ?

Thank you,
Laurent
 

DonManfred

Expert
Licensed User
Longtime User
xChart1.YAxisName = AndroidResources3.GetApplicationString("chart_yAxis")
B4X:
if AndroidResources3.GetApplicationString("chart_yAxis") <> null then
    xChart1.YAxisName = AndroidResources3.GetApplicationString("chart_yAxis")
end if
?
 
Top