Wednesday, February 25, 2009

Multi columns in listView

Hi all,

I read some articles on multicolumn in a list view with android.
As I worked on this subject, I can show you where I could get.

I tried to work on resolution independant list view, so I tried to place the columns relativelyto the screen, rather than relying on absolute coordinates. This way, the result should be correct, whatever the phone ( /notebook ? ) resolution, or whether you are on landscape or portrait mode.

The difficult part is to find the correct layout to pass to your adapter.
My first try was to have a column on the left side and a column on the right side.
Sound easy, but my first tries couldn't make it.

That was my first try for an line layout :



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">
<TextView
        android:id="@+id/PlayerName"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Nom"
        />
<TextView
        android:id="@+id/PlayerScore"
android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:gravity="right"
        android:text="score"
        />
</LinearLayout>



But it failed... Both entries were stacked on the left of the screen...

I tried both to set the gravity and the layout_gravity to 'right', but it didn't change anything.

The solution was with the width of my second text view. 
I imagine that I couldn't have the two textview width set to 'wrap content', because it couldn't fill a whole line.

So the trick was to have one textView width set to fill parent, the second one to wrap _content, and to have the text placed on the right of the second textView, using the gravity parameter.
But, for some reason, you can't set the first textview width to 'fill_parent'.
I imagine that in this case, the first textview will take all the parent width, and won't let any place for the second one. And when the second textView width is set to 'fill_parent', it will take all the place, but taking into account the space taken but by the first, already initialised, textview.

Here's the working code anyway :


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">
<TextView
        android:id="@+id/PlayerName"
        android:textColor="#ffFFffFF"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Nom"
        />
<TextView
        android:id="@+id/PlayerScore"
android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:gravity="right"
        android:layout_marginRight="25dip"
        android:textColor="#ffFFffFF"
        android:text="score"
        />
</LinearLayout>



Just a note : I add a right margin for the second text view, to reserve some space for the listview scrollbar ( without it, the text was behind the scrollbar ) !

Then I tried to have a three columns layout, with one column on the left, one column in the middle, and the last one on the right.
To be honest, I tried anything I could think of in tweaking the precedent code, with no success !

Until I tried to use a relative layout instead of a linear layout !

With a relative layout, the first try was the good one :


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
 >
<TextView
        android:id="@+id/Center"
android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true" 
        android:textColor="#ffFFffFF"
        android:text="Center"
        />
<TextView
        android:id="@+id/PlayerName"
        android:textColor="#ffFFffFF"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_toLeftOf="@+id/Center"
        android:text="Nom"
        />
<TextView
        android:id="@+id/PlayerScore"
android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_toRightOf="@+id/Center"
        android:layout_marginRight="25dip"
        android:textColor="#ffFFffFF"
        android:gravity="right"
        android:text="score"
        />
</RelativeLayout>




Now I still have a question in the back of my mind. 
This reminds me my early web development, where I tried to tweak HTML code to have resoution independant pages. HTML was designed for that !
And some years after that, every serious website is designed to be view in a fixed resolution.

So my question : is it worth it ?
For the moment, only one android device exist, so resolution independance is not a question.
And I feel like all the upcoming phones will have the same resolutions, and that every layout will be design with absolute pixel values... Wait and see ?


Wednesday, February 4, 2009

Tutorial : how to emulate an SD card with the Android emulator

It's important to know how to emulate an SD Card if you're into Android development.
Whether it be because your application actually use it, or to use it for your development, for instance for profiling.

Here are the required step to use an SDCard :

1) Create the emulated SDCard on your real computer :

Google provides a tool just for that, in their SDK/tools directory called mksdcard.
just give it the size of the SdCard you want to create ( in kilobytes or megabytes ) , the name of the resulting file, and you're done :
Open a command prompt, go to your SDK/tool directory, and type :
mksdcard 64M MyNewSdCard

and that's it !

2) Using the emulated SDCard with the emulator

To use the emulated SDCard in the emulator, simply launch the emulator with the '-sdcard EmulatedCard_File_Path' parameter :
So, from the command prompt in the SDK/Tool directory, type :
emulator -sdcard MyNewSdCard

3) Using the emulated SDCard with the emulator from Eclipse :

From eclipse, open the menu entry :
Run / RunConfigurations...

Then on the right panel, choose 'target'
And in the Additionnal Emulator Command Line Options, add
'-sdcard fullPath_to_your_SdCard_File'



And that'all !

4) How to explore the emulated SdCard content and push / pull files from your computer :

Within Eclipse :

Open the DDMS perspective :
Window / Open Perspective / Other... / DDMS

Now select the file explorer tab ( in the right panel ), and you have acces to a classic explorer of your emulated phone, including the sdcard !

Now to push a file from your computer, or pull a file from the emulated device, you have two discrete icons in the upper right corner :


Just use them to to browse and select on your computer the file to pull or push !

Without Eclipse :

Just launch DDMS from the command prompt in the SDK / tools directory.
The file explorer is in the device menu...

Monday, February 2, 2009

How I improved my android application responsiveness in the emulator... without doing anything !

It's been a long time since I found the Android application I'm developping ( a word game ) was not as responsive as I would like.
As I don't have any kind of Android phone, all of my testing happen in the emulator, and I'm not sure how well it fits the phone reality ( and actually, I'm aware that this does not mean anything, as there may be a lot of different phones out there... ).
But still, I expected the emulator to be in scope with reality, and to give me a good start to measure responsiveness ( and actually, I have no other choices ).

So the responsiveness of my application was below my expectations, and I had planned a optimization phase to fix it as much as I could.

And yesterday, I found myself manipulating my game, and finding it was responsive enough for me !
It took me a little time to understand what had changed, as I never tried to optimize it, and my last developments were GUI / cosmetic changes !

Actually, I had disconnected the debugger !
When my application is running with eclipse connected to it, it is way slower than it is without it !

Although it is a simple optimization, and quite reassuring for my game, it is the second time that having eclipse connected to the emulator gets in my way ( the first time being with the profiler : here ).

And that is more perturbing...