advanced

Store images/files in database

SDK Version: 
M3

Hoever the practice is to store them normally and save the access route (Uri), sometimes it can be handy to store files/images completely in database.
In sqlite database there are only a few data types, so its easy to choose: files can be stored in a text as a ByteArray.

Lets see a sample code, where we download an image from the Internet then store it in the local database:

  1. //where we want to download it from
  2. URL url = new URL(IMAGE_URL);  //http://example.com/image.jpg
  3. //open the connection
  4. URLConnection ucon = url.openConnection();
  5. //buffer the download
  6. InputStream is = ucon.getInputStream();
  7. ByteArrayBuffer baf = new ByteArrayBuffer(128);
  8. //get the bytes one by one
  9. int current = 0;
  10. while ((current = bis.read()) != -1) {
  11.         baf.append((byte) current);
  12. }
  13.  
  14. //store the data as a ByteArray
  15. //db is a SQLiteDatabase object
  16. ContentValues dataToInsert = new ContentValues();                          

How to use canvas in your android apps - Part 2

SDK Version: 
M3

In the second part of this tutorial series, I will show you what you can do with canvas and paint.


If you complete the first part, your onDraw method in Panel class is similar as this:

  1.         @Override
  2.         public void onDraw(Canvas canvas) {
  3.                
  4.                 Paint paint = new Paint();
  5.                
  6.  
  7.                 Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
  8.                                 R.drawable.kangoo);
  9.                 canvas.drawColor(Color.BLACK);
  10.                 canvas.drawBitmap(kangoo, 130, 10, null);
  11.                
  12.         }
  13.          

We will use the the Paint type object this time. You can use it to draw lines, circles and text on your canvas. For example this two lines will draw a red circle for you.

  1. paint.setColor(Color.RED);

How to use canvas in your android apps - Part 1

SDK Version: 
M3
If you want to use a custom layout with a canvas in your application, this tutorial may be useful to you. We will draw a kangoo at the end of this tutorial.


There is a cool article on site Custom View - HorizontalSlider, what you can use to understand the following code:

  1. [...]
  2. <com.helloandroid.canvastutorial.Panel android:id="@+id/SurfaceView01" android:layout_width="wrap_content"
  3.  
  4. android:layout_height="wrap_content" android:maxHeight="40dip">
  5. </com.helloandroid.canvastutorial.Panel>
  6. [...]

This is your main.xml.

Your main class extends Activity and similar as the following code snip:

  1. public class Canvastutorial extends Activity {
  2.     /** Called when the activity is first created. */
  3.     @Override

Hello Baby widget feature update - notification tutorial

SDK Version: 
M3

We have updated our Hello Baby widget, with a notification feature. So I'm using this opportunity, to show you how to use notifications with widgets.

Hello readers, my name is Tamas, and I'm the newest member of the HelloAndroid.com team :)
Today we decided to update our Hello Baby widget, with a notification feature. So I'm using this opportunity, to show you how to use notifications with widgets.
The plan was to alert the user with a status bar notification 1,2,3 and 7 days before the days-left counter reaches 0.

How to optimize your android app's performance

SDK Version: 
M3

So here's how you can optimize, analyze your current application using TraceView.
Open your Project, and after onCreate(), or anywhere you want to start Log tracing, paste the following lines:

  1. /* start tracing to "/sdcard/filename.trace&quot; */
  2. Debug.startMethodTracing("filename");

It is very important to stop tracing after you have done debugging. To do that, place these lines before onCreate().
  1. public void onDestroy() {
  2.         // stop tracing
  3.         Debug.stopMethodTracing();
  4.     }

Now run your Project, do anything you'd like to optimize, and quit. Open up DDMS File Explorer, and download /sdcard/filename.trace to your hard disk drive.

Go to your Android SDK folder, open Tools directory, and run ./traceview filename.trace

Here's how it looks like:

How to use custom designed numbers in your android widget

SDK Version: 
M3
If you don't want to download external fonts into your application, this article will be useful to you. You can draw your own numbers for example in gimp from 0 to 9. Copy thoose numbers in to your project folder's res/drawable subfolder. Each number is a new png (for example: one.png, two.png...). Then don't forget to refresh this directory in eclipse.

In my last project I wrote an easy method, what I used to draw single numbers on a widget.

How to add external 3D fonts to your Android applications

SDK Version: 
M3
Step 1.
First, go to google.com, and find an eye-candy font, that you like the most. Look for „free 3d fonts”. I'm going to use this one: http://www.urbanfonts.com/fonts/Xtrusion.htm
After you have downloaded it, unzip if it is zipped.

Step 2.
Go to your project's location, open the „assets” folder, and create a new folder called „fonts”.
Copy and Paste your „xy.ttf” font to this new directory.

Step 3.
If you are using Eclipse, don't forget to refresh your project's directory tree, make sure your SDK can see your font file.

  1. /* Setting up fonts */
  2. Typeface fontFace = Typeface.createFromAsset(getAssets(), "fonts/xtrusion.ttf");
  3. Typeface fontFace2 = Typeface.createFromAsset(getAssets(), "fonts/other.ttf");
  4. TextView someText = (TextView)findViewById(R.id.TextView01);

How to configure widget-settings by clicking on widget developer tutorial

SDK Version: 
M3

Sometimes you need to change your widget settings after you put one to home screen. In my new tutorial I will show an easy way to solve this problem.

ProgressBar updating using Message Handler

SDK Version: 
M3

You're going to learn how to make a progressbar, which gives you a feedback what's going on in the background.

Let say you have a LoadingScreen activity (loadingscreen.xml layout), and a Main activity (main.xml).

You'd like to do some job in the Main activity, and update the progressbar status at the same time. To do this, we are going to use Messages. Although, you can update you're progressbar by sending the progressbar instance itself to your Main activity, but that's obviously not the prettiest solution.

What we going to do is:

1. Declaring a progressbar instance in LoadingScreen Activity:

So in your LoadingScreen.java, after Oncreate(), you should add something like this line to your codeline: (don't forget to add a progressbar to your layout first!)

  1.   ProgressBar pb = (ProgressBar)findViewById(R.id.ProgressBar01);

2. Setting up a Handler to handle Messages from outside:

How to download file/image from url to your device

SDK Version: 
M3

We wrote a code for download image from a website to the phone. You can work with images (and files) in your applications, but this article may be useful for java developers too.

Syndicate content