Created
April 13, 2016 02:51
-
-
Save joeyjiron06/027e053ea3eb32580172f7874c4e0669 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Android Utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class AndroidUtils { | |
private AndroidUtils() {} | |
public static void writeRawResourceToFile(Context context, int resourceId, String directory, String filename) { | |
// open raw resouce is taken from 'src/BUILD_TYPE/res/raw' where BUILD_TYPE is usually main | |
InputStream indexFileInputStream = context.getResources().openRawResource(resourceId); | |
//create a file with the directory and filename | |
File file = new File(directory, filename); | |
// use Java Utils to copy the stream to the file. https://gist.github.com/joeyjiron06/79ee6057780ffc0b681f | |
Utils.copyInputStreamToFile(indexFileInputStream, file); | |
} | |
public static void setBackgroundResource(View view, int resourceId) { | |
view.setBackgroundResource(resourceId); | |
// fix padding issue | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { | |
int paddingBottom = view.getPaddingBottom(); | |
int paddingTop = view.getPaddingTop(); | |
int paddingRight = view.getPaddingRight(); | |
int paddingLeft = view.getPaddingLeft(); | |
view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom); | |
} | |
} | |
public static void hideKeyboard(Activity activity) { | |
// hide keyboard | |
View view = activity.getCurrentFocus(); | |
if (view != null) { | |
hideKeyboard(view); | |
} | |
} | |
public static void hideKeyboard(View view) { | |
InputMethodManager imm = (InputMethodManager)view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); | |
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); | |
} | |
private float sp2px(float px) { | |
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, px, getContext().getResources().getDisplayMetrics()); | |
} | |
private float dp2px(float px) { | |
Resources r = getContext().getResources(); | |
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, px, r.getDisplayMetrics()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment