How to customize Toasts

SDK Version: 

Customizing Toasts

A toast notification is a message that pops up on the surface of the window. It only fills the amount of space required for the message and the user's current activity remains visible and interactive. The notification automatically fades in and out, and does not accept interaction events.

The Basics

You can create customized Toasts and setting its display position, as you like.
Here's a little code snippet to demonstrate how it works:

  1.        String text = “example toast text!;
  2.        Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
  3.        /* Positioning your Toast */
  4.        int offsetX = 0, offsetY = 0;
  5.        toast.setGravity(Gravity.BOTTOM, offsetX, offsetY);

Creating a Custom Toast View

WebView with zoom controls, image from SDCARD

SDK Version: 

The Problem:
When I'm trying to load an image (tried both gif and png) from my assets directory, into a webview.
In my assets folder, I have an image called myImage.gif
Here's my wrong code:

  1. WebView data = (WebView) findViewById(
  2. data.loadData("<IMG HEIGHT=\"42px\" WIDTH=\"42px\" SRC=\"file:///android_assets/myImage.gif\" />", "text/html",  "UTF-8");

All that happens when I try it, is that I get a blank screen with a
empty 42px/42px box.
In logcat, I see the message:
03-30 00:21:14.398: DEBUG/WebCore(214): Console: Not allowed to load
local resource: file:///android_assets/myImage.gif line: 0 source:

The Solution:
Even though, I'm going to load the image file from the filesystem (SDCARD or Phone Storage), instead of Assets folder, I've found a solution for this, and you don't even have to care about ContentProviders... (Which is also a good solution, but this is much easier & quicker in this case.)

Calling system settings from an Android app - GPS example

SDK Version: 
This tutorial shows how to redirect the user to a system settings screen asking to modify some settings the application depends on. We will make a specific example with GPS: The application can be used only if GPS is available.

The android systems GPS setting screen can be called just like any other Activities:

  1. startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);

To check GPS availability use the code code below: (the Activity must implement LocationListener)

Using ksoap2 for android, and parsing output data

SDK Version: 

So the other day, I was asked to check out how we could use soap on Android, preferably with ksoap2 for android, and a public SOAP Web Service. For the latter the TopGoalScorers web service was chosen.

How to send email from your application

SDK Version: 

Today we'll create an easy email sender application.
First of all we need to create a layout to set the address, the subject and email body box.

  1. <?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@
  3. +id/LinearLayout01" android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  7. xmlns:android=""
  9. android:orientation="vertical"><LinearLayout android:id="@+id/LinearLayout02";
  11. android:layout_width="wrap_content" android:layout_height="wrap_content"
  13. android:orientation="horizontal"><EditText android:layout_width="wrap_content"
  15. android:layout_height="wrap_content" android:width="170dip" android:id="@
  17. +id/emailaddress"></EditText><TextView android:layout_width="wrap_content"
  19. android:layout_height="wrap_content" android:id="@+id/emailaddress"
  21. android:text="Email address"></TextView>
  22. </LinearLayout>
  25. <LinearLayout android:id="@+id/LinearLayout03";

How to play video and audio on Android Part 2.

SDK Version: 

In this part, I'm going to focus on MediaPlayer. I'm going to show you, how to stop it properly, and how to create a media player more easily.
The default behavior is the same, with every activity, after onPause and onStop, if the activity is not destroyed, it will continue to run in the background, until it is killed so the system can free up the memory it is using. So until the activity is destroyed, the mediaplayer will continue to occupy space in the phones memory.

How to avoid OutOfMemory Error / OOM (The Ugly Truth Revealed)

SDK Version: 

ERROR/dalvikvm-heap(4204): 691456-byte external allocation too large for this process.
01-25 22:13:18.694: ERROR/(4204): VM won't let us allocate 691456 bytes 01-25 22:13:18.694

If you have ever got the message above, you are at the right place.

First of all the reason:
Heap size != External Memory size

Dalvik's external memory is limited to ~ 4MBs for each process (That is the Ugly Truth). If it overflows, you get the BitmapFactory.DecodeFile Error.
That's why even if you have like 2MB-s heap memory free, VM won't let you allocate ~700KB.
Since you can't modify external memory's size, you have to reduce your memory usage. This is the only solution I have, but it really works at least.

The solution:

Let say you have a nice big Gallery with lots of large images. An ImageAdapter class will provide all of the images that you are going to use. Therefore you'll need a List of ImageViews.

How to display a custom dialog in your Android application

SDK Version: 

How to display a custom dialog in your Android application

Yesterday Jozsi showed you, how to make an alert dialog, today I'm going to show you, how to make a custom dialog/popup window.
Sometimes, it's better to make your own dialog, because this way, you can display whatewer you want., the way you want it.
First, make your own layout, with the needed elements. Here, I'm going to use two buttons, a textview inside a scrollview, and an imageview...

Connecting to MySQL database

SDK Version: 
The most spread method to connect to a remote MySQL database from an android device, is to put some kind of service into the middle. Since MySQL is usually used together with PHP, the easiest and most obvious way to write a PHP script to manage the database and run this script using HTTP protocol from the android system. mysql logo

We can code the data in JSON format, between Android and PHP with the easy to use built in JSON functions in both languages.

I present some sample code, which selects data from a database depending on a given condition and creates a log message on the android side with the received data.

Lets suppose that we have a MySQL database named PeopleData, and a table int created, with the following SQL:

  1. CREATE TABLE `people` (

How to avoid OutOfMemory (OOM) Exception using Bitmaps (Solved)

SDK Version: 

Step 1.
First thing I want you to think about is that do You really need to use Bitmaps?
If the answer is No, go to step 2. :) Otherwise, think again.
Ok, Let me explain it. I needed Bitmaps to get the width of the images, to create an ImageAdapter for my Gallery. As far as I know, there's no other way to do that. So I used Bitmaps, and even if I recycled them, once in a while it stopped with the message: DDMS: OutOfMemory .. Phone: Force Close..
Not to speak about how laggy your app will be if you are using Bitmaps..
I've spent 2 weeks to find a solution for this problem, but I couldn't find any usable of them. Then I thought, I try not using Bitmaps anymore. So I have to find another way to get the width of images. Unfortunately, it's just a workaround, but it works so who cares, right?
I can determine widths with a PHP script, store it in my existing database. After I did that, everything was working properly.

Syndicate content