Example usage for android.widget TextView getText

List of usage examples for android.widget TextView getText

Introduction

In this page you can find the example usage for android.widget TextView getText.

Prototype

@ViewDebug.CapturedViewProperty
public CharSequence getText() 

Source Link

Document

Return the text that TextView is displaying.

Usage

From source file:Main.java

private static void onCenterDraw(TextView view, Canvas canvas, Drawable drawable, int gravity) {
    int drawablePadding = view.getCompoundDrawablePadding();
    int ratio = 1;
    float total;/*  ww  w .ja  va2  s.com*/

    switch (gravity) {
    case Gravity.END:
        ratio = -1;
    case Gravity.START:
        total = view.getPaint().measureText(view.getText().toString()) + drawable.getIntrinsicWidth()
                + drawablePadding + view.getPaddingLeft() + view.getPaddingRight();
        canvas.translate(ratio * (view.getWidth() - total) / 2, 0);
        break;
    case Gravity.BOTTOM:
        ratio = -1;
    case Gravity.TOP:
        Paint.FontMetrics fontMetrics = view.getPaint().getFontMetrics();
        total = fontMetrics.descent - fontMetrics.ascent + drawable.getIntrinsicHeight() + drawablePadding
                + view.getPaddingTop() + view.getPaddingBottom();
        canvas.translate(0, ratio * (view.getHeight() - total) / 2);
        break;
    }
}

From source file:Main.java

private static void onCenterDraw(TextView view, Canvas canvas, Drawable drawable, int gravity) {
    int drawablePadding = view.getCompoundDrawablePadding();
    int ratio = 1;
    float total;/*w  ww.j a v  a  2  s .  co  m*/

    switch (gravity) {
    case Gravity.RIGHT:
        ratio = -1;
    case Gravity.LEFT:
        total = view.getPaint().measureText(view.getText().toString()) + drawable.getIntrinsicWidth()
                + drawablePadding + view.getPaddingLeft() + view.getPaddingRight();
        canvas.translate(ratio * (view.getWidth() - total) / 2, 0);
        break;
    case Gravity.BOTTOM:
        ratio = -1;
    case Gravity.TOP:
        Paint.FontMetrics fontMetrics0 = view.getPaint().getFontMetrics();
        total = fontMetrics0.descent - fontMetrics0.ascent + drawable.getIntrinsicHeight() + drawablePadding
                + view.getPaddingTop() + view.getPaddingBottom();
        canvas.translate(0, ratio * (view.getHeight() - total) / 2);
        break;
    }
}

From source file:com.nextgis.maplibui.util.ControlHelper.java

public static void highlightText(TextView textView) {
    final CharSequence text = textView.getText();
    final SpannableString spannableString = new SpannableString(text);
    spannableString.setSpan(new URLSpan(""), 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannableString, TextView.BufferType.SPANNABLE);
}

From source file:Main.java

public static void setTextViewCharHilighted(TextView textView, String text, int startIndex, int endIndex,
        int color) {
    if (textView == null || text == null) {
        return;/*from  ww w  .jav a 2s .  c o  m*/
    }

    if (startIndex < 0) {
        return;
    }

    if (endIndex > text.length()) {
        return;
    }

    textView.setText(text, BufferType.SPANNABLE);

    Spannable span = (Spannable) textView.getText();
    if (span == null) {
        return;
    }

    span.setSpan(new ForegroundColorSpan(color), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

From source file:Main.java

/**
 * Checks if a View matches a certain string and returns the amount of total matches.
 * /*  ww w .  j  a  va  2  s  . co  m*/
 * @param regex the regex to match
 * @param view the view to check
 * @param uniqueTextViews set of views that have matched
 * @return number of total matches
 */

public static int getNumberOfMatches(String regex, TextView view, Set<TextView> uniqueTextViews) {
    if (view == null) {
        return uniqueTextViews.size();
    }

    Pattern pattern = null;
    try {
        pattern = Pattern.compile(regex);
    } catch (PatternSyntaxException e) {
        pattern = Pattern.compile(regex, Pattern.LITERAL);
    }

    Matcher matcher = pattern.matcher(view.getText().toString());

    if (matcher.find()) {
        uniqueTextViews.add(view);
    }

    if (view.getError() != null) {
        matcher = pattern.matcher(view.getError().toString());
        if (matcher.find()) {
            uniqueTextViews.add(view);
        }
    }
    if (view.getText().toString().equals("") && view.getHint() != null) {
        matcher = pattern.matcher(view.getHint().toString());
        if (matcher.find()) {
            uniqueTextViews.add(view);
        }
    }
    return uniqueTextViews.size();
}

From source file:com.memetro.android.alerts.CommentDialog.java

public static void showDialog(final Context context, String comment, String creator, boolean isMine,
        final Long id) {

    final Dialog mDialog = new Dialog(context);
    mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    mDialog.setContentView(R.layout.comment_dialog);
    mDialog.setCancelable(true);//  www . java 2s  .com

    TextView titleText = (TextView) mDialog.findViewById(R.id.title);
    TextView messageText = (TextView) mDialog.findViewById(R.id.message);
    Button closeButton = (Button) mDialog.findViewById(R.id.close);
    Button deleteButton = (Button) mDialog.findViewById(R.id.delete);

    titleText.setText(titleText.getText().toString() + " " + creator);
    messageText.setMovementMethod(ScrollingMovementMethod.getInstance());
    messageText.setText(comment);

    if (isMine) {
        deleteButton.setVisibility(View.VISIBLE);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DeleteInBg(context, String.valueOf(id)).execute();
                mDialog.dismiss();
                // TODO Refrescar las alertas
            }
        });
    }

    closeButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mDialog.dismiss();
        }
    });

    mDialog.show();

}

From source file:com.skalski.websocketsclient.ActivityMain.java

static void appendText(TextView textView, String text, int textColor) {

    int start;//from   w  ww  .j av  a 2 s  .  c  om
    int end;

    start = textView.getText().length();
    textView.append(text);
    end = textView.getText().length();

    Spannable spannableText = (Spannable) textView.getText();
    spannableText.setSpan(new ForegroundColorSpan(textColor), start, end, 0);
}

From source file:com.fsck.k9.helper.Utility.java

public static boolean requiredFieldValid(TextView view) {
    return view.getText() != null && view.getText().length() > 0;
}

From source file:com.jefftharris.passwdsafe.lib.AboutDialog.java

/**
 * Update the fields of the about fragment
 *///  w  w  w  . j  av  a2 s  . c  o  m
public static String updateAboutFields(View detailsView, final String extraLicenseInfo, Context ctx) {
    String name;
    StringBuilder version = new StringBuilder();
    PackageInfo pkgInfo = PasswdSafeUtil.getAppPackageInfo(ctx);
    if (pkgInfo != null) {
        name = ctx.getString(pkgInfo.applicationInfo.labelRes);
        version.append(pkgInfo.versionName);
    } else {
        name = null;
    }

    if (PasswdSafeUtil.DEBUG) {
        version.append(" (DEBUG)");
    }

    TextView tv = (TextView) detailsView.findViewById(R.id.version);
    tv.setText(version);
    tv = (TextView) detailsView.findViewById(R.id.build_id);
    tv.setText(BuildConfig.BUILD_ID);
    tv = (TextView) detailsView.findViewById(R.id.build_date);
    tv.setText(BuildConfig.BUILD_DATE);
    tv = (TextView) detailsView.findViewById(R.id.release_notes);
    tv.setText(Html.fromHtml(tv.getText().toString().replace("\n", "<br>")));

    ToggleButton btn = (ToggleButton) detailsView.findViewById(R.id.toggle_license);
    final TextView licenseView = (TextView) detailsView.findViewById(R.id.license);
    btn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            licenseView.setText(extraLicenseInfo);
            licenseView.setVisibility(isChecked ? View.VISIBLE : View.GONE);
        }
    });
    btn.setVisibility((extraLicenseInfo != null) ? View.VISIBLE : View.GONE);

    return name;
}

From source file:Main.java

public static void setTextWithLinks(TextView textView, String htmlText) {
    setHtmlText(textView, htmlText);//w w  w  . j a  va2 s .co m
    textView.setOnTouchListener((v, event) -> {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

            TextView widget = (TextView) v;
            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            ClickableSpan[] link = Spannable.Factory.getInstance().newSpannable(widget.getText()).getSpans(off,
                    off, ClickableSpan.class);

            if (link.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    link[0].onClick(widget);
                }
                return true;
            }
        }
        return false;
    });
}