Skip to content

Instantly share code, notes, and snippets.

@SouravKumarPandit
Last active November 28, 2018 09:58
Show Gist options
  • Save SouravKumarPandit/36c8b1493e39e07c5d171ce4c3157e26 to your computer and use it in GitHub Desktop.
Save SouravKumarPandit/36c8b1493e39e07c5d171ce4c3157e26 to your computer and use it in GitHub Desktop.
public class StyleEditLayout extends LinearLayout implements RadioGroup.OnCheckedChangeListener
{
Context context;
public StyleEditLayout(Context context)
{
super(context);
intit(context);
}
public StyleEditLayout(Context context, @Nullable AttributeSet attrs)
{
super(context, attrs);
intit(context);
}
public StyleEditLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
intit(context);
}
public void intit(Context context)
{
this.context = context;
setOrientation(LinearLayout.VERTICAL);
setGravity(Gravity.TOP);
setPadding(25, 25, 25, 25);
EditText styleEditText = new EditText(context);
styleEditText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SUtils.dpToPx(200)));
styleEditText.setPadding(20, 20, 20, 20);
styleEditText.setMinLines(6);
styleEditText.setSingleLine(false);
styleEditText.setGravity(Gravity.TOP);
styleEditText.setId(R.id.custom_edit);
GradientDrawable pressedDrawable = new GradientDrawable();
pressedDrawable.setCornerRadius(SUtils.dpToPixel(5));
pressedDrawable.setStroke(2, getResources().getColor(R.color.colorPrimaryDark));
styleEditText.setBackground(pressedDrawable);
RadioGroup radioGroup = new RadioGroup(context);
radioGroup.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
radioGroup.setOrientation(LinearLayout.HORIZONTAL);
// radioGroup.setPadding(15,15,15,15);
radioGroup.addView(getRadioButton("None", 20400));
radioGroup.addView(getRadioButton("Bl", 20500));
radioGroup.addView(getRadioButton("It", 20600));
radioGroup.addView(getRadioButton("Ul", 20700));
radioGroup.addView(getRadioButton("Color", 20800));
radioGroup.setOnCheckedChangeListener(this);
addView(styleEditText);
addView(radioGroup);
}
private RadioButton getRadioButton(String sLable, int iRadioId)
{
int flag = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
RadioButton radioButton = new RadioButton(context);
radioButton.setText(sLable);
int i5 = SUtils.dpToPx(5);
radioButton.setPadding(i5, i5, 0, i5);
radioButton.setTextColor(getResources().getColor(R.color.gray_700));
radioButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
radioButton.setId(iRadioId);
radioButton.setTextColor(Color.BLACK);
SpannableStringBuilder sbSpanChar = new SpannableStringBuilder(sLable);
if (iRadioId == 20500)
sbSpanChar.setSpan(new StyleSpan(Typeface.BOLD), 0, sbSpanChar.length(), flag);
else if (iRadioId == 20600)
sbSpanChar.setSpan(new StyleSpan(Typeface.ITALIC), 0, sbSpanChar.length(), flag);
else if (iRadioId == 20700)
sbSpanChar.setSpan(new UnderlineSpan(), 0, sbSpanChar.length(), flag);
else if (iRadioId == 20800)
sbSpanChar.setSpan(new ForegroundColorSpan(Color.BLUE), 0, sbSpanChar.length(), flag);
radioButton.setText(sbSpanChar);
if (iRadioId == 20400)
radioButton.setChecked(true);
return radioButton;
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i)
{
EditText styleEditText = findViewById(R.id.custom_edit);
if (styleEditText != null)
if (i == 20500)
{
styleEditText.setFilters(new InputFilter[]{new SInputFilter.AllBold()});
} else if (i == 20600)
{
styleEditText.setFilters(new InputFilter[]{new SInputFilter.AllItalic()});
} else if (i == 20700)
{
styleEditText.setFilters(new InputFilter[]{new SInputFilter.AllUnderLine()});
} else if (i == 20800)
{
styleEditText.setFilters(new InputFilter[]{new SInputFilter.AllColored(Color.BLUE)});
} else
{
styleEditText.setFilters(new InputFilter[]{});
}
}
public interface SInputFilter
{
int flag=Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
public static class AllCaps implements InputFilter
{
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (Character.isLowerCase(source.charAt(i))) {
char[] v = new char[end - start];
TextUtils.getChars(source, start, end, v, 0);
String s = new String(v);
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(s);
TextUtils.copySpansFrom((Spanned) source,
start, end, null, sp, 0);
return sp;
} else {
return s;
}
}
}
return null; // keep original
}
}
public static class AllColored implements InputFilter
{
private final int iColor;
AllColored(@ColorInt int iColor)
{
this.iColor=iColor;
}
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (Character.isLowerCase(source.charAt(i))) {
char[] v = new char[end - start];
TextUtils.getChars(source, start, end, v, 0);
String s = new String(v);
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(s);
sp.setSpan(new ForegroundColorSpan(iColor),start,end,flag);
return sp;
} else {
return s;
}
}
}
return null; // keep original
}
}
public static class AllBold implements InputFilter
{
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (Character.isLowerCase(source.charAt(i))) {
char[] v = new char[end - start];
TextUtils.getChars(source, start, end, v, 0);
String s = new String(v);
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(s);
sp.setSpan(new StyleSpan(Typeface.BOLD), start,end, flag);
return sp;
} else {
return s;
}
}
}
return null; // keep original
}
}
public static class AllItalic implements InputFilter
{
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (Character.isLowerCase(source.charAt(i))) {
char[] v = new char[end - start];
TextUtils.getChars(source, start, end, v, 0);
String s = new String(v);
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(s);
sp.setSpan(new StyleSpan(Typeface.ITALIC), start, end, flag);
return sp;
} else {
return s;
}
}
}
return null; // keep original
}
}public static class AllUnderLine implements InputFilter
{
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (Character.isLowerCase(source.charAt(i))) {
char[] v = new char[end - start];
TextUtils.getChars(source, start, end, v, 0);
String s = new String(v);
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(s);
sp.setSpan(new UnderlineSpan(), start, end, flag);
return sp;
} else {
return s;
}
}
}
return null; // keep original
}
}
/**
* This filter will constrain edits not to make the length of the text
* greater than the specified length.
*/
public static class LengthFilter implements InputFilter
{
public LengthFilter(int max) {
mMax = max;
}
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
int keep = mMax - (dest.length() - (dend - dstart));
if (keep <= 0) {
return "";
} else if (keep >= end - start) {
return null; // keep original
} else {
return source.subSequence(start, start + keep);
}
}
private int mMax;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment