onRestoreInstanceState() is called only when recreating activity after it was killed by the OS. Such situation happen when:

  • orientation of the device changes (your activity is destroyed and recreated)
  • there is another activity in front of yours and at some point the OS kills your activity in order to free memory (for example). Next time when you start your activity onRestoreInstanceState() will be called.

如果数据内容改变,应该调用父窗口的UpdataData 或者是Invalidata函数,通知窗口内容已经发生了改变,系统自己来调用OnDraw函数处理这个改变消息。

Invalidate(Rect),只渲染Rect区域.Rect是一个矩形, Draw是在Invalidate调用时它才会被触发.


Setup the Preferences

Android preferences is a key/value entries that store data that can be specific to a certain activity or shared among all activities within the application. 

the data are stored in a xml file.

  1. Preferences can be retrieved only by a single activity, we do it like this: we get a SharedPreferences object by calling getPreferences(int mode)

SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);

SharedPreferences.Editor editor=prefs.edit(); editor.putString(“pref 1”, “some text”); editor.commit();

  • the mode value can be one of the following:
      1. Context.MODE_PRIVATE (0): a file creating mode that makes the created file only accessible by applications with the same user ID (access the file from the same application context, will desctribe later).
      2. Context.MODE_WORLD_READABLE (1): file mode makes the file readable from other applications.
      3. Context.MODE_WORLD_WRITEABLE (2): file mode allows other applications to write to the file.

we get an instance of SharedPreferences.Editor and write the preference value with editor.putString(String key, String value) method.
shared preferences allows you to insert preferences using the following methods:

  1. editor.putBoolean(String key, boolean value).
  2. editor.putFloat(String key,float value).
  3. editor.putInt(String key, int value).
  4. editor.putLong(String key, long value)
  5. editor.putString(String key, String value)

then we call edit.commit() to save the preferences to the file. commit returns a boolean indicating the result of saving, true if successful and false if failed.

It can be shared and retrieved among all activities within the application, or through all applications on the device. To save preferences that can be retrieved from all activities in the application we do it like this:

SharedPreferences prefs= getSharedPreferences("demopref", Context.MODE_WORLD_READABLE);
        SharedPreferences.Editor editor=prefs.edit();
        editor.putString("demostring", "hello");

we give our preferences file a name (demopref in this case) so that other activities can reference that preferences file.
Sharing preferences across applications: 

We can store preferences in one application and read them in another application, this is done reading the preferences file by loading them through the first application’s context. let’s assume we have two applications:

  1. Application 1 with package name “com.mina.prefdemo“.
  2. Application2 with package name “com.mina.demoapp“.
If application1 creates a preferences file with the name “demopref” and inserts a String preference with the key/value “demostring/hello”.
now we access this file and value from application 2 like this:
Context con;
  try {
   con = createPackageContext("com.minasamy.prefdemo", 0);
   SharedPreferences pref=con.getSharedPreferences("demopref", Context.MODE_PRIVATE);
   String x=pref.getString("demostring", "not found");
  } catch (NameNotFoundException e) {
   Log.e(Tag, e.toString());

// Setup the Preferences for this application, by class.
    Preferences prefs = Preferences.userNodeForPackage(MainClass.class);

   // Retrieve some preferences previously stored, with defaults in case this is the first run.
    String text = prefs.get(“A”“a”);
    String display = prefs.get(“B”“b”);

// Assume the user chose new preference values: Store them back.


Theme.Light android doesn’t work

“If you’re developing for API level 11 and higher, you should instead use Theme.Holo or Theme.DeviceDefault ”

We want to create a transparent activity in our app but we also want our app to use a Holo theme when run on HoneyComb +.  However, Theme.Translucent.NoTitleBar does NOT utilize the nice new Holo theme.

So your app would use Holo.Light, but your transparent activity would use the older default theme. This will look very unprofessional.

SetApplicationAttribute(android:theme, "@android:style/Theme.Holo.Light")

The Holo themes’ Transparent effect can be gained by setting your application theme to


But again, this has a problem. Run this on a device operating on an Android version less than HoneyComb (i.e. gingerbread) and you get an error as the theme simply doesn’t exist.



Anything added to the theme.xml in the values folder will be used as default. Anything added to the theme.xml in the values-v11 will be use when android v11 (honeycomb) or above is in use.


<?xml version="1.0" encoding="utf-8"?>

        name="MyThemeTransparent" parent="android:style/Theme.Translucent.NoTitleBar">


<?xml version="1.0" encoding="utf-8"?>

        name="MyThemeTransparent" parent="@android:style/Theme.Holo.Light.Panel">

hsv wheel color picker

via http://chiralcode.com/color-picker-for-android/

Single color picker

Color Picker is based on HSV color model. Using color wheel in the middle, user can adjust hue and saturation. Arc on the right side allows user to change value of the selected color. Arc on the left side shows currently selected color. This is how it looks like:

Android Color Picker Android Color Picker Android Color Picker

Multi color picker

More advanced version allows to pick several colors at once. It is really not easy to compose a palette of eye-catching colors. Changing only the hue while keeping saturation and color value on the same level gives nice effects and is easy to achieve.

Android Color Picker Android Color Picker Android Color Picker

Project is available at GitHub: https://github.com/chiralcode/Android-Color-Picker/


UberColorPicker Demo

UberColorPicker Demo - screenshot thumbnail UberColorPicker Demo - screenshot thumbnail

Superdry Color Picker

Superdry Color Picker Demo - screenshotSuperdry Color Picker Demo - screenshot


set image button background image for different state

I want that if I pressed image button then image should change from normal to pressed until I press other button but it doesn’t happen.

I have set normal image in the imagebutton background and I am trying to change image(pressed) fromonclick method, but it doesn’t change.

create an xml in your drawable like this

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        android:drawable="@drawable/btn_sendemail_disable" />
        android:drawable="@drawable/btn_send_email_click" />
        android:drawable="@drawable/btn_sendemail_roll" />
        android:drawable="@drawable/btn_sendemail" />

how colors work in android.

I have this color set as background of my LinearLayout and I get a background gray with some transparency.

<gradient android:startColor="#b4555555" android:endColor="#b4555555"
 android:angle="270.0" />

If I remove the last two chars (55) I get a solid color, loosing the transparency.

Android uses Hex ARGB values, which are formatted as #AARRGGBB. That first pair of letters, the AA, represent the Alpha Channel. You must convert your decimal opacity values to a Hexdecimal value. Here are the steps:

Alpha Hex Value Process

  1. Take your opacity as a decimal value and multiply it by 255. So, if you have a block that is 50% opaque the decimal value would be .5. For example: .5 x 255 = 127.5
  2. The fraction won’t convert to hex, so you must round your number up or down to the nearest whole number. For example: 127.5 rounds up to 128; 55.25 rounds down to 55.
  3. Enter your decimal value in a decimal to hexadecimal converter, like thishttp://www.binaryhexconverter.com/decimal-to-hex-converter, and convert your values
  4. If you only get back a single value, prefix it with a zero. For example, if you’re trying to get 5% opacity and your going through this process you’ll end up with the hex value of D. Add a zero in front of it so it appears as 0D.

Here’s a correct table of percentages to hex values. E.g. for 50% white you’d use #80FFFFFF.

  • 100% — FF
  • 95% — F2
  • 90% — E6
  • 85% — D9
  • 80% — CC
  • 75% — BF
  • 70% — B3
  • 65% — A6
  • 60% — 99
  • 55% — 8C
  • 50% — 80
  • 45% — 73
  • 40% — 66
  • 35% — 59
  • 30% — 4D
  • 25% — 40
  • 20% — 33
  • 15% — 26
  • 10% — 1A
  • 5% — 0D
  • 0% — 00

eclipse debug yellow highlight

When the debugger stops at a breakpoint, you can continue the debugger session by selecting the Step Over option from the Debug view toolbar (see Figure 9). This steps over the highlighted line of code and continues execution at the next line in the same method (or it will continue in the method from which the current method was called). The variables that are changed as a result of the last step are highlighted in a color (default is yellow).

convert getpixel() to hex in android

int c = bitmap.getPixel(x, y);
returns a 7 or 8 digit number like -14438067 for green for example.
The mask makes sure you only get RRGGBB, and the %06X gives you zero-padded hex (always 6 chars long):

String hexColor = String.format(“#%06X”, (0xFFFFFF & intColor));
Integer intColor = -16895234;
String hexColor = “#” + Integer.toHexString(intColor).substring(2);

From developer.android.com/reference/android/graphics/Color.html, “Colors are represented as packed ints, made up of 4 bytes: alpha, red, green, blue…The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue. Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution".

when to use handler in android

If the programmer does not use any concurrency constructs, all code of an Android application runs in the main thread and every statement is executed after each other.
To provide a good user experience all potentially slow running operations in an Android application should run asynchronously, e.g. via some way of concurrency constructs of the Java language or the Android framework. This includes all potential slow operations, like network, file and database access and complex calculations.

1. Using Java threading in Android
Android supports the usage of the Thread class to perform asynchronous processing.

Android also supplies the java.util.concurrent package to perform something in the background, e.g. using the ThreadPools and Executor classes.

If you need to update the user interface from a new Thread, you need to synchronize with the user interface thread.
2.Concurrency constructs in Android
Android provides additional constructs to handle concurrently in comparison with standard Java. You can use the android.os.Handler class or the AsyncTasks classes. More sophisticated approach are based on the Loader class, retained Fragments and services.

Purpose of the Handler class
The Handler class can be used to register to a thread and provides a simple channel to send data to this thread.

A Handler object registers itself with the thread in which it is created. For example, if you create a new instance of the Handler class in the onCreate() method of your activity, the resulting Handler object can be used to post data to the main thread.

The data which can be posted via the Handler class can be an instance of the Message or the Runnable class.

Android touch

try to use itemized overlay to draw image and to draw at certain point override the OntouchEvent Method and pass it the lat and long and you will get Image on the touched location if you need some example u can see here

The Android standard View class support touch events. You can react touch events in your custom views and your activities. Android supports multiple pointers, e.g. fingers which are interacting with the screen.

Android touch

The base class for touch support is the MotionEvent class which is passed to Views via the onTouchEvent() method. To react to touch events you override the onTouchEvent() method. The MotionEvent class contains the touch related information.e.g. the number of pointers, the X/Y coordinates and size and pressure of each pointer.

This method returns true if the touch event has been handled by the view. Android tries to find the deepest view which returns true to handles the touch event. If the view is part of another view (parent view), the parent can claim the event by returning true from the onInterceptTouchEvent() method. This would send an MotionEvent.ACTION_CANCEL event to the view which received previously the touch events.

To react to touch events in an activity, register an OnTouchListener for the relevant Views.

1. Five colour brushes.
2. Three types of drawing pencils.
3. Beautiful colours are given to draw a nice painting.

You can use different shapes like: circle, square, rectangle, cubes,triangles, lines, arrow, stars etc… Press on color icon to set the background color.

To color an image on user’s tap, to fill a given image with some color

– Implement and supply the source code for a fast flood fill algorithm for the Android platform that can instantly fill a region bounded by one specified colour with the desired colour
– Ensure that the code functions on both phone and tablet Android devices with various screen sizes
– Write clean, modular, robust code to implement the desired requirements
– Provide a test harness and source to verify the performance of the code on the target platform

can‘t find calss on path data/app/xxx.apk

To fix this, right-click your project, properties, build path and go to export tab. There make sure “Android private libraries” are checked. A clean and rebuild should put you on track …
Right-click the project in Eclipse’s Package Explorer and choose Properties.
Choose Java Build Path
Choose Order and Export
Check the boxes next to Android Private Libraries and Android Dependencies
After making these changes, do a clean and then rebuild.

With this change, I was able to build an APK that installed and ran on an Android device.


Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......







Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision


Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness



Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

John's Weblog

Just another WordPress.com weblog

I Am That I Am

Chasing tomorrow's sunrise.