Any ini file for Android?

Harris

Expert
Licensed User
Longtime User
Is there the notion of an "ini" file in Android? Or what method is uded to store app vars that can be read and writen to a structured file (other than SQL db).

Thanks

Mike Whetmore
 

Harris

Expert
Licensed User
Longtime User
Thank you. I shall research and try that.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Using a Map is the recommended way to store settings.
Three tips:
- You can use Map.GetDefault(...) instead of Map.Get(). This way you can fetch the saved value, and if the value doesn't exist because it is the first time, the default value will be returned.
- If you have several boolean settings you can use InputMap keyword to display a dialog with those items and the user can check or uncheck each item.
- File.ReadMap doesn't maintain the order of the original Map. Usually it doesn't matter. However if you are showing the Map with InputMap then the order is probably important for you. You can load data to an existing Map with File.ReadMap2. This way you can force the order of items (assuming that the map already included the keys in the file).
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
I am getting familar with map files. I have discovered the GetDefault and it works well.

I see lists that contain a green check mark for bools and wondered how that was accomplished - I shall try the Input map keyword. Any code examples that may shortcut my effort?

I did use scrollview to create a scroll list of my large graphical buttons. After removing the parent of the panel which contains my image and text, creatiing a sub to add and space them - it worked like a charm!

I did notice however that renaming a panel (Panel1 to Button1Pnl) seems to loose the event handler. The button did not work unit I renamed the panel back to the original name. I supposed I could just rename the event section to use panel1 (the original).

Anyways... Its all good...
 
Upvote 0

maXim

Active Member
Licensed User
Longtime User
Hi All,

a few months ago I translated what I had already done for Basic4ppc, with an early version of Basic4android I made in pure code a small INI file management: Basic4android - db2000 utility - INI... Not tested with the new version of the compiler but can be useful?
 
Upvote 0

Mickego

Member
Licensed User
Longtime User
Is there the notion of an "ini" file in Android? Or what method is uded to store app vars that can be read and writen to a structured file (other than SQL db).

Thanks

Mike Whetmore


Just build these 2 functions to use map for ini-file handling
__________________________________________________
To write record HOST=127.0.0.1 to Test.ini, make this Call:
WriteIni("HOST","127.0.0.1",File.DirDefaultExternal,"Test.ini")
.................................................................................
To read above Record use this:
myhost = ReadIni("HOST",File.DirDefaultExternal,"Test.ini")
___________________________________________________
B4X:
'-----------------------------------------------------------------------
Sub WriteIni(mykey As String,myvalue As String,fPath As String,filename As String)
    Dim Map1 As Map
    Map1.Initialize
   If File.Exists(fPath,filename) Then
   Else
      File.WriteMap(fPath, filename, Map1)   'to create it if not exist
   End If
   Map1 = File.ReadMap(fPath,filename)
            Map1.Put(mykey, myvalue)
    File.WriteMap(fPath, filename, Map1)
End Sub
'--------------------------------------------------------------------
Sub ReadIni(key As String,fPath As String,filename As String)
    Dim Map1 As Map
    Map1 = File.ReadMap(fPath,filename)
   For i = 0 To Map1.Size - 1
      If Map1.GetKeyAt(i) = key Then 
         Return Map1.GetValueAt(i)
      End If
    Next
End Sub
'---------------------------------------------------------------------
 
Upvote 0

balistreri1

Member
Licensed User
Longtime User
Hello, I am trying to PUT and GET an INTEGER value to a MAP file and I am getting errors. Is there a way to read and write to these MAP files INTEGER values?
 
Upvote 0

balistreri1

Member
Licensed User
Longtime User
Dim Map1 As Map
Dim B1_Text As String
Dim B1_Width As Int

Map1.Initialize
Map1.Put("B1_Text", "Hello World")
Map1.Put("B1_Width", 123)

Button1.Text = Map1.Get("B1_Text")
Button1.Width = Map1.Get("B1_Width")

Only the Text/String value works, the integer WIDTH value generates error????
 
Upvote 0

balistreri1

Member
Licensed User
Longtime User
I fixed it..
Activity.AddView(Button1, B1_Left, B1_Top, B1_Width, B1_Height)

One last question..
How do I specify the filename and location of the MAP file???

I want have multiple files and replace them, thus loading new button values.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Ok, so I'm going the MAP route to create an .ini kind of file..
But I have one question that no one has yet putted...

Since the principle of an ini file is to set atrib to in app stuf, like positions, textsizes, colors, etc... some of these are stipulated in (programing time), so they are NOT changeable by the final app...

my questions are.. how does a MAP file look like?
what is the seperator for the "Key:Value"?
Erel says it can hold any type, so how would a color be referenced?
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
So I cated a very small test app, just to get a grip of what Map does and how it translates the values to store...
I main interest is in stuff like colors, so my test consisted in:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim Ini As Map
   Ini.Initialize
   Ini.Put("ColorByName",Colors.DarkGray)
   Ini.Put("ColorByARGB",Colors.ARGB(200,124,124,124))

   File.WriteMap(File.DirRootExternal,"SCMap.ini",Ini)
End Sub

The resulting fle, when opened in notepad, shows me a single line, but that line copied to this post seems to have hidden breaks seperating the values into lines.


#Sun May 06 14:40:00 CEST 2012
ColorByARGB=-931365764
ColorByName=-12303292

So, from my uderstanding, I CAN specify a color, in code, by it's name, like colors.Black, and that writes its integer equivalent, BUT I CANNOT translate a saved integer to a "known" color name... I'll have to live with it
 
Last edited:
Upvote 0
Top