Search in sources :

Example 1 with MongolTypefaceSpan

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();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) SuperscriptSpan(android.text.style.SuperscriptSpan) ForegroundColorSpan(android.text.style.ForegroundColorSpan) DialogInterface(android.content.DialogInterface) Typeface(android.graphics.Typeface) ScaleXSpan(android.text.style.ScaleXSpan) SpannableString(android.text.SpannableString) RelativeSizeSpan(android.text.style.RelativeSizeSpan) ClickableSpan(android.text.style.ClickableSpan) MongolTextView(net.studymongolian.mongollibrary.MongolTextView) View(android.view.View) AdapterView(android.widget.AdapterView) TextPaint(android.text.TextPaint) UnderlineSpan(android.text.style.UnderlineSpan) SpannableString(android.text.SpannableString) MongolTypefaceSpan(net.studymongolian.mongollibrary.MongolTypefaceSpan) StyleSpan(android.text.style.StyleSpan) SubscriptSpan(android.text.style.SubscriptSpan) BackgroundColorSpan(android.text.style.BackgroundColorSpan)

Aggregations

DialogInterface (android.content.DialogInterface)1 Typeface (android.graphics.Typeface)1 AlertDialog (android.support.v7.app.AlertDialog)1 SpannableString (android.text.SpannableString)1 TextPaint (android.text.TextPaint)1 BackgroundColorSpan (android.text.style.BackgroundColorSpan)1 ClickableSpan (android.text.style.ClickableSpan)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 RelativeSizeSpan (android.text.style.RelativeSizeSpan)1 ScaleXSpan (android.text.style.ScaleXSpan)1 StyleSpan (android.text.style.StyleSpan)1 SubscriptSpan (android.text.style.SubscriptSpan)1 SuperscriptSpan (android.text.style.SuperscriptSpan)1 UnderlineSpan (android.text.style.UnderlineSpan)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 MongolTextView (net.studymongolian.mongollibrary.MongolTextView)1 MongolTypefaceSpan (net.studymongolian.mongollibrary.MongolTypefaceSpan)1