Posts

Showing posts from 2012

Change two colors by percentage

In Android, it is not good when most of the views has alpha value. Therefore, it is better by changing color of the view. Below is the color interplation between a and b with given proportion: private float interpolate(float a, float b, float proportion) { return (a + ((b - a) * proportion)); } /** Returns an interpoloated color, between a and b */ private int interpolateColor(int a, int b, float proportion) { float[] hsva = new float[3]; float[] hsvb = new float[3]; Color.colorToHSV(a, hsva); Color.colorToHSV(b, hsvb); for (int i = 0; i < 3; i++) { hsvb[i] = interpolate(hsva[i], hsvb[i], proportion); } return Color.HSVToColor(hsvb); } Thanks to: http://stackoverflow.com/questions/4414673/android-color-between-two-colors-based-on-percentage

Change XML Layout automatically when orientation happens

Image
Most of Android developers know that Android has lots of unique and useful features. One of them is changing the layout based on current orientation. But it seems that it is not easy to manage what to do before and after changing the layout. Before manage/control the layout, you must have two layouts, landscape (in layout-land/ folder) and portrait (in layout/ folder). Then, we start to manage/control the layouts. If you don't want to manage/control all stuffs, just let Android do it for you, by doing: Remove android:configChanged="orientation" of your activity. This will cause that you don't receive onConfigurationChanged event. By doing this, there are some pros and cons: Pros: Android will reset the ContentView when onConfigurationChanged (we can't see it anymore). We just prepare the layouts easily. Cons: All parameters/properties will be reset when Android resets the ContentView. In the first step, it is hard to ...

Customize view using XIB

Image
This is the example of how to customize UITableViewCell using XIB. 1. Create your XIB: 2. Load XIB and create it as a view: 3. Done

Force using english localized if current localized does not exists

Create a class: CLocale.h #undef NSLocalizedString#define NSLocalizedString(key, comment) \ [CLocale localizedString:(key)] @interface CLocale : NSObject + (NSString*) localizedString:(NSString*)key; @end CLocale.m #import "CLocale.h" @implementation CLocale + (NSString*) localizedString:(NSString*)key { NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"]; NSString* currentLocale = [languages objectAtIndex:0]; NSString* localized = [[NSBundle mainBundle] localizedStringForKey:key value:@"" table:nil]; // if key and localized are same, also it is not english, // then force to use english language if ([localized compare:key] == NSOrderedSame && [currentLocale compare:@"en"] != NSOrderedSame) { NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"]]; localized = NSLocalizedStringFro...

What should every programmer know about web development?

The idea here is that most of us should already know most of what is on this list. But there just might be one or two items you haven't really looked into before, don't fully understand, or maybe never even heard of. Interface and User Experience Be aware that browsers implement standards inconsistently and make sure your site works reasonably well across all major browsers. At a minimum test against a recent  Gecko  engine ( Firefox ), a WebKit engine ( Safari ,  Chrome , and some mobile browsers), your supported  IE browsers  (take advantage of the  Application Compatibility VPC Images ), and  Opera . Also consider how  browsers render your site  in different operating systems. Consider how people might use the site other than from the major browsers: cell phones, screen readers and search engines, for example. — Some accessibility info:  WAI  and  Section508 , Mobile development:  MobiForge . S...

Cannot drop index needed in a foreign key constraint

When you receive the message "MySQL Cannot drop index needed in a foreign key constraint" You have to drop the foreign key. Foreign keys in MySQL automatically create an index on the table (There was a SO Question on the topic). ALTER TABLE mytable DROP FOREIGN KEY mytable_ibfk_1; Then, try again! http://stackoverflow.com/questions/8482346/mysql-cannot-drop-index-needed-in-a-foreign-key-constraint

Share file between activites

1. Set this same shared user id in manifest <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.a" android:versionCode="1" android:versionName="1.0" android:sharedUserId="com.example.id"> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.b" android:versionCode="1" android:versionName="1.0" android:sharedUserId="com. example.id"> 2. Get another app’s context with createPackageContext() Context otherContext = context.createPackageContext("com.example.a", 0); AssetManager am = otherContext.getAssets(); http://joshzhchen.blogspot.com/2010/09/android-application-share-user-id.html http://stackoverflow.com/questions/5493396/how-to-reference-android-assets-across-packages

Using assets from any apk

Android allows a nice feature of adding your own files in the final apk package. These files are called “assets”. For adding assets to your project you simply add your files to the “prj_name/assets” directory. The files you add in the assets directory will be zipped in the final “apk” project, along with the executable-files and the other resources. One has two methods to access the asset files: use android.content.res.AssetManager Note that this will limit your asset files to 1Mb, at least for Android 1.5 release. use java.util.zip  This has no limitations to size, but will access all resources in the zip, so one has to filter the results. The implementations at a glance: android.content.res.AssetManager AssetManager am = getResources().getAssets(); // get the local asset manager InputStream is = am.open( asset ); // open the input stream for reading while ( true ) // do the reading { int count = is.read(buf, 0, MAX_BUF); // .. use the d...

Repair HFS+ when your BootCamp is error

Restart your Mac. As soon as it turns on, even before you hear it go bong, hold down Cmd+S. The system will boot showing a lot of text. If it doesn't and you see the Apple logo, you've waited too long. Reboot and try again. When at the prompt type  fsck -f . If changes are successfully made it will repeat the process then say "Modifications have been made". Then simply type in  reboot  and you should be able to partition your drive successfully. http://hints.macworld.com/article.php?story=20080120064811786

How to remove default/preloaded applications like browser,camera,contacts...

Suppose u have developed a new contact application which is much better than the preloaded contact application in android..so when u want to install it eclipse will throw u bunch of error saying "Please uninstall the contact application first by adb uninstall "package name". When u tried to do that...Alas...it did not work...saying FAILED The reason for this is that u don't have the root access and u have only read only access..So to change the access and remove the contacts(any application) follow the below instructions.. Start your emulator Execute "adb shell" in on terminal from SDK_ROOT/tool folder Mount your system folder with read/write permission mount -o remount,rw /dev/block/mtdblock3 /system Go to system/apps folder cd /system/apps Remove APK rm defaultapp.apk   Bingo Check your emulator.The default application is gone.Now u can install your application with eclipse. N.B.:This is required to remove the apps already present in emulator...If u want ...

Speed up SQLite insert

Using android.database.DatabaseUtils.InsertHelper class. Sample code: SQLiteDatabse db = getWriteableDatabase(); //use the db you would normally use for db.insert, and the "table_name" //is the same one you would use in db.insert() InsertHelper iHelp = new InsertHelper(db, "table_name"); //Get the indices you need to bind data to //Similar to Cursor.getColumnIndex("col_name"); int first_index = iHelp.getColumnIndex("first"); int last_index = iHelp.getColumnIndex("last"); try{ db.beginTransaction(); for(int i=0 ; i<num_things ; ++i) { //need to tell the helper you are inserting (rather than replacing) iHelp.prepareForInsert(); //do the equivalent of ContentValues.put("field","value") here iHelp.bind(first_index, thing_1); iHelp.bind(last_index, thing_2); //the db.insert() equilvalent iHelp.execute(); } db.setTransactionSuccessfu...

Set up ADB environment in Mac OSX

Open Terminal Type cd ~ this will take you to your home directory Type touch . profile this will create a hidden file named profile Type open - e . profile this will open the file you just created in TextEdit In the file, type export PATH = $ { PATH }:/ pathToTheAndroidSdkFolder / android - sdk - mac_86 / platform - tools Save file, close TextEdit, Quit Terminal, and Relaunch Terminal NOTE: By creating an environment variable you won't need to cd to the Android/tools folder every time you want to run ADB Reference:  http://esausilva.com/2010/10/02/how-to-set-up-adb-android-debug-bridge-in-mac-osx/

Get mimetype from header file

// return: jpeg, png, bmp, unknown public static String getMimeTypeFileByHeader(String realPath) { File f = new File(realPath); try { InputStream in = new FileInputStream(f); byte[] HEADERJPEG = new byte[] {(byte) 0xFF, (byte) 0xD8, (byte) 0xFF}; byte[] HEADERBMP = new byte[] { 0x42, 0x4d }; byte[] HEADERPNG = new byte[] { (byte) 0x89, 0x50, 0x4e }; byte[] header = new byte[3]; int h = in.read(header); if (h==3 && Arrays.equals(header, HEADERJPEG)) { return "jpeg"; } if (h==3 && Arrays.equals(header, HEADERPNG)) { return "png"; } byte[] header2 = new byte[2]; for (int i=0; i<2; i++) header2[i] = header[i]; if (Arrays.equals(header2, HEADERBMP)) { return "bmp"; } } catch (Throwable e) {} return "unknown...