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: 2,046
  • AndroidResourcesDemo.zip
    28 KB · Views: 1,370
  • AndroidResources_v1_60.zip
    5.2 KB · Views: 1,957
Last edited:

warwound

Expert
Licensed User
Longtime User
AndroidResources updated to version 1.1

This update adds a single new method:

GetAndroidDrawableNames As List

It returns a List of Strings - the names of the device's built in Android Drawables.

Version 1.1 is attached to the first post in this thread.

Martin.
 
Last edited:

Petrus

Member
Licensed User
Longtime User
Thank you for the library - which has helped me with my project much!:sign0098:

regards
Petrus
 

warwound

Expert
Licensed User
Longtime User
AndroidResources updated to version 1.2

I've added a new method:

GetApplicationString (ResourceName As String) As String.

You can define resource strings using XML and add them to your project's Objects/res/values folder.
Make the XML file read-only!

Example XML:

B4X:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My B4A App</string>
    <string name="menu_title">Menu title</string>
</resources>

Example B4A code:

B4X:
'Activity module
Sub Process_Globals
   '   AndroidResource can be delared as a Process_Global - it is not an Activity object
   Dim AndroidResources1 As AndroidResources
End Sub

Sub Globals
   Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
   
   Dim Text As StringBuilder
   Text.Initialize
   
   Text.Append(AndroidResources1.GetApplicationString("app_name"))
   Text.Append(CRLF)
   Text.Append(AndroidResources1.GetApplicationString("menu_title"))
   
   Label1.Text=Text.ToString
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

It might not look useful BUT it makes localisation of your app possible: Localization | Android Developers

Updated library attached to the first post in this thread.

Martin.
 

warwound

Expert
Licensed User
Longtime User
AndroidResources updated to version 1.3

Here is a new method:

GetApplicationStrings (ResourceNames As Map) As Map

Get one or more application resource Strings.
Pass a Map to this method where each Map key is a String resource name.
The method will return the same Map object where the Map values are the resource Strings defined by the key.
If a resource String is not found the Map value will not be changed.

An example will make it clearer (look at the previous post to see the XML):

B4X:
'Activity module
Sub Process_Globals
   '   AndroidResource can be delared as a Process_Global - it is not an Activity object
   Dim AndroidResources1 As AndroidResources
End Sub

Sub Globals
   Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
   
   Dim ResourceStrings As Map
   ResourceStrings.Initialize
   ResourceStrings.Put("app_name", Null)
   ResourceStrings.Put("menu_title", Null)
   ResourceStrings.Put("no_such_resource", "no_such_resource")
   
   ResourceStrings=AndroidResources1.GetApplicationStrings(ResourceStrings)
   
   Dim Text As StringBuilder
   Text.Initialize
   
   Text.Append(ResourceStrings.Get("app_name"))
   Text.Append(CRLF)
   Text.Append(ResourceStrings.Get("menu_title"))
   Text.Append(CRLF)
   Text.Append(ResourceStrings.Get("no_such_resource"))
   
   Label1.Text=Text.ToString
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

So you can more efficiently get a batch of resource Strings with this method.

Updated library attached to the first post in this thread.

Martin.
 

warwound

Expert
Licensed User
Longtime User
AndroidResources updated to version 1.4

Following on from the previous posts today i have added some more methods to the library.

See the first post in this thread for syntax reference and the new version.

Martin.
 

moster67

Expert
Licensed User
Longtime User
I just discovered this library. This will surely be useful.

Many thanks for your amazing libraries and your support!

:sign0098:
 

timwil

Active Member
Licensed User
Longtime User
AndroidResources updated to version 1.4

Following on from the previous posts today i have added some more methods to the library.

See the first post in this thread for syntax reference and the new version.

Martin.

Holy Cow!!!! Dont you sleep? :)
 

keirS

Well-Known Member
Licensed User
Longtime User
Just been playing with this library. Very useful. The drawable returned is a NinePatchDrawable and not a BitmapDrawable. So I a added a bit of code to get a ListView with all the graphics and their descriptions converting the NinePatch to a BitMap.

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
     Dim AndroidResources1 As AndroidResources
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   ListView1.Initialize("")
   ListView1.FastScrollEnabled=True
   
   
   Activity.AddView(ListView1, 0, 0, 100%x, 100%y)
   Dim AndroidDrawableNames As List
   AndroidDrawableNames= AndroidResources1.GetAndroidDrawableNames
   
   Dim i As Int
   For i=0 To AndroidDrawableNames.Size-1
       Dim Object1 As Object
      Dim b As Bitmap
       Dim Canvas1 As Canvas
       Dim DesRect As Rect
      b.InitializeMutable(60dip,60dip)
      Canvas1.Initialize2(b)
      DesRect.Initialize(0,0,59,59)
      If AndroidResources1.GetAndroidDrawable(AndroidDrawableNames.Get(i)) <> Null Then
         'Convert NinePatch To Bitmap
         Object1 = AndroidResources1.GetAndroidDrawable(AndroidDrawableNames.Get(i))
           Canvas1.DrawDrawable(Object1,DesRect)
         ListView1.AddTwoLinesAndBitmap(AndroidDrawableNames.Get(i),"",b)
      End If
   
   Next
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 

IanMc

Well-Known Member
Licensed User
Longtime User
Where are the actual .jar and .xml files for this library?
 

yonson

Active Member
Licensed User
Longtime User
marker_default.png doesn't show up on demo

Hi
thanks for this excellent library, I am having problems with it though, on the list of Android Drawable Names, the image contained in drawable-nodpi (marker_default.png) DOESN'T show up on the list.

I'm not sure if I'm misunderstanding, but should it not appear on that list? if not how to I access it?

It is showing up on the resources-nodpi folder so is being built ok, and is set to read only.

Many thanks
 
Top