Skip to content

Instantly share code, notes, and snippets.

@9re
Created November 30, 2011 01:09
Show Gist options
  • Save 9re/1407489 to your computer and use it in GitHub Desktop.
Save 9re/1407489 to your computer and use it in GitHub Desktop.
InputFilter & DynamicLayout
gen/
bin/
.classpath
.project
Sample for InputFilter & DynamicLayout
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kayac.test.edittext"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".InputFilterTestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.kayac.test.edittext;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class InputFilterTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText editText = (EditText) findViewById(R.id.editText);
// test emoji
Bundle bundle = editText.getInputExtras(true);
if (bundle != null) {
bundle.putBoolean("allowEmoji", true);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.kayac.test.edittext.MaximumLineBoundedEditText
android:id="@+id/editText"
android:layout_width="300dp"
android:layout_height="200dp"
android:padding="20dp"
android:singleLine="false"
android:gravity="center"
android:layout_gravity="center"
android:text="@string/hello" />
</LinearLayout>
package com.kayac.test.edittext;
import android.content.Context;
import android.text.DynamicLayout;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.Layout.Alignment;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.Toast;
public class MaximumLineBoundedEditText extends EditText {
private static final int MAX_LINES_DEFAULT = 3;
private int mMaxLines = MAX_LINES_DEFAULT;
// for debugging purpose.
private long mToastTimestamp = -1;
public MaximumLineBoundedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
setFilters(mInputFilters);
}
public void setMaxLines(int maxLines) {
mMaxLines = maxLines;
}
public int getMaxLines() {
return mMaxLines;
}
private InputFilter[] mInputFilters = new InputFilter[] {
new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
// first, calculate the next CharSequence, which would be displayed.
CharSequence base = TextUtils.concat(
dest.subSequence(0, dstart),
source.subSequence(start, end),
dest.subSequence(dend, dest.length()));
// Layout class is used internally in TextView to calculate
// the actual size and to draw the text
DynamicLayout layout = new DynamicLayout(
base, getPaint(),
getWidth() - getPaddingRight() - getPaddingLeft(), // desired width with paddings
Alignment.ALIGN_CENTER, 1.0f, 0.0f, true);
// calculate the line count which is needed to display
// the new CharSequence
int lines = layout.getLineCount();
if (lines > mMaxLines && // if the new line count exceeds the maximum and
(end - start) - (dend- dstart) > 0) { // the replacement increases the length of the CharSequence
long t = System.currentTimeMillis();
if (t - mToastTimestamp > 1000) {
mToastTimestamp = t;
Toast.makeText(getContext(), "You cannot input text which is longer than " +mMaxLines+" lines!", Toast.LENGTH_SHORT)
.show();
}
return ""; // then decline the input
}
// otherwise, keep original
return null;
}
}
};
}
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "ant.properties", and override values to adapt the script to your
# project structure.
# Project target.
target=android-4
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, InputFilterTestActivity!</string>
<string name="app_name">InputFilterTest</string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment