Friday, November 8, 2013

Android Java Code Fix: StrictMode prevents code execution

StrictMode.ThreadPolicy was introduced since API Level 9 and the default thread policy had been changed since API Level 11, which in short, does not allow network operation (include HttpClient and HttpUrlConnection) get executed on UI thread. if you do this, you get NetworkOnMainThreadException.
This restriction can be changed, using:
    if (android.os.Build.VERSION.SDK_INT > 9) {
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
      StrictMode.setThreadPolicy(policy);
    }
Add the above code into your main activity's onCreate() method.
In addition, it is always recommended to move network operation off the UI thread, for example, using AsyncTask.

Read more discussions at http://stackoverflow.com/questions/8706464/defaulthttpclient-to-androidhttpclient

Thursday, October 31, 2013

AIDE: Developing real Android Apps directly on Android devices


-----
AIDE is an integrated development environment (IDE) for developing real Android Apps directly on Android devices. AIDE supports the full edit-compile-run cycle: write code with the feature rich editor offering advanced features like code completion, real-time error checking, refactoring and smart code navigation, and run your App with a single click.

AIDE will turn your Android tablet with keyboard into a real development box. We use the Transformer Prime running Ice Cream Sandwich to code with AIDE. AIDE will turn your Android Phone into a small development computer to browse and touch your code on the go.
AIDE supports building Apps with Java/Xml and the Android SDK as well as Apps with C/C++ and the Android NDK.

AIDE is fully compatible with Eclipse projects. You can just copy the sourcecode to your device and open the Eclipse project in AIDE to start coding. Alternatively you can keep your sourcecode on your Dropbox - AIDE integrates with Dropbox and allows to easily download from your Dropbox and sync back your changes. AIDE can also open Android Studio projects, which follow the default folder structure.
AIDE supports GIT for professional development
-----
Cover art
---
INSTALL: https://play.google.com/store/apps/details?id=com.aide.ui

Android 4.4 Kit Kat Special Offer

http://www.android.com/kitkat/
Kit Kat is giving you the chance to win a Nexus 7
Find more info on the page below:

Android 4.4 Kit Kat Update



5 Things We Know About Android 4.4 KitKat
By Cory Gunther —  10/30/2013
(Read more at http://www.gottabemobile.com/2013/10/30/5-things-know-android-4-4-kitkat/#A5G6qxk3grCb6MpC.99)

1) UI Changes

2) Google Experience Launcher

3) Hangouts

4) Device Updates

5) Release Date

Wednesday, October 30, 2013

Basic4Android Version 3 Tutorial (Download, Setup and Get Started)


-----
Basic4Android Version 3
Contents
Basic4Android Version 3 Tutorial (Download, Setup and Get Started)

1) Download Basic4Android Version 3

1.1) Go to URL http://www.basic4ppc.com
1.2) Notice that the News section announces the release of Basic4Android v3.0.0
1.3) For further info on v3.0.0, you may read http://www.m2mevolution.com/news/2013/10/15/7478239.htm
The website noted that:
The highlight of this new version (v3.00) is a new Rapid Debugger tool that supports enhanced productivity features, such as hot code swapping, that are not available in any other native Android development platform.
The Rapid Debugger tool was designed to increase developers' productivity by drastically shortening the "write code, then test result" cycle. Using standard debuggers, it often takes 20-30 seconds (and sometimes even longer) to compile and run an app on a mobile device. With this new tool, developers can activate rapid debugging mode and see the results of their code changes in less than two seconds. Over the course of a development project, this can save hours in wasted development time.
Another key feature enabled by the Rapid Debugger is hot code swapping. This allows developers to modify the code while the app is running and view the results "on the fly", without the need to close the app, compile the code and re-run the app. Hot code swapping helps to streamline the coding process and saves significant time and development costs.
1.4) Click the button “Download the free trial version”.
1.5) You will be forwarded to http://www.basic4ppc.com/android/downloads.html
Take note that the web page says:
“Basic4android can run in two modes: local compilation and remote compilation”.
1.6) The next section of the pages shows you how to install in both ways.

2) Install Basic4Android Version 3

2.1) As alternative to the above steps, you can also try the steps below:
2.1.1) Download Basic4android v.3.0.0 from http://www.basic4ppc.com/android/files/b4a-trial.exe 
2.2) Save the file b4a-trial.exe
You can just save it to My Documents.
2.3) Run the installer.
2.4) Starting the Setup Wizard.
2.5) Accept the agreement.
2.6) Select destination location, e.g. c:\b4a.
2.7) Option for additional tasks.
Click Next.
2.8) Ready to Install.
2.9) Complete the Setup Wizard
The initial promo screen appears.
Proceed to the next section.

3) Getting Started with Basic4Android Version 3

(This step assumes that you have followed the entire Step 2 above)
(Make sure that your Android Emulator is running before commencing this steps below)
3.1) You need to first configure the paths.
Go to menu Tools/Configure Paths.
3.2) Browse for the paths for the required programs and click OK.
3.3) The Main Window contains a default sample codes which are ready to be tested on emulator or real device.
3.4) Find the Run button located in the tool bar.
Click Run.
3.5) You will be required to save your file.
The following example shows the file name “test” that is saved in the location c:\b4a\workspace.
Wait for the process…
3.7) The Android Emulator
---
NOTE:
Basic4Android has a help page for index search
Visit the site and search what you need
Basic4Android has a Wiki Page for libraries, http://www.basic4ppc.com/android/wiki/index.php/Libraries
Visit the site and try them out!.
-----

Wednesday, October 23, 2013

Using Java In Eclipse For Android Apps Development (For Beginners)

Android Developer Toolkit (ADT) is a free software available at https://developer.android.com/sdk/index.html

Download the ADT bundle zip file from the above URL, unzip and run the program called eclipse. This applies to all platforms (Windows, Mac and Linux).

-----

1) RUN ADT

1.1) Run Android ADT Bundle (v21.0.1-543035)
1.2) Close Welcome Panel.

2) CREATE NEW PROJECT

2.1) Go to menu File/New/Android Application Project
2.2) New Android Application (Names and SDK Versions)
Application Name: Greetings
Project Name: Greetings
Package Name: com.example.greetings
Minimum Required SDK: API 8: Android 2.2 (Froyo)
Target SDK: API 17: Android 4.2 (Jelly Bean)
Compile With: API 17: Android 4.2 (Jelly Bean)
Theme: Holo Light with Dark Action Bar
2.3) New Android Application (Project Configuration)
Accept Default.
2.4) New Android Application (Launcher Icon)
2.5) New Android Application (Create Activity)
2.6) New Android Application (Activity Layout)
2.7) Done.
2.7.1) activity_main.xml is selected by default.
2.7.2) the file content is displayed in the form of Graphical Layout.

3) EDIT THE (LAYOUT) VIEW

3.1) Notice that initially the layout contains a label “Hello World!”.
3.2) Click to select the label.
Press [Delete] button to delete the label.
3.3) In the Layouts section of Palette, click and drag LinearLayout (Vertical) to the top left corner of the main layout.
3.4) Find the Outline Panel.
Notice that there is a warning icon.
If you hover your mouse over the icon, the explanation message pops up.
We will add child objects into this layout.
3.5) Find the abc Text Fields object in the Palette.
3.6) Click the object and drag it into the LinearLayout item in the Outline Panel.
3.7) Outcome:
3.8) Find the button object in the Palette.
3.9) Drag it into LinearLayout and place it under editText1 object as follows:
3.10) Your view will look like below:
3.11) Save.
3.12) Switch to activity_main.xml tab.
You should see the following codes:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >
        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <requestFocus />
        </EditText>
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
</RelativeLayout>
Take note of the highlighted (id) texts, i.e editText1 and button1.
Later, we will use these id names to bind Java codes to the XML layout.

4) VIEW JAVA CODES

4.1) In the Package Explorer panel, find the source file MainActivity.java.
4.2) Double-click the file name.
Notice two panels.
4.2.1) Main Panel.
4.2.2) Outline Panel.
Note: To show line numbers for code editor:
Go to menu Windows/Preferences,
Click Editors/Text Editors,
Select Show line numbers.

5) UNDERSTANDING JAVA CODES

This section assumes that you are totally new to Java Language.
5.1) The Outline Panel shows the structure of a typical Java Code File (technically called Java Class File)
The code structure is divided into 3 parts.
Part 1: Package name (written in reverse-DNS form). This is a unique name representing your project.
Part 2: Import Declarations. Think of this as code libraries from which you may import methods (functions) for your programming solutions.
  • android.app.Activity is a class used to represent a single screen with a user interface.
  • android.app.Bundle is a class used for passing data between activities.
  • android.app.Menu is a class used to provide menu options related to an application.
Part 3: The MainActivity Definition. This is where you specify what you would want the computer to do.
In the above example, you have instructions for the event onCreate and onCreateOptionsMenu, i.e.
Event onCreate = when the activity is created
Event onCreateOptionsMenu = when the OptionsMenu is created
5.2) The Main Panel shows the codes in Java Syntax.        
There are few things to learn from the codes:
  • Related codes are grouped within { } symbols. They can be nested as well.
  • Java implements object oriented programming concept. You will see a lot of dot-style notation e.g. android.os.Bundle.
  • New class (child) can be created from the existing class (parent). The child inherits the properties and methods of the parent. E.g.  MainActivity will inherit whatever properties and methods of Activity class. This promotes code-reusability.
  • The keyword “super” is used to initialize the object based on the parent’s method first.
  • If the child class would want to modify some methods inherited from the parent class, it can “override” (refer line no. 9 and 15) them with different instructions. E.g. (line no.12) setContentView with the res/layout/activity_main.xml file and  (line no.18) getMenuInflater by inflating values from res/menu/activity_main.

6) EDITING JAVA CODES

6.1) Initially, XML Layout and Java Codes are not related and thus they do not know one another. Objects in the XML Layout (e.g. editText1 and button1) must be identified first before they can be processed by Java Codes.
6.2) Let’s say we want …
the button to respond to clicks by …
popping up a message containing text entered by user in the text field.
6.3) We need the following algorithm:
(blue colour text refers to the codes in Step 6.4)
1) Bind a button object to the button layout identified by button1. (line no.13)
2) Attach an onClickListener (a method to listen for click events) method to the button. (line no.14)
3) Override the onClick (a method to respond to click events) method to the button. (line no.16)
4) Bind a text object to the text layout identified by textEdit1. (line no.17)
5) Get the input value from the text object and convert the value to a string. (line no.18)
6) Display the string value using toast object. (line no.19)
6.4) Edit the codes as follows:
6.5) Error markers appear under certain keywords that are not recognized by Java compiler. However, Eclipse has a smart debugging function that is able to suggest corrections. In this example, choose the option to import the missing class.
6.6) Repeat the same technique to the other error markers. Do it carefully and ensure that you are choosing option to import class only.
6.7) Outcome:
6.8) Run as Android Application…
6.9) Outcome: