how to
How to: Create a scrollable Map with Cells
by Martin on Jan.04, 2010, under how to, tutorial
This how to will show you how you can create a simple 2D Map with Cells to place stuff on it. Just like the old school SimCity.
The first thing you need is an Activity with a SurfaceView and a Thread to trigger the drawing. Who doesn’t know these fundamentals, please read my series on 2d graphics first.
Lets start with the smallest unit for our map: the Cell.
Each Cell will have a background color and a unique ID.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.droidnova.android.games; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; /** * A part of the map. */ public class Cell { public int _id = 0; public int _backgroundColor = Color.GREEN; /** * Konstruktor. * @param id */ public Cell(int id) { _id = id; } /** * Draw the cell * * @param canvas Canvas to draw on. * @param paint Color of the "pencil". * @param x X coordinate. * @param y Y coordinate. */ public void draw(Canvas canvas, Paint paint, int x, int y) { paint.setColor(_backgroundColor); canvas.drawRect(x, y, x + CellMap._cellSize, y + CellMap._cellSize, paint); paint.setColor(Color.BLACK); canvas.drawText("" + _id, x + 1, y + 10, paint); } } |
On line 32 you see, how we draw the cell. The variable _cellSize is a static variable from CellMap, which will be introduced later. Everything else should be already known.
(continue reading…)
Ubuntu 9.10 with Eclipse and ADT – Problems
by Martin on Oct.26, 2009, under how to
Sometimes I am a bit over motivated. Thats the reason I upgraded my Ubuntu to 9.10 RC1 yesterday.
Nearly everything worked fine and after 90 minutes downloading and installing Ubuntu 9.10 booted the first time.
The disillusion followed today: What happened with my Eclipse installation?
The LogCat view has no logging type buttons, the Devices view has lost its button for stopping processes and some windows don’t accept a mouse click on their buttons (mostly the OK button). I have to press Enter to get them work.
After a short search I found this bug report for ubuntu: https://bugs.launchpad.net/ubuntu/+source/gtk+2.0/+bug/442078
A workaround is also mentioned there:
Create a launch script for eclipse with the following code:
#!/bin/sh GDK_NATIVE_WINDOWS=1 /path/to/eclipse
I am not sure if Ubuntu is responsible or Eclipse, but after using this launch script, everything worked fine.
Creating Sound Effects in Android: Part 1
by Stephen Flockton on Oct.20, 2009, under how to, tutorial
In today’s tutorial I am going to show you my method of creating, managing and using sound effects in Android. In this first part I’ll show you the basic method of encapsulating your sound management code. This method works best when you have a typical application, or simple game all in one thread.Part 2 will show you a more advanced way to manage your sound across multiple classes.
The method I use to play sounds is to use the Sound Pool classes rather then the Media Player classes that the Android dev-guide seems to suggest. While there is nothing wrong with using the Media Player classes for simple applications they did not provide the flexibility I needed.
(continue reading…)
How to: Create a splash screen
by Martin on Oct.14, 2009, under how to
Many Applications, mostly games, on the market show splash screens. With this screen they prompt a logo for the application and/or the author.
I will show you a short way to implement a splash screen which will occur on every startup, will stay for a number of seconds you can define, will close on touching the screen and will not reappear on pressing the back button.
I created an empty project named SplashScreen with the activity SplashScreen. This activity will display the splash screen, so we have to create a new activity which will be the first real view you want to display. In my case this activity is named MyApp.
(continue reading…)
Debugging in Android using Eclipse
by Jimmy on Sep.30, 2009, under how to
Android is a great platform which makes creating applications for mobile devices much easier than before. But in the progress of creating your applications you are bound to face bugs. For this purpose you need a good understanding of debugging your software. In this article I will explain how to debug your applications using the Eclipse IDE with the Android plugins.
We’ll use the Android Debug Bridge (ADB) which basically is a tool that runs on both your android platform (emulator or device) and your development computer. The ADB can be used via the command line interface, or just simply via Eclipse. We will be using Eclipse to debug since it’s much easier. If you are debugging on a device, you first have to enable USB debugging. Go to the settings of the machine > software > development > USB-debugging (or something like that, my phone is Dutch!)
(continue reading…)
Creating Game Menus in Android
by Stephen Flockton on Sep.30, 2009, under how to, tutorial
As requested here is a sample tutorial in how to create a menu system for games in Android. Sorry for the delay but this tutorials take time to code test and write up. Anyway I hope you find it useful.
Before I jump into the code I’m going to take a second to explain my way of coding menus in Android. As we all know Android is built on the concept of activities. If you have been following earlier tutorials you already know how to create activities which can display graphics and deal with player input. But what if you want several different screens, such as options or credits? You could code them all into one activity but you would end up with a bloated and hard to maintain class.
(continue reading…)
2D Sprite Animation in Android Addendum
by Stephen Flockton on Sep.23, 2009, under how to, tutorial
If you read my last tutorial entry here, then you may have come across a problem with the code.
If you load several large bitmaps using the BitmapFactory class to decode the bitmap you application will give you the dreaded force close dialogue box. A quick look in the logcat shows that a bitmap exceeds the virtual machine memory budget.
1 | ERROR/AndroidRuntime(750): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget |
You may wonder how its possible that a few PNG’s can take up all the 16 mb of memory allocation for an application.
(continue reading…)
How to create an option menu
by Martin on Aug.28, 2009, under how to
Today we learn how you can create an option menu for your application.
Lets start with an empty android project. The package name will be com.droidnova.android.howto.optionmenu and the activity will have the name SimpleOptionMenu.
Our activity should now look very familiar to us:
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.droidnova.android.howto.optionmenu; import android.app.Activity; import android.os.Bundle; public class SimpleOptionMenu extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } |
Display borders in TableLayout
by Martin on Mar.20, 2009, under how to
As the developer guide says that table border lines are not displayed there is only a dirty fix for this problem:
Give the TableLayout a background color, give the TableRow another background color and set margin to the TableRow. The amount of the margin is the amount of the “border”. Same for each View in the TableRow.
A “dirty” sample:
1 2 3 4 5 6 7 8 | <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:stretchColumns="*" android:background="#ff0000"> <TableRow android:background="#00ff00" android:layout_margin="2dip"> <Button android:id="@+id/button" android:text="+" android:background="#0000ff" android:layout_margin="2dip"/> <TextView android:text="@string/label" android:background="#0000ff" android:layout_margin="2dip"/> <TextView android:id="@+id/amount" android:background="#0000ff" android:layout_margin="2dip"/> </TableRow> |
Source: My answer on anddev.org forum
TableLayout supports column span
by Martin on Mar.13, 2009, under how to
While working on a little game to learn more about Intents, Activity life cycle and other stuff, I used the TableLayout for the first time. I realized that the auto-completion missed the attribute android:layout_span which is referred by the R.attr. The same with android:stretchColumns. As I read some minutes ago, some people still think column span is not possible – so I decided to publish a sample to prove them wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!-- snipped --> <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:stretchColumns="*"> <TableRow> <TextView android:id="@+id/info" android:layout_span="3" android:text="@string/info" android:layout_gravity="center_horizontal" /> </TableRow> <TableRow> <Button android:id="@+id/button_add" android:text="+" /> <TextView android:text="@string/label" /> <TextView android:id="@+id/amount" /> </TableRow> <!-- snipped --> </TableLayout> <!-- snipped --> |
If the auto-completion leaves out an attribute, there can be more attributes being unsupported, so always use the API as cross reference!