Android Tutorial Collecting tips - Beginners wanted!

Status
Not open for further replies.
I want to collect useful tips for developers who are starting to develop with Basic4android.
I'm deeply familiar with Basic4android and therefore not always aware of difficulties that new users may encounter.
I would like to ask you to think for a moment about difficulties you encountered while getting started with Basic4android and write some tips that may help others who face the same difficulties.

Please start a new thread for any question. Only tips, examples and references should be posted here.
 

JMB

Active Member
Licensed User
Longtime User
Creating an SD Card file for use between Emulated Devices

In my investigations into the use of the AVD Manager, I've discovered the following about SD Card files. I am sure this stuff is all known to the experts, but I am still a beginner, so I thought I would post this info. I've tried putting it in the Wiki but there are problems there...

One of the options when creating a Virtual Device using the AVD Manager is to allow the use of a file to represent the SD Card. The advantage of this is that the file is not tied to a single Virtual Device, unlike the option to create an SD Card of a specified memory size.

This means that you can share the same SD Card file between emulated devices, although only one can access it at a time.

SD Card - File option

There is a tool provided with the Android SDK which allows you to create SD Card files. They are in FAT32 format.

In order to create a File, you need to use a tool which is provided by the Android SDK, "mksdcard", and a reference to this tool can be found in the Android Documentation here.

If you want to create a file with a volume name of mySDCard, and a size of 128M, you would follow these steps:

  • Open a Command Prompt window from within Windows (on Windows 7, this is found under "All Programs-Accessories-Command Prompt")

  • If your path does not contain the Android SDK (which it probably doesn't), you will need to navigate to the folder "C:\Program Files (x86)\Android\android-sdk-windows\tools" using DOS commands.

  • Enter "mksdcard -l mySDCard 128M mySDCardFile.img"
This will create a file called "mySDCardFile.img". You can use this as your SD Card file from within the AVD Manager.

As an interesting aside, for Mac users there is the added bonus that you can mount this image file on your Mac desktop and it will appear as a drive to which you can add or delete files as required.

Note that when the image file is in use, it is locked, so you can't edit files from the Mac O/S while it is in use inside an active Virtual Device. You have to close the Virtual Device first. Similarly, when the Mac O/S has the image file mounted, you won't be able to use it in a Virtual Device.

I've not found an equivalent way of accessing the image file in Windows yet.
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
I see that you are experiencing the same thing I have -- that working on the documentation Wiki pages is an excellent way to learn more about B4A.

When I was working on Keywords, I noticed that while Dim a, b, c As Int is a shortcut for making all three variables integers, this shortcut cannot be used for Dim SomeSub (a, b, c as Int). Instead, you have to say Dim SomeSub (a As Int, b As Int, c As Int). I had previously overlooked the statement in the docs that the shortcut only applies to Dim.

I've got over 100 subs in my currrent app and many of them had the incorrect syntax, so I had to go back and change them all.
 

JMB

Active Member
Licensed User
Longtime User
And here's something else I discovered while investigating stuff for the Wiki.

As a beginner, it's easy to get confused - or even lazy - about defining your variables, especially when Float and Double are the options - they're both non-integers so it's tempting to think it won't really matter.

A Float is a single precision 32 bit floating point number - i.e. it takes up 4 bytes.
A Double is a double-precision 64 bit floating point number - i.e. it takes up 8 bytes.

It's tempting to think you're going to save memory by using Floats instead of Doubles, especially if you're creating big arrays of these numbers.

But beware of rounding errors!

I've been playing with some functions for the Wiki and I noticed this. The ABS() function expects a Double as the argument and returns a Double as the results. But say we decide to try and save memory and use Floats instead...

Dim a as float :a=1.1

If you do msgbox(abs(a),"") you get 1.100000023841858

set a to 1.9 and abs(a) returns 1.899999976158142

set a to -1.1 and abs(a) returns 1.100000023841858

set a to -1.9 and abs(a) returns 1.899999976158142

If you then do this:

Dim b as float: b=abs(a)*10000

you get 18999.99976158142

These are significant errors!

However, if you define your variables as types which match the type expected by the ABS() function i.e. a Double then you get all the correct results:

abs(1.1)=1.1
abs(1.9)=1.9
abs(-1.1)=1.1
abs(-1.9)=1.9

and 10000*abs(-1.9)=19000

I guess the moral of the story is don't be lazy with selecting your variable types! It's all very well saving memory, but make sure you're not catastrophically affecting your maths accuracy.

JMB
 

agraham

Expert
Licensed User
Longtime User
Be aware this effect can affect Doubles as well although it is less obvious owing to the higher resolution of Doubles. It arises because the binary format of Float and Double cannot accurately represent every decimal number. Floating point operations on computers are effectively always approximations. Where absolute accuracy is required special techniques are required or special types like the Java BigDecimal or the .NET Decimal need to be used.
 

rajaramcomputers

Member
Licensed User
Longtime User
Adding a Library for your Application

I started downloading the MySQL (Database Example) and it worked fine.

I started writing my own code, try adding code referencing HttpClient and I found the difference in color (Not Blue) and no reference for the HTTP Library.
Simple..
Here I am sharing the image of where we have to add the available library

Without Library
wOLib.png


Since there is no reference to HTTP and HttpClient will not be listed

With Library

WLib.png


Done ..
 
Last edited:

alfcen

Well-Known Member
Licensed User
Longtime User
Unwanted Click Event

Suppose you have an Activity containing several buttons with Click events.
Now, you add a Panel onto the Activity, thus covering buttons. As you tap on the
panel you will see that a click event was fired on a button on the Activity.
This is NOT a B4A bug, on the contrary, I might be quite useful. However, if this
is not wanted, just add:

B4X:
Sub Panel1_Click
   'do nothing here or place code to be executed upon tapping on the panel
End Sub
 

kanaida

Active Member
Licensed User
Longtime User
Passing URL's a little more safely.

Making a string unreadable but URL friendly. Sometimes you don't want to show plain text in a URL with parameters, but aren't so worried about encryption. You just don't want users easily changing things and getting creative on you. This is not recommended for usernames and passwords, but perfectly okay for other data you just want to obfuscate a little bit.

Add a reference to StringUtils

B4X:
Dim s As StringUtils

Dim OrderId as string
OrderId = "AB32409"

Dim EncodedString as string
EncodedString = s.EncodeUrl(s.EncodeBase64("Administrator".GetBytes("UTF8")),"UTF8")

'Do some work, like an http request:
Dim URL As String
URL = "http://www.something.com/something.asp?OrderId=" & EncodedString

Dim req As HttpRequest
req.InitializeGet(URL)

'some code to send the request etc...

It makes the order Id look similar to: 0Mdf%6dDfTI%3D

And of course your page that's receiving the request has to first do Base64String -> Byte() -> String to read it. In vb.net/asp.net/C# it's a one liner:
B4X:
Dim OrderId = Encoding.UTF8.GetString(Convert.FromBase64String(EncodedOrderId))
 

optimist

Member
Licensed User
Longtime User
DBUtils.ExecuteListView and ListView_ItemClick

I want to collect useful tips for developers who are starting to develop with Basic4android.

OK, here is my Tip regarding ListView_ItemClick and DBUtil.

It's well documented, that you can add Database-Records to an Listview:
B4X:
DBUtils.ExecuteListView(SQL, "SELECT Line1, Line2 FROM Table", Null, 0, ListView, True)

BUT: How to connect back to Database-Records after ItemClick?

B4X:
Sub ListView_ItemClick (Position As Int, Value As Object)
  Log("Value: " & Value) End Sub
End Sub
is useless. It delivers someting like "[Ljava.lang.String;@44f98238"

Solution:

Use an Database-Key as third invisible Paramerter in Database-Query
B4X:
DBUtils.ExecuteListView(SQL, "SELECT Line1, Line2, Key FROM Table", Null, 0, ListView, True)

In ItemClick Event cast "Value" to an Array of Strings
B4X:
Sub ListView_ItemClick (Position As Int, Value As Object)
 Dim Cols() As String
 Cols = Value
 Log("Pos:    " & Position)
 Log("Line1:  " & Cols(0))
 Log("Line2:  " & Cols(1))
 Log("Key: " & Cols(2))
End Sub

Using this invisible Key, it's easy to perform Database-Actions on clicked Database-record. :)

Greetings
Michael

PS: My english is not so good. If something is incorrect pleas correct.
 

gpe

Member
Licensed User
Longtime User
as a matter of facts

First two tips:
- Compiler says: "Are you missing a library reference?". Go to ......

Hello everybody I'm a new here (and probably the eldest of all members).
As a matter of fact, this was the first difficulty I encountered, till I saw this thread, so, my first suggestion is to give it more visibility.

Many thanks, I'm discovering a new world !
 

Jim Brown

Active Member
Licensed User
Longtime User
IDE tip

Use #Region ... #End Region to create custom collapsible sections in your code. Very handy for hiding a large blocks of source code
B4X:
#Region My collection of functions
Sub SomeThing()
   ...
End Sub

Sub Another()
   ...
End Sub
#End Region

When collapsed all you see is:
B4X:
[+] My collection of functions
 

warwound

Expert
Licensed User
Longtime User
Speed up the emulator...

Hi all.

Is it just (my) wishful thinking or does Zeam Launcher speed up the emulator?

Quote from Android Marketplace:

Zeam Launcher is a minimalistic launcher alternative for phones running Android 2.2 and above.

I've been using Zeam for a month or so now on both my ZTE Blade and Advent Vega so decided to try it on the emulator.
I'm impressed - the emulator is still far from fast but definitely seems more responsive with Zeam than it was with the stock launcher.

I copied the Zeam APK from my Vega to install it on the emulator - i didn't find anywhere online i could simply download it and of course we have no Market access on the emulator.

Martin.
 

Smee

Well-Known Member
Licensed User
Longtime User
A simple thing but i did not pick it up for quite a while. I started just adding a label and a panel to a blank sheet in the designer. Not to do anything but just to understand the 'basics'. It did not occur to me that i had to load the layout in Activity_Create. Even though i had gone through the creating a first program.
I couldn't understand why nothing was visible
 

klaus

Expert
Licensed User
Longtime User
If you had read the Beginner's Guide you would have known it.
In chapter 2. My first program this is explained in detail, and especially on top of page 25:
First, we need to load our layout file to the Activity in the Activity_Create Sub.


Best regards.
 

Smee

Well-Known Member
Licensed User
Longtime User
Thanks Klaus,

I did read it but as usual in a hurry to jump in and start and i must have missed it
 

Smee

Well-Known Member
Licensed User
Longtime User
Steep Learning curve :sign0137:

I think a good idea would be an absolute basic guide as well as the current one which is excellent, and thanks to all those who have made it so.

whilst reading some code trying to understand it
Dim panels(8) As Panel
For i = 0 To panels.Length - 1
it struck me as odd.
most programming languages start at base 0 but i thought why is it -1. Obviously when i took the -1 off the code it threw an exception but in vb this loop may read as

Dim panels(8) As Panel
For i = 0 To panels(count) or 8

so if u forgive my rambling, a VERY basic guide explaining this may be helpfull?

Esp for those of us that think we know how to do it, skim the beginners guide, realise we know jack and then have to go to the VERY beginning to learn the whys and wherefore's with some small examples

Thanks Erel for a great P/L and environment

Joe

As per usual u can forget most of this rant and did not research enough. I just discovered nfordbscndrd's excellent part of the guide. Although i still do not fully understand a dim of 8 and then subtracting 1

Sorry guys
 
Last edited:

klaus

Expert
Licensed User
Longtime User
Dim panels(8) As Panel
Defines 8 Panels.
panels.Length = number of items, number of Panels.
The Panel items go from
panels(0), panels(1), panels(2), panels(3), panels(4), panels(5), panels(6),
panels(7) 8 items with indexes from 0 to 7 so panels.Length - 1 .

So
Dim panels(8) As Panel
For i = 0 To panels.Length - 1
is correct and necessary

Best regards.
 

warwound

Expert
Licensed User
Longtime User
Here's a piece of info i found recently on the forum - can't find the thread now though.

You can set the width or height of a View to -1 and it will fill it's parent's width or height.

You can use -1 as the width or height of a View either in code or in the Designer.

Martin.
 
Status
Not open for further replies.
Top