Skip to content

Instantly share code, notes, and snippets.

<LinearLayout
android:id="@+id/tutorial_viewpager_container"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/tutorial_rounded_corner_background">
<android.support.v4.view.ViewPager
android:id="@+id/tutorial_viewpager"
android:background="@android:color/transparent"
@mrleolink
mrleolink / MultiStateDrawable.java
Last active August 29, 2015 13:55
A really useful gist which helps to create pressed state for Android's Button/ImageButton without using additional drawbles
package net.leolink.android.gist;
public class MultiStateDrawable extends LayerDrawable {
// The color filter to apply when the button is pressed
protected ColorFilter _pressedFilter = new LightingColorFilter(Color.GRAY, 1);
public MultiStateDrawable(Drawable d) {
super(new Drawable[] { d });
}
@mrleolink
mrleolink / SoftKeyboardHandledLinearLayout.java
Last active December 21, 2015 04:33
A hack to catch events when soft keyboard comes up/down on Android (Tested on 2.3 and 4.0.4) (For usage: check out my blog post at: http://tech.leolink.net/2014/02/a-hack-to-catch-soft-keyboard-showhide.html)
public class SoftKeyboardHandledLinearLayout extends LinearLayout {
private boolean isKeyboardShown;
private SoftKeyboardVisibilityChangeListener listener;
public SoftKeyboardHandledLinearLayout(Context context) {
super(context);
}
public SoftKeyboardHandledLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
@mrleolink
mrleolink / SingleLineTextView.java
Created June 30, 2014 12:06
A custom TextView that can shrink its text size to fit in a single line
/**
* Forked from: http://androidxref.com/4.1.1/xref/frameworks/base/core/java/com/android/internal/widget/DialogTitle.java
* Created by LeoLink on 2014-06-30.
*/
public class SingleLineTextView extends TextView {
public SingleLineTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setSingleLine();
setEllipsize(TextUtils.TruncateAt.END);
@mrleolink
mrleolink / AutoWrapLayout.java
Last active August 29, 2015 14:09
A simple implementation for AutoWrapLayout for TextView
/**
* This f**king code was created by Leo on 11/12/14.
*/
public class AutoWrapLayout extends RelativeLayout {
private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
private static final int DEFAULT_MARGIN = 5; // dp
private int margin;
private LinkedList<String> dataList;
@mrleolink
mrleolink / CustomEllipsizeTextView.java
Created November 20, 2014 08:56
A hack to set custom ellipsize and postfix string for Android's TextView
public class CustomEllipsizeTextView extends TextView {
private static final int MAX_LINE = 2;
private static final String ELLIPSIZE_STRING = "...";
private String originalText;
private int maxLines = MAX_LINE;
private String postfix;
private boolean postfixAdded;
@mrleolink
mrleolink / intToHexString.java
Last active November 27, 2016 09:00
Explanation how to convert an int to hex string in Java use bitwise operations
public static String toHexString(int decimal) {
String codes = "0123456789ABCDEF";
StringBuilder builder = new StringBuilder(8);
builder.setLength(8);
for (int i = 7; i >= 0; i--) {
// E.g: let decimal = 229 -> its value when expressed in binary is 11100101
// And binary form of 0xF is 1111 (equals to ..00000001111)
@mrleolink
mrleolink / UnsignedRightShiftOperator.java
Created October 8, 2015 05:27
Unsigned right shift operator explanation
int value = -229;
System.out.println(value);
System.out.println(value >> 5);
System.out.println(value >>> 5);
System.out.println(Integer.toBinaryString(value));
System.out.println(Integer.toBinaryString(value >> 5)); // `>>` preserves integer's sign
System.out.println(Integer.toBinaryString(value >>> 5)); // `>>>` doesn't preserve integer's sign, just shift everything to the right then fill all the empty bit on the left with 0
//======================= RESULT
-229
@mrleolink
mrleolink / .zshrc
Last active November 27, 2016 09:02
My .zshrc
# General
export LANG=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
# Path to your oh-my-zsh installation.
export ZSH=~/Dropbox/MY/Preferences/Unix/\[dot\]oh-my-zsh
export MANPATH="/usr/local/man:$MANPATH"
export SSH_KEY_PATH="~/.ssh/dsa_id"
# zsh custom folder
ZSH_CUSTOM="$ZSH/custom"
@mrleolink
mrleolink / AsyncFuture.java
Last active November 27, 2016 09:02
An implementation of Future which also provides UI Thread callback for Android - Revision 1 is reviewed here: http://codereview.stackexchange.com/questions/143608/combination-of-javas-future-and-androids-asynctask
import android.os.Handler;
import android.os.Looper;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;