Android Utils

A small collection of classes and methods for Android.
There is also some stuff from Class Collection.

Have not done anything on this for a long time so it is kind of dead :-(

Here you can download the code: Google Drive
  • Check if a string is empty.
  • Cut up string into certain length. stringCutUp("abcdefgh",3 ) => abc def gh
  • Get String between two other Strings. getStringBetweanStrings("aabbbcc", "aa", "cc") ==> bbb
  • Replace String between two other Strings. replaceStringBetweanStrings("aabbbcc", "aa", "cc", "ddd") ==> aadddcc
  • Break a string into tokens, stringTokenizer("hej|hopp||1|") ==> 'hej', 'hopp', '', '1', '' (also returns empty tokens)
  • Replace all occurences of text with another in a StringBuilder
  • Get firstIndexOf and lastIndexOf char in a StringBulder
  • GET/POST request to html server.
  • Get ip-number you have on the net.
  • Logging wrapper for android.
  • Get country, tows, treet and other informations from latitude and longitude
  • Check if date is valide.
  • Get current date and time yyyy-MM-dd and HH:mm:ss.
  • Parse and format date without creating new DateFormat.
  • Show a alert dialog.
  • A AsyncTask showing a please wait dialog on execute, dialog has a cancel button.
  • A activity with some extra features.
  • A widget with some extra features.
  • A ordinary ImageView but it also kan load a image from url
  • Parsing html with Jsoup.

Dependencies (All needed jar is under the lib directory)

Java 1.6
Android SDK (http://developer.android.com/sdk/index.html)
Jsoup, If you use JsoupConnector (http://jsoup.org/)

Usage

You must download Android SDK http://developer.android.com/sdk/index.html.
And add android.jar to your classpath.
In my case: /home/steelsquid/Workspace/Server/AndroidSdk/platforms/android-4/android.jar

Short description of classes

org.steelsquid.androidutils.Utils
Contains som usefull methods.
- Check if a string is empty.
- Cut up string into certain length. stringCutUp("abcdefgh",3 ) => abc def gh
- Get String between two other Strings. getStringBetweanStrings("aabbbcc", "aa", "cc") ==> bbb
- Replace String between two other Strings. replaceStringBetweanStrings("aabbbcc", "aa", "cc", "ddd") ==> aadddcc
- Break a string into tokens, stringTokenizer("hej|hopp||1|") ==> 'hej', 'hopp', '', '1', '' (also returns empty tokens)
- Replace all occurences of text with another in a StringBuilder
- Get firstIndexOf and lastIndexOf char in a StringBulder
org.steelsquid.androidutils.HttpClient
GET/POST request to html server.
org.steelsquid.androidutils.UtilsNet
- Check if port is free to use.
- Get ip-number you have on the net.
org.steelsquid.androidutils.SocketClient
A very simple class to send/receive text to/from a socket server.
org.steelsquid.androidutils.LocationName
A locatin from ReverseGeocoder.
org.steelsquid.androidutils.Logging
Logging wrapper for android.
org.steelsquid.androidutils.ReverseGeocoder
Get country, tows, treet and other informations from latitude and longitude.
Read the information from http://maps.google.com/maps.
You also need a http://code.google.com/android/add-ons/google-apis/maps-api-signup.html.
org.steelsquid.androidutils.UtilsTime
Usefull crap to handle time and date.
- Check if date is valide.
- Get current date and time yyyy-MM-dd and HH:mm:ss
- Parse and format date without creating new DateFormat.
org.steelsquid.androidutils.UtilsAndroid
Some useful Android stuff.
org.steelsquid.androidutils.AbstractDialogTask
A AsyncTask showing a please wait dialog on execute, dialog has a cancel button.
Work as a AsyncTask with a please wait dialog.
execute() will start the task
cancel() will stop the task early (onCancel do the stuff that make it cancel).
Override method: onStart() Execute in the UI thread before onBackground().
Override method: onBackground() Execute in own thread.
Override method: onCancel() Cancel the task.
Override method: onStop() Execute in the UI thread after onBackground().
org.steelsquid.androidutils.AbstractWidget
Set update interval in seconds. Send redraw command...
Override method: onContentView(): You only need to return the layout id to use in this activity.
Override method: onClickView(): Use this to start a activity when the widget is clicked.
Override method: onBuild(): Called the first time an instance of your widget is added to the homescreen.
Override method: onPrepare(): This is called before onDraw is executed for all appWidgetId.
Override method: onDraw(): This is called to update the App Widget at intervals defined by the updatePeriodMillis attribute. This method is also called when the user adds the App Widget
Override method: onDelete(): This will execute when a widget is removed from the home screen.
Override method: onDemolish(): Called once the last instance of your widget is removed from the homescreen.
org.steelsquid.androidutils.AbstractActivity
This is a Activity with some extra features.
Override method: onContentView() Return the layout id to use in this activity.
Override method: onAutoLoadAndSaveView() Child input views will have there value/status automatically saved and load on activity start and stop.
Override method: onOptionsMenuView() Return the view for the option menu.
Override method: onOptionsMenuClicked() When a item is clicked in the option menu.
Override method: onBuild() Use this to load the state and create the activity
Override method: onShow() Use this when a activity is shows.
Override method: onHide() Use this when a activity hides.
Override method: onDemolish() Use this to (uncreate) the activity (maybe save data).
Execute method broadcast:
By using the method sendMethodExecBroadcast(final String nameOfMethod, ...) you can send a broadcast to a activity that executes a method on that activity.
The method must look like this: public void nameOfMethod(final Bundle extras) throws Exception 
Send new intent:
By using the method sendMethodExec(final String nameOfMethod, ...) you can send a intent to a activity that executes a method on that activity.
The method must look like this: public void nameOfMethod(final Bundle extras) throws Exception {
org.steelsquid.androidutils.ImageUrlView
A ordinary ImageView but it also can load a image from url
org.steelsquid.androidutils.JsoupConnector
This class uses jsoup to parse html.
Hidden fields, cookies and permanent parameters.
Cookies will be read from the server response and moved to the next server request.
If the server response has hidden fields they will be moved to the next server request.
You can also set permanent paramaters that will be sent on every server request.


Revision history

2011-04-10   0.2   Initial release
2011-05-08   0.3   Nothing new in the code but using Netbeans 7.
2011-08-06   0.4   UtilsNet and SocketClient.
2011-09-07   0.5   JsoupConnector.
2011-09-15   0.6   New AbstractActivity, AbstractWidget and UtilsAndroid.
2011-09-25   0.7   Lot of changes in AbstractActivity and AbstractWidget.