Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Saturday, August 2, 2014

Steps to develop Android applications

So I think the way to setup everything and to be able to develop and run an app are not friendly user at all.

Just last week, I met a guy who is interested in Android. He downloaded the ADT plugin with eclipse. Everything looks good, he created a new application based on the "blank activity". Then he hit run and boom error... How to demotive new people to start coding app for Android...
I think I had a similar problem with a "fullscreen activity".


Anyway, here are the list of steps that I should remember when changing VM:

- if it's a 64-bit VM, don't forget that Android architecture is ARM 32-bits, so you should install 32-bits libraries!
sudo apt-get install libc6:i386 libstdc++6:i386
sudo apt-get install lib32z1

- you will need Java:
sudo apt-get install openjdk-7-jdk

- Setting up a Device for Development
http://developer.android.com/tools/device.html
sudo vi /etc/udev/rules.d/51-android.rules
#Samsung 
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", MODE="0666", GROUP="plugdev"
#Google
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", MODE="0666", GROUP="plugdev"
#ASUS
SUBSYSTEM=="usb", ATTR{idVendor}=="0b05", MODE="0666", GROUP="plugdev"
#LG
SUBSYSTEM=="usb", ATTR{idVendor}=="1104", MODE="0666", GROUP="plugdev"

sudo chmod a+r /etc/udev/rules.d/51-android.rules

For VirtualBox:
- install the Guest Additions
- add a shared folder so it's easy to copy from file between host and guest
- add your user to the group vboxsf to be able to access the share folder:
sudo usermod -a -G vboxsf alex
- in the VB settings of the VM, go to USB and add your device! So the VM will capture the device.
- install GIT
sudo apt-get install git

You can install Eclipse IDE with built-in ADT
http://developer.android.com/sdk/index.html
And go to: Window-> Preferences -> Android -> DDMS -> ADB Connection Timeout (ms)
And set it to 10000 (if you leave it to 5000, you  may see some timeout)

Sunday, February 20, 2011

SQLiteDatabase created and never closed

Problem
When I'm using my application and I can see this error in the LogCat tab :


02-17 17:20:38.556: ERROR/Database(434): Leak found
02-17 17:20:38.556: ERROR/Database(434): java.lang.IllegalStateException: /data/data/databases/xx.db SQLiteDatabase created and never closed


Solution
Well, at first I thought it was because of my cursors. So I decided to check every cursor and close them all after having done with them.
But I still got the same problem.

So in fact, it's just that for every activity (onCreate) I was opening a connection to the database, but I never closed it.
So the solution was to close the connection in each activity:


@Override
protected void onStop() {
dbService.closeConnection();
super.onStop();
}


And that's it !

Sunday, February 13, 2011

Android Insufficient storage

Problem
I was debugging my application using an android virtual device in Eclipse.
When I run the application I could see this error in the console:

[2011-02-12 19:22:25 - shiro] Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE
[2011-02-12 19:22:25 - shiro] Please check logcat output for more details.
[2011-02-12 19:22:25 - shiro] Launch canceled!

The size of my app is around 22Mo (there are a lot of pictures).

Solution
The first time I configured the android virtual device, I put only 2 or 3Mo for "SD Card size".
So I had no choice to create a new virtual device and put something like 30Mo.
I ran my app for the first time. No problem. Then I wanted to run it again, and I got the same problem. I guess I could set the size of SD card to 100Mo, or in the virtual device, you can go to the menu > settings > manage applications. Then choose the application and uninstall it. Then you can run your app again in debug mode from Eclipse.

And that's it!

Tuesday, September 7, 2010

Android: 2 view elements side by side

In my Android application, I wanted to display 2 spinners, side by side (50% of the width each).
I didn't want to use width = "100px" for example. I wanted to be relative as much as possible.
So here is what I did:


android:layout_alignParentBottom="true" android:layout_width="fill_parent">
android:layout_height="wrap_content" android:weightSum="100">

android:id="@+id/btnBack"
android:layout_height="wrap_content"
android:text="@string/back"
style="?button"
android:layout_weight="50">


android:id="@+id/btnMap"
android:layout_height="wrap_content"
android:text="@string/map"
style="?button"
android:layout_weight="50">





***WARNING*** XML tags are case sensitive. In my example above, I don't know why it's display in lower case... Please use TableLayout, TableRow and Button!

I created a tableLayout with 1 tablerow. In each tablerow I added 2 buttons.
I set the attribute weightsum="100" to the row. And for each element of the row,I set layout_weight="50".
And that's it!

Friday, March 26, 2010

Android join tables

In my application, I have many tables and for a reason, I had to do a query using "join".
I'll not explain how to access a database, but just so you know, check on Google for
object SQLiteDatabase.

So I was checking at the method query , here is the information:

public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

Parameters
table :The table name to compile the query against.

For example:

Cursor cursor = myDataBase.query(
"myTableA" ,
new String[] { "_id", "name"},
null,null, null, null, null);

That's nice, but what if I want to a "join" ? I need more than 1 table in my query.
Well, at beginning I found CursorJoiner. Nice, I can join 2 cursors. But what if I have 3 or more joins to do ??

In fact, the parameter "table " (function query) can contains many tables (not just one).
For example:

Cursor cursor = myDataBase.query(
"myTableA a, myTableB b",
new String[] { "a._id", "a.name", "b._id", "b.name"},
"a.foreignKey = b._id",
null, null, null, null);


That was not difficult, but the definition of the parameter "table " was not so good.

Another solution would be to use SQLiteQueryBuilder.


SQLiteQueryBuilder myQuery = new SQLiteQueryBuilder();
myQuery.setTables("myTableA, myTableB");
myQuery.appendWhere("a.FK = b._id");
Cursor cursor = myQuery.query(myDataBase, null, null, null, null, null, null);

Tuesday, March 16, 2010

Android: add xml layout dynamically

For the application I'm creating, the user can use "search" function. This function will return a list of castles.
Here is the XML to display one result (/res/layout/element_result.xml). There is 1 button, 2 text fields and 1 image :


android:id="@+id/layoutElement"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
android:id="@+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/go"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
>

android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/presentation"
android:layout_alignLeft="@+id/presentation"
>

android:id="@+id/presentation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/image"
>

android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>




Then, I created a XML layout (/res/layout/results.xml) who will contain the list of results. That means, there will be many times element_result.xml inside results.xml. There is a scrollView and a table layout.


android:id="@+id/widget28"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
android:id="@+id/scrollResult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal"
android:layout_above="@+id/btnBack"
android:layout_alignParentLeft="true"
>
android:id="@+id/myTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>



android:id="@+id/btnBack"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/back"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
>




Ok, our views are ready. Now how to use them in Java. I read this blog, and that helps me a lot : Android LayoutWidth being disregarded by cascaded use of LayoutInflater.

Here is my activity file:

public class ResultActivity extends Activity{

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// "results" is for the xml file "results.xml"
setContentView(R.layout.results);

addElementResult();
}

public void addElementResult() {
// "myTableLayout" is the id of the element in results.xml
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
// we will use service from inflater
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);

//doing a loop to add many times the same xml
for(int i = 0; i < 17; i++) {
// "element_result" is the name of the xml file "element_result.xml".
// Create the itemView who will be added.
// itemView = element_result.xml
View itemView = inflater.inflate(R.layout.element_result, null);
// get the textView, and set the text to something
TextView t1 = (TextView) itemView.findViewById(R.id.presentation);
t1.setText("something" + i);

TextView t2 = (TextView) itemView.findViewById(R.id.description);
t2.setText("oh oh oh" + i);

//add the itemView
tl.addView(itemView, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
}


So the result will look like that :


Note: in results.xml, I created a scrollView because if there is 20 results, the user should be able to scroll down to see all the results.
Note: I used "tableLayout" to be able to add results one under another one.

Wednesday, March 10, 2010

Android: main.out.xml error


So it's been long time, I'm not doing Ruby anymore.
Now, for myself, I decided to create an Android application. At first, I wanted to create an Iphone application, but only people having a Mac can use the SDK... So let's use the Android SDK!
After doing the tutorial Hello World (see here), I started doing my application. And after a few minutes I had an error...

[2010-03-09 11:32:04 - shiro]Error in an XML file: aborting build.
[2010-03-09 11:32:05 - shiro]res\layout\main.xml:0: error: Resource entry main is already defined.
[2010-03-09 11:32:05 - shiro]res\layout\main.out.xml:0: Originally defined here.
[2010-03-09 11:32:05 - shiro]D:\workspace\shiro\res\layout\main.out.xml:1: error: Error parsing XML: no element found

For information, I'm using Eclipse. Why when I click on "run" I have this error ? Why Eclipse is creating a new file "main.out.xml" and then complain about it ? My application is really small, I didn't write any line of code in my main activity...

Well the error is simple, and it's not related to the application directly. It's a problem with Eclipse. To run the application, I must select the project (root folder) in the left column and then I can click on "run".

I had this error, because I was running the "main.xml". So Eclipse could not do anything with that.
Here is a noobie error, but it was difficult to find the solution...
I guess the next post will also concern Android application. It's always fun to discover new things :)