Skip to content

Instantly share code, notes, and snippets.

View sunwicked's full-sized avatar
🏠
Working from home

sunny sunwicked

🏠
Working from home
View GitHub Profile
@sunwicked
sunwicked / Get Apk Details
Last active December 18, 2015 04:39
Finding out an Android apk's details: package name, version code, version name .. etc.
/**
* Used to find details of an apk file placed on the
* device, requirements are location of the file.
* PackageInfo gives details like package name, version code, version name .. etc
**/
String srcApk = Environment.getExternalStorageDirectory()+"/your.apk";
PackageInfo pf = getPackageManager().getPackageArchiveInfo(srcApk, PackageManager.GET_META_DATA);
@sunwicked
sunwicked / LruCache in a ViewPager
Last active December 19, 2015 16:18
Implementation of Number Pager Adapter with shell LruCache for caching of imgae. #Currently only memory caching is implemented Things to do : #Add file cache
public class NumberPagerAdapter extends PagerAdapter {
View v;
private LayoutInflater inflater;
private Context contxt;
private LruCache<String, Bitmap> mMemoryCache;
@sunwicked
sunwicked / Overlay Two Images
Created July 12, 2013 10:03
A small snippet for overlapping images and merging them into one. Adjust size and position of bitmap as you want
public Bitmap overlay(Bitmap baseBitmap, Bitmap bmp2) {
Rect dst = new Rect();
final Bitmap bitmapMan = Bitmap.createBitmap(baseBitmap.getWidth(),
baseBitmap.getHeight(), baseBitmap.getConfig());
final Canvas canvas = new Canvas(bitmapMan);
canvas.drawBitmap(baseBitmap, new Matrix(), null);
dst.set(bitmapMan.getWidth() / 4, bitmapMan.getHeight() / 4,
bitmapMan.getWidth() * 3 / 4, bitmapMan.getHeight() * 3 / 4);
@sunwicked
sunwicked / location of a Toast on-screen
Created August 11, 2013 11:53
Re-position the location of a Toast on-screen
Toast t = Toast.makeText(this, "Top left!", Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
t.show();
@sunwicked
sunwicked / mix intents
Created October 5, 2013 13:14
Mixing intents from manning android 50 hacks https://github.com/Macarse/50AH-code
public void onPickBoth(View v) {
Intent pickIntent = new Intent(Intent.ACTION_GET_CONTENT);
pickIntent.setType("image/*");
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Intent chooserIntent = Intent.createChooser(pickIntent,
getString(R.string.activity_main_pick_both));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent });
@sunwicked
sunwicked / get dimesnions
Created December 24, 2013 10:06
Get dimensions of a view after measurement of view is completed , solves getting view's dimensions as 0 in onCreate()
//root of layout
final View view = findViewById(R.id.root);
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
ViewTreeObserver vto = view.getViewTreeObserver();
width = profile_Lin.getWidth();
height = profile_Lin.getHeight();
@sunwicked
sunwicked / Color to Graysacle
Created December 24, 2013 10:08
Converting images to grayscale
// making drawable grey
ColorMatrix grayMatrix = new ColorMatrix();
grayMatrix.setSaturation(0);
ColorMatrixColorFilter grayscaleFilter = new ColorMatrixColorFilter(grayMatrix);
//getting a drawable
Drawable d = greyBtn.getBackground();
d.setColorFilter(grayscaleFilter);
greyBtn.setBackgroundDrawable(d);
@sunwicked
sunwicked / dp to pixels
Created May 12, 2014 09:23
Converting dp units to pixel units
// The gesture threshold expressed in dp
private static final float GESTURE_THRESHOLD_DP = 16.0f;
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
mGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);
// Use mGestureThreshold as a distance in pixels...
@sunwicked
sunwicked / gist:00436948fa89e5a994bb
Created February 15, 2015 19:32
Using color Tint
public static void tintAndSetCompoundDrawable (Context context,
@DrawableRes int drawableRes, int color, TextView textview) {
Resources res = context.getResources();
int padding = (int) res.getDimension(
R.dimen.activity_horizontal_margin);
Drawable drawable = res.getDrawable(drawableRes);
drawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
@sunwicked
sunwicked / CustomTab.Java
Created October 21, 2015 11:10
Creating Custom Tabs to add divider
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
RelativeLayout relativeLayout = (RelativeLayout)
LayoutInflater.from(this).inflate(R.layout.tab_layout, tabLayout, false);
TextView tabTextView = (TextView) relativeLayout.findViewById(R.id.tab_title);
tabTextView.setText(tab.getText());
tab.setCustomView(relativeLayout);
}