Posts

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