use of net.studymongolian.mongollibrary.MongolTypefaceSpan in project mongol-library by suragch.
the class MongolTextViewActivity method onSpanClick.
public void onSpanClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add span");
final String[] spanTypes = { "BackgroundColorSpan", "ForegroundColorSpan", "RelativeSizeSpan", "TypefaceSpan", "ScaleXSpan", "StyleSpan", "SubscriptSpan", "SuperscriptSpan", "UnderlineSpan", "ClickableSpan" };
final SpannableString spannableString = new SpannableString(mtvExample.getText().toString());
// check boxes
builder.setMultiChoiceItems(spanTypes, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// user checked or unchecked a box
// select the middle third
int start = mtvExample.getText().length() / 3;
int end = mtvExample.getText().length() * 2 / 3;
switch(which) {
case // highlight
0:
if (isChecked) {
BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW);
spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
BackgroundColorSpan[] spans = spannableString.getSpans(0, spannableString.length(), BackgroundColorSpan.class);
for (BackgroundColorSpan span : spans) {
spannableString.removeSpan(span);
}
}
break;
case // text color
1:
if (isChecked) {
ForegroundColorSpan span = new ForegroundColorSpan(Color.GREEN);
spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
ForegroundColorSpan[] spans = spannableString.getSpans(0, spannableString.length(), ForegroundColorSpan.class);
for (ForegroundColorSpan span : spans) {
spannableString.removeSpan(span);
}
}
break;
case // SIZE
2:
if (isChecked) {
RelativeSizeSpan span = new RelativeSizeSpan(2f);
spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
RelativeSizeSpan[] spans = spannableString.getSpans(0, spannableString.length(), RelativeSizeSpan.class);
for (RelativeSizeSpan span : spans) {
spannableString.removeSpan(span);
}
}
break;
case // FONT
3:
// setting a custom font as a span requires the custom MongolTypefaceSpan
if (isChecked) {
Typeface font = MongolFont.get(MongolFontActivity.JCLGQ, getApplicationContext());
MongolTypefaceSpan span = new MongolTypefaceSpan(font);
spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
MongolTypefaceSpan[] spans = spannableString.getSpans(0, spannableString.length(), MongolTypefaceSpan.class);
for (MongolTypefaceSpan span : spans) {
spannableString.removeSpan(span);
}
}
break;
case // scale X
4:
if (isChecked) {
ScaleXSpan span = new ScaleXSpan(2f);
spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
ScaleXSpan[] spans = spannableString.getSpans(0, spannableString.length(), ScaleXSpan.class);
for (ScaleXSpan span : spans) {
spannableString.removeSpan(span);
}
}
break;
case // bold style
5:
if (isChecked) {
// TODO is there a correct way to apply ITALIC to Mongolian text (can adjust paint.setTextSkewX)
// StyleSpan span = new StyleSpan(Typeface.ITALIC);
StyleSpan span = new StyleSpan(Typeface.BOLD);
spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
StyleSpan[] spans = spannableString.getSpans(0, spannableString.length(), StyleSpan.class);
for (StyleSpan span : spans) {
spannableString.removeSpan(span);
}
}
break;
case // Subscript
6:
if (isChecked) {
SubscriptSpan span = new SubscriptSpan();
spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
SubscriptSpan[] spans = spannableString.getSpans(0, spannableString.length(), SubscriptSpan.class);
for (SubscriptSpan span : spans) {
spannableString.removeSpan(span);
}
}
break;
case // Superscript
7:
if (isChecked) {
SuperscriptSpan span = new SuperscriptSpan();
spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
SuperscriptSpan[] spans = spannableString.getSpans(0, spannableString.length(), SuperscriptSpan.class);
for (SuperscriptSpan span : spans) {
spannableString.removeSpan(span);
}
}
break;
case // Underline
8:
if (isChecked) {
UnderlineSpan span = new UnderlineSpan();
spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
UnderlineSpan[] spans = spannableString.getSpans(0, spannableString.length(), UnderlineSpan.class);
for (UnderlineSpan span : spans) {
spannableString.removeSpan(span);
}
}
break;
case // Clickable
9:
if (isChecked) {
ClickableSpan span = new ClickableSpan() {
@Override
public void onClick(View view) {
Toast.makeText(MongolTextViewActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
};
spannableString.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mtvExample.setMovementMethod(LinkMovementMethod.getInstance());
} else {
ClickableSpan[] spans = spannableString.getSpans(0, spannableString.length(), ClickableSpan.class);
for (ClickableSpan span : spans) {
spannableString.removeSpan(span);
}
}
}
}
});
// dialog buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mtvExample.setText(spannableString);
}
});
builder.setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
dialog.show();
}
Aggregations