Skip to content

Instantly share code, notes, and snippets.

View omegasoft7's full-sized avatar
🎯

Farhad Sanaei omegasoft7

🎯
  • Germany, Hamburg
View GitHub Profile
@omegasoft7
omegasoft7 / SetID to View Programmatically
Created January 9, 2015 09:52
SetID to a View Programmatically
Google finally realized the need of generating unique IDs for programmatically created views...
From API level 17 and above, you can call
View.generateViewId()
Then use View.setId(int).
In case you need it for targets lower than level 17, here is its internal implementation in View.java you can use directly in your project, put it in your util class or somewhere:
@omegasoft7
omegasoft7 / Java to JavaScript.txt
Created December 19, 2014 15:33
Connect Java and JavaScript. Call a java method on Javascript codes that are running in webview
There is a hack:
Bind some Java object so that it can be called from Javascript with WebView:
addJavascriptInterface(javaObjectCallback, "JavaCallback")
Force execute javascript within an existing page by
WebView.loadUrl("javascript: var result = window.YourJSLibrary.callSomeFunction();
window.JavaCallback.returnResult(result)");
(in this case your java class JavaObjectCallback should have a method returnResult(..))
// //show after fully load of layers
ViewTreeObserver vto = myview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@Override
public void onGlobalLayout() {
//Do needs after load interface here
public static final String PATTERN_EMAIL = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
public static boolean isValidEmailAddress(String email) {
Pattern pattern = Pattern.compile(PATTERN_EMAIL);
return pattern.matcher(email).matches();
}
public static int colorFromIOS(double r, double g, double b, double a) {
int red = (int) (r * 255);
int green = (int) (g * 255);
int blue = (int) (b * 255);
int alpha = (int) (a * 255);
return Color.argb(alpha, red, green, blue);
}
public static class DateDiff {
public long seconds;
public long minutes;
public long hours;
public long days;
public DateDiff(long seconds, long minutes, long hours, long days) {
this.seconds = seconds;
this.days = days;
this.minutes = minutes;
public static void logOut(String string) {
if (string.length() > 3000) {
int length = string.length();
String str = string;
while (length > 3000) {
Log.w("MYAPP", str.substring(0, 3000));
str = str.substring(3000);
length = str.length();
@omegasoft7
omegasoft7 / MakeOrientationWithoutDestroying.java
Last active August 29, 2015 14:11
Make orientation without destroy and reload activity
//ON Activity
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//Landscape orientation
} else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
@omegasoft7
omegasoft7 / getPixcelsByDP.java
Last active August 29, 2015 14:11
Calculate Pixcels by getting DP
// this will calculate and return Pixel value by getting DP value
public static int getDPbyPixcel(int pix) {
return (int) (pix * HYGlobal.density + 0.5f);
}