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.
 

CarlM

Member
Licensed User
Longtime User
Note that the file is not deleted. So you can add it again easily.

Thats what I thought but I can't find the deleted code module either with the IDE explorer or Windows explorer.

Menu: Project > Add New Module > Code Module
(I named Module "TestCodeModule")

Menu: Project > Remove Module >
(module remeoved ok)

Menu: Project > Add Existing Module >
I can't see 'TestCodeModule' in any of the project or files directories, nor in Basic4Android directories..

:confused:
 

roarnold

Active Member
Licensed User
Longtime User
Installation of B4A and SDK/JDKs

I noticed during the installation of the SDK/JDKs in support of B4A including the Anywhere software permissions can be an issue depending on your ownership of the base computer. What I mean is - if you have a login to the computer the installation will be a bit different than if the system does not require login.

Permission will be required and if you have a login to the system then install the B4A, JDK and SDK at the root - "C:\". What I found is that the JDK/JRE will install in two locations ...\Program Files and \Program Files (X86). To solve the JDK issue is done by simply adding the JAVA_HOME environmental to the My Computer environmental list.

B4A and Android SDK they should be installed at the root (C:\) as Eclipse. Additionally, I set up a B4A location to save the project packages during installation under My Documents.

Hope this helps. Let me know if there are any questions. Ron
 

warwound

Expert
Licensed User
Longtime User
Enable links in a Label

Here's a little bit of code i thought i'd share with you all.

It's been asked before - how to enable clickable links in Label text, and this seems to be a simple solution.

B4X:
'Activity module
Sub Process_Globals
End Sub

Sub Globals
   Dim Label1, Label2 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim LabelText As String
   LabelText="some text with hyperlinks"&CRLF&CRLF&"<a href='http://google.com>Visit Google</a> and more text"&CRLF&CRLF&"http://developer.android.com/reference/android/text/util/Linkify.html#ALL"&CRLF&CRLF&"even more text here..."
   
   Label1.Initialize("")
   Label1.Text=LabelText
   
   Label2.Initialize("")
   
   '   enable the underlying TextView object's autoLink functionality BEFORE adding setting the Label Text property 
   Dim Reflector1 As Reflector
   Reflector1.Target=Label2
   Reflector1.RunMethod2("setAutoLinkMask", "15", "java.lang.int")
   
   Label2.Text=LabelText
   
   Activity.AddView(Label1, 0, 0, 100%x, 50%y)
   Activity.AddView(Label2, 0, 50%y+1dip, 100%x, 50%y)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

The Reflection library is used here to call the setAutoLinkMask of the Label/TextView.
(A B4A Label is an Android TextView).

The values that can be used for the mask are documented here: TextView | Android Developers, scroll down to Constants.

The value of 15 enables all links to be made clickable, other values can be used to enable just email addresses for example.
And presumable values can be combined using a logical AND to enable various combinations of mask.

Sample code attached.

Martin.
 

Attachments

  • LabelExtras.zip
    5.7 KB · Views: 329

CharlesIPTI

Active Member
Licensed User
Longtime User
Kneube OMG

:sign0104: :sign0104: :sign0104: :sign0104: :signOops:

No Time to search and see if this was expounded upon in this thread previously
but since I'm really hating this throw back language I thought I'd leave these tid-bits

I'm a C# developer, would love to do this in Java/Eclipse but I'm not proficient enough in that to assert my desire. :sign0104:


Sooo...

#1) Basic4Android does not like Brackets around its If statements

if ( xzy scoobiedoo ABC) Then ' its ok with the parens
{ do some funky chicken here } ' Its NOT KeWeL with the brackets

#1a) Oh and dont for get your stupid little End If's Uggggg Nasty


#2) Here's why I made this post If you initialize a new button in code
and name its event with the TRUE event name
"fooButton_Click"
the flipping activity fires that event --- when it loads up .. try it ... !

Sub Globals
Dim btnLogin As Button
End Sub


Sub Activity_Create(FirstTime As Boolean)
btnLogin.Initialize(btnLogin_Click) --- > NOTE NO QUOTES !!!!!! GRRRRRR!!!!!! :BangHead:
End Sub


Sub btnLogin_Click
ToastMessageShow("OMG Who's pushing my Buttons?")
End Sub


Yea I know stupid of me but the intellisense
asked me for the event name so I gave it the event name


#3)
Lastly place the "EventName"
hahahha its really the View(Control) name Grrrrr!!!! :BangHead: :sign0010: :sign0147:

as an argument in quotes like this
btnLogin.Initialize("btnLogin")

Because Basic4Android just wont find it PERIOD unless its in Quotes


:sign0089:
 
Last edited:

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Re: CharlesIPTI post

1. I hate brackets and prefer the cleaner End If. I program in C#, C++, PHP, etc all using them too. Even with indentation you have no clue if that bracket is for a Sub, an If, a Loop, etc. Sure you can edit the line and add a comment after it, but how is that different than End If?

2. By not using quotes you passed a function(And the exact name of it) to the Initialize statement...Of course it will execute. No Bug.

3. I'm not sure if I understand the comment, but I do agree the Eventname stuff was confusing at first how it reserves that name to mean the event prefix for the events for that view. It doesn't have to be the name of the view though. I actually name mine different. The examples often set the same name for multiple buttons too and process them all in one click event.
 
Last edited:

rbsoft

Active Member
Licensed User
Longtime User
@Roger
I program in C#, C++, PHP...
You forgot PowerBasic, Roger!:D


Rolf Brandt
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Hey Rolf. I wondered when we'd bump into each other here. PB doesn't use brackets though and uses the End X structure like I prefer. I meant in that statement that I used languages that have brackets and still hated them. Code Folding and indentation help as well as IDE perks where it matches them up, but if any are out of sync it doesn't match and you are in the same boat. Having BASIC structure and syntax is not "Nasty" or wrong, but just another way of specifying things. The only reason I commented was that it wasn't a beginner tip, but a user of another language just bashing BASIC and complaining about things for no reason.

One thing missing from B4A that would really help is when you type the beginning of a block structure it automatically fills in the rest like a template by adding the needed End X and placing your cursor pre-indented in between them ready to code.
 

rbsoft

Active Member
Licensed User
Longtime User
...a user of another language just bashing BASIC and complaining about things for no reason.
I fully agree, Roger.

About the block structures I have so gotten acustomed during my VB6 and PB/FireFly days to always add the END line manually right away. When I switched to VB 2005 I found it even annoying if I wanted to encapsulate a block of code with if ... end if and VB would automatically put the end if before the code block. So I finally deactivated it.

...and yes I hate the bracket stuff too. I found the code structure always a bit difficult to read when I was doing Dlls in C and C++.

Anyway good to have you here!


Rolf
 
Last edited:

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Yeah, it would have to be an option or something like holding ctrl+enter at the end of the line or something like the Tab stuff that creates the Event template Subs. I've used a couple IDEs that handled the encapsulation pretty well where if typing in between other text it didn't add the End statement for certain structures, and when you did finally type the End it auto indented what you encapsulated which was cool.
 

roarnold

Active Member
Licensed User
Longtime User
Arrays

I would recommend an additional example to the tutorial when discussing arrays. While it does speak of object and items associated with them it was not quite clear as a whole. Now after some fingering I managed to parse and add the info to the array. The difference being the example had { at the beginning with the [ further down with the menu items.
 

roarnold

Active Member
Licensed User
Longtime User
HTTP Utils

Would be nice to clarify with a simpler example how the utils work. I had to utilize HTTP client until such time I can go back and understand the utils you have.
 

CharlesIPTI

Active Member
Licensed User
Longtime User
... Having BASIC structure and syntax is not "Nasty" or wrong,


OK Nasty was a bad choice, I actually started out with Visual basic then Visual Basic.Net, I just don't miss those End If days. And I like method signatures that declare what type it is going to return instead of Sub DoSomething(String strSourceString). For like five minutes I sat there trying to figure out why it was complaining about my return type being in the signature then I did a quick review of VB syntax...


Its just a matter of preference.. don't kill me :sign0104: :sign0013:
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
And I like method signatures that declare what type it is going to return instead of Sub DoSomething(String strSourceString).

May want to review Section 13 in Beginners Guide (Reading the whole thing before developing is a good tip too). Returning a type is fine. The confusion may be that some languages use SUB and FUNCTION where subs usually don't return a type (Or really they do but it is ignored) and Functions return a value. B4A appears to be a hybrid like some languages in that Subs can do either.
 

LrdSatyr8

New Member
@Roger

You forgot PowerBasic, Roger!:D


Rolf Brandt

Awesome... Got some other PB users here too! Hey Rolf and Rog! Personally I like the structure of Basic over C because it's alot easier to read and follow when debugging. But, even thou I'm new around here, I've got a couple of good tips for SQL users here that aren't really explained very well in the beginner's guide that I've had to scour the bases for...

First off, make sure you have the SQL library check marked in your LIBS panel and then you have to define a variable for holding your SQL handle... do so with:

B4X:
Dim SQL1 as SQL

I prefer to place it in my Globals so I can access it thruout the code. Next here are a few good examples of all the major functions you will need to create a database handling application:

To check to see if a table exists in the database:

B4X:
tmp = SQL1.ExecQuerySingleResult("SELECT count(name) FROM sqlite_master WHERE type='table' AND name ='config'")

tmp will return 0 if the table doesn't exist.

To create a table, try the following:

B4X:
SQL1.ExecNonQuery("CREATE TABLE IF NOT EXISTS config (id INTEGER, itemname TEXT, itemcount DOUBLE)")

To insert values into your database you can do so directly or indirectly with variables. But make sure you place single ' (quotes) around text entries.

Directly:

B4X:
SQL1.ExecNonQuery("INSERT INTO config VALUES (1, 'Apples', 1234.56)")

or Indirectly:

B4X:
Dim Id as Integer: Id = 1
Dim ItemName as String: ItemName = "Apples"
Dim ItemCount as Double: ItemCount = "1234.56"
SQL1.ExecNonQuery("INSERT INTO config VALUES (" & Id & ", '" & ItemName & "', " & ItemCount & ")")

(Side note, the ExecNonQuery will not return a value but will trigger an exception if there is a problem and can be called directly)

To update an existing entry in the database with different data use the following example:

B4X:
SQL1.ExecNonQuery("UPDATE config SET id='"& et_ID.Text & "', itemname='" & et_ItemName.Text & "', itemcount='" & et_ItemCount.Text & "' WHERE id="& et_ID.Text )

To delete or remove a record from the database, chose a unique column and issue the following command:

B4X:
SQL1.ExecNonQuery("DELETE FROM config WHERE id='" & et_ID.Text & " AND itemname='" & et_ItemName.Text & "'")

To select a whole bunch of rows from your database and utilize them for use in your program using the wildcard *, here's the method I've been using:

B4X:
Dim Cur As Cursor
Dim Id as Int
Dim ItemName as String
Dim ItemCount as Double
Cur = SQL1.ExecQuery("SELECT * FROM config ORDER BY id DESC")    
For i = 0 To Cur.RowCount - 1        
  Cur.Position = i 
  Id = Cur.GetInt("id")
  ItemName = Cur.GetString("itemname")
  ItemCount = Cur.GetDouble("itemcount")
  ' Do stuff to the values here
Next

You could refine your search to just return a couple values like this:

B4X:
Cur = SQL1.ExecQuery("SELECT itemname, itemcount FROM config ORDER BY id ASC")

Only the two values will be returned in the search. Note, you can sort your search using the ORDER BY command at the end in either ASCending or DESCending order. You could probably do more with that but those two are the most common I've personally used.

You can also return a complete sum of all the values of database column using the following

B4X:
Cur = SQL1.ExecQuery("SELECT SUM(itemcount) AS itemtotal FROM config")
ItemTotal = Cur.GetDouble("itemtotal")

You can replace SUM with AVG to return the Average value of all the values. Note it will add all the non-Null values from the itemcount column and place the total sum value into the new value you create called itemtotal. You retrieve the info just like you would any other column.

Now as a bit of a tip, I prefer to place SQL direct commands in UPPERCASE and all my database table names in lowercase because it makes it easier for me to read and differentiate between a command and a variable.

As stated in a prior post, I find that BeginTransaction/EndTransaction greatly improve the speed of a large database but don't do much for smaller ones (less then 50 entries)

These are just a few of the SQL commands I've used and find very helpful in dealing with a database app. I'm sure I'm not perfect, and I cut and paste as much code as I could, but edited a few lines to give a but more clarity. If any of it doesn't work, please let me know and feel free to correct any typos I made. Hopefully it's of use to someone else. Happy programming all and it feels great to be excited about programming again! Thanks Erel!

-=> Jim! <=-
 

CharlesIPTI

Active Member
Licensed User
Longtime User
Humble

OK here's my two for today.. It was all too easy for me to treat a list (Collection) as if it were an array ,,,

one has to use the get(int X) and cannot access a list by lst(x)

:sign0161:

And One thing I'd like to have that I didnt see available is Multiple bookmarks:::
A) that are accessible through multiple modules
&
B) that persist after a debug run

Personally I use markers in pairs having one little bookmark that disappears after I debug once has me writing down line numbers & module names instead



WHOOPS ONE MORE Please

A Watch Window modern style would be NICE !!!!
 
Last edited:

RichardK

Member
Licensed User
Longtime User
Examining corrected code

When you post a zip file with problem code, and a corrected file is posted by one of the experts: copy the corrected code into a new folder and open both the original project and the correction side-by-side (by double-clicking on the B4A file).

example - incorrect code:
lblKeyWords.text = typDeck(Numbers(i).strkeywd)

Corrected by Klaus:
lblKeyWords.Text = typDeck(Numbers(i)).strkeywd

This makes changes easier to spot (though not neccessarily to understand).

RichardK
 

CharlesIPTI

Active Member
Licensed User
Longtime User
No Title Ful Screen & Stuff Like that

When you make an activity "Active" selected in the IDE the project menu options change. For example select a service instead of an activity and you can see that the menu options change.

Well The first time I added a new activity I couldnt figure out why I was still getting the app title .. I knew I removed that via the settings .. but now that I have TWO activites I have to make that setting for BOTH activities ,, theres even a setting for show title in the designer for layouts ..... so make sure you've crossed all of your t's and dotted your I's


Now if I could only get this this to accept my App Icon again -- I wonder if that changes with each activity :BangHead::BangHead:
 

CharlesIPTI

Active Member
Licensed User
Longtime User
Object Browser

With all of these libraries you will have to reference it would be nice to have an object Browser available
 
Status
Not open for further replies.
Top