Skip to content

Instantly share code, notes, and snippets.

@li2
li2 / AlertDialog.java
Last active May 6, 2018 11:45
How to Use and Style the new AlertDialog from appCompat 22.1 and above? #tags: android-activity
// http://stackoverflow.com/questions/29797134/how-to-use-and-style-the-new-alertdialog-from-appcompat-22-1-and-above
private void showViewTimeSettingDialog() {
// not work
//ContextThemeWrapper themedContext = new ContextThemeWrapper(getActivity(), R.style.CustomAlertDialogStyle);
//AlertDialog.Builder builder = new AlertDialog.Builder(themedContext);
// 向 AppTheme 中加入自定义的 AlertDialog theme 无效,必须在 Builder() 中指定 style. not work
/*
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
@li2
li2 / OnDebouncedClickListener.java
Last active March 20, 2018 05:12
Android 按键去抖动处理 / 防止连续多次点击。Avoid multiple rapid button click at same time in android. #tags: android-view
import android.os.SystemClock;
import android.view.View;
import java.util.Map;
import java.util.WeakHashMap;
/**
* A Debounced OnClickListener
* Rejects clicks that are too close together in time.
* This class is safe to use as an OnClickListener for multiple views, and will debounce each one separately.
@li2
li2 / CenteredImageSpan.java
Last active March 20, 2018 06:46
使 ImageSpan 和 text 居中对齐 & 高度一致。ImageSpan 有一个构造器接收 drawable,所以可以根据 TextView 高度,设置 drawable.setBounds(0, 0, height, height),因此 CenteredImageSpan 也就没必要了。 #tags: android-view
// From http://stackoverflow.com/questions/25628258/align-text-around-imagespan-center-vertical
public class CenteredImageSpan extends ImageSpan {
// Extra variables used to redefine the Font Metrics when an ImageSpan is added
private int initialDescent = 0;
private int extraSpace = 0;
public CenteredImageSpan(final Drawable drawable) {
this(drawable, entry, DynamicDrawableSpan.ALIGN_BOTTOM);
@li2
li2 / AddImageToText.java
Last active March 20, 2018 06:40
Add image to text in textView. 对于简单的图文混排的 TextView 可以使用 SpannableStringBuilder。 #tags: android-view
/* __
Like this (|__| represents an image):
__ __
|__| Settings - |__| Safety.
Get TextView height by OnGlobalLayoutListener dynamically, and then
set drawable size by calling drawable.set(left, top, right, bottom),
in order to make sure the height of drawable is same as the string.
http://stackoverflow.com/questions/3591784/getwidth-and-getheight-of-view-returns-0
@li2
li2 / AndroidSpannableStringExample.java
Last active May 18, 2018 14:21
SpannableString class to implement text with foreground color, background color, underline, strikethrough, bold & italic style, relative font size, superscript, subscript, URLs, clickable, text composed with image, and so on. #tags: android-view
/**
* Created by weiyi on 3/28/16.
* http://li2.me/2016/03/android-spannablestring-example.html
* https://github.com/li2/Learning_Android_Open_Source/tree/master/AndroidTextSample
*/
public class SpannableStringApiActivity extends AppCompatActivity {
@Bind(R.id.textView) TextView mTextView;
@Bind(R.id.spannableForegroundColor) TextView mForegroundColorTextView;
@Bind(R.id.spannableBackgroundColor) TextView mBackgroundColorTextView;
@li2
li2 / OnMultipleClickListener.java
Last active March 20, 2018 05:09
Android 连续多次点击事件回调接口。Interface definition for a callback to be invoked when a view is multiple clicked. 继承自 View.OnClickListener,当连续多次点击时被回调。点击之间的间隔必须小于给定值,才能被识别为一次连续的点击;当点击之间的间隔超时后,丢弃之前记录的点击,重新开始记录。 #tags: android-view
import android.os.SystemClock;
import android.view.View;
import java.util.Map;
import java.util.WeakHashMap;
// 源码基于 http://stackoverflow.com/questions/16534369/avoid-button-multiple-rapid-clicks
public abstract class OnMultipleClickListener implements View.OnClickListener {
private static final int DEFAULT_MAX_INTERVAL = 1000; // ms
private int mClickIndex;
@li2
li2 / ToggleButton.java
Last active March 20, 2018 11:33
使用 ToggleButton 用来展现两种状态,使用 OnDebouncedClickListener 用来防止多次点击确实起了作用屏蔽了无效点击,但也存在1个问题,快速点击时,ToggleButton 的状态还在切换。如何解决? #tags: android-view
protected void onCreate(@Nullable Bundle savedInstanceState) {
mOperation1Btn = (ToggleButton) view.findViewById(R.id.operation1);
mOperation1Btn.setOnClickListener(new OnDebouncedClickListener() {
@Override
public void onDebouncedClick(View v) {
onToolbarOperation1Clicked(mOperation1Btn.isChecked());
}
});
}
@li2
li2 / rounded_rectangle.xml
Last active May 6, 2018 11:44
Implement rounded rectangle (rectangles with rounded corners) background. Usage: android:background="@drawable/rounded_rectangle" #tags: android-view
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white"/>
<padding
android:left="16dp"
android:top="4dp"
android:right="16dp"
android:bottom="4dp"/>
<corners
android:radius="2dp"/>
@li2
li2 / MonitorService.java
Last active March 20, 2018 11:30
[Monitor Service status regularly, restart the service if it's died] #tags: android-service
/**
* Created by weiyi on 4/18/16.
*/
public class MonitorService extends IntentService {
private static final String TAG = makeLogTag(MonitorService.class);
private static final int MONITOR_INTERVAL = 5 * 1000;
private Context mAppContext;
private Handler mHandler;
@li2
li2 / AddView.java
Last active March 20, 2018 11:48
[How to add views dynamically to a RelativeLayout already declared in the xml layout?] #tags: android-view
private void addPageTurnButton() {
View view = getLayoutInflater().inflate(R.layout.widget_page_turn, null);
/*
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
view.setLayoutParams(params);