Java Code Examples for android.widget.TextView.getText()

The following are Jave code examples for showing how to use getText() of the android.widget.TextView class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: reflow-animator   File: ReflowTextAnimatorHelper.java   View Source Code Vote up 7 votes
private static Layout createUnrestrictedLayout(@Nonnull TextView view) {
    CharSequence text = view.getText();
    Layout layout = view.getLayout();
    TextPaint paint = layout.getPaint();

    if (SDK_INT >= M) {
        return StaticLayout.Builder
                .obtain(text, 0, text.length(), layout.getPaint(), layout.getWidth())
                .setAlignment(layout.getAlignment())
                .setLineSpacing(view.getLineSpacingExtra(), view.getLineSpacingMultiplier())
                .setIncludePad(view.getIncludeFontPadding())
                .setBreakStrategy(view.getBreakStrategy())
                .setHyphenationFrequency(view.getHyphenationFrequency())
                .build();
    } else {
        return new StaticLayout(
                text,
                paint,
                text.length(),
                layout.getAlignment(),
                view.getLineSpacingMultiplier(),
                view.getLineSpacingExtra(),
                view.getIncludeFontPadding());
    }
}
 
Example 2
Project: buildAPKsSamples   File: CreateGestureActivity.java   View Source Code Vote up 6 votes
@SuppressWarnings({"UnusedDeclaration"})
public void addGesture(View v) {
    if (mGesture != null) {
        final TextView input = (TextView) findViewById(R.id.gesture_name);
        final CharSequence name = input.getText();
        if (name.length() == 0) {
            input.setError(getString(R.string.error_missing_name));
            return;
        }

        final GestureLibrary store = GestureBuilderActivity.getStore();
        store.addGesture(name.toString(), mGesture);
        store.save();

        setResult(RESULT_OK);

        final String path = new File(Environment.getExternalStorageDirectory(),
                "gestures").getAbsolutePath();
        Toast.makeText(this, getString(R.string.save_success, path), Toast.LENGTH_LONG).show();
    } else {
        setResult(RESULT_CANCELED);
    }

    finish();
    
}
 
Example 3
Project: chat-sdk-android-push-firebase   File: ChatSDKAbstractChatActivity.java   View Source Code Vote up 6 votes
/**
 *  Setting the thread name in the action bar.
 * */
protected boolean setThreadName(TextView txtName){
    String displayName = thread.displayName();

    if (StringUtils.isBlank(displayName) )
        return false;

    if (txtName.getText() == null || !displayName.equals(txtName.getText().toString()))
    {
        // Set the title of the screen, This is used for the label in the screen overview on lollipop devices.
        setTitle(displayName);
        
        txtName.setText(displayName);
        txtName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showToast(((TextView) v).getText().toString());
            }
        });

        return true;
    }

    return false;
}
 
Example 4
Project: Simpler   File: StatusDataSetter.java   View Source Code Vote up 6 votes
/**
 * 微博文本触摸监听处理
 *
 * @param textView 点击的TextView
 * @param event
 * @return true:点击事件被处理;false:点击事件未被处理,向上冒泡
 */
private boolean textTouchEvent(TextView textView, MotionEvent event) {
    boolean ret = false;
    CharSequence text = textView.getText();
    Spannable sText = Spannable.Factory.getInstance().newSpannable(text);
    int action = event.getAction();
    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        x -= textView.getTotalPaddingLeft();
        y -= textView.getTotalPaddingTop();
        x += textView.getScrollX();
        y += textView.getScrollY();
        Layout layout = textView.getLayout();
        int line = layout.getLineForVertical(y);
        int offset = layout.getOffsetForHorizontal(line, x);
        ClickableSpan[] links = sText.getSpans(offset, offset, ClickableSpan.class);
        if (links.length != 0) {
            if (action == MotionEvent.ACTION_UP) {
                links[0].onClick(textView);
            }
            ret = true;
        }
    }
    return ret;
}
 
Example 5
Project: GitHub   File: StringUtil.java   View Source Code Vote up 5 votes
/**获取string,为null则返回""
 * @param tv
 * @return
 */
public static String getString(TextView tv) {
	if (tv == null || tv.getText() == null) {
		return "";
	}
	return getString(tv.getText().toString());
}
 
Example 6
Project: Android-WordToSpan   File: WordToSpan.java   View Source Code Vote up 5 votes
@Override
public void onClick(View textView) {
    TextView tv = (TextView) textView;
    Spanned s = (Spanned) tv.getText();
    int start = s.getSpanStart(this);
    int end = s.getSpanEnd(this);
    clickListener.onClick(type, s.subSequence(start, end).toString().trim());
}
 
Example 7
Project: chromium-for-android-56-debug-video   File: InfoBar.java   View Source Code Vote up 5 votes
@Override
public CharSequence getAccessibilityText() {
    if (mView == null) return "";
    TextView messageView = (TextView) mView.findViewById(R.id.infobar_message);
    if (messageView == null) return "";
    return messageView.getText() + mContext.getString(R.string.bottom_bar_screen_position);
}
 
Example 8
Project: Markwon   File: TableRowsScheduler.java   View Source Code Vote up 5 votes
private static Object[] extract(@NonNull TextView view) {
    final Object[] out;
    final CharSequence text = view.getText();
    if (!TextUtils.isEmpty(text) && text instanceof Spanned) {
        out = ((Spanned) text).getSpans(0, text.length(), TableRowSpan.class);
    } else {
        out = null;
    }
    return out;
}
 
Example 9
Project: APIJSON-Android-RxJava   File: StringUtil.java   View Source Code Vote up 5 votes
/**获取string,为null则返回""
 * @param tv
 * @return
 */
public static String getString(TextView tv) {
	if (tv == null || tv.getText() == null) {
		return "";
	}
	return getString(tv.getText().toString());
}
 
Example 10
Project: CustomAndroidOneSheeld   File: URLSpanNoUnderline.java   View Source Code Vote up 5 votes
public static void stripUnderlines(TextView textView) {
    Spannable s = new SpannableString(textView.getText());
    URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
    for (URLSpan span: spans) {
        int start = s.getSpanStart(span);
        int end = s.getSpanEnd(span);
        s.removeSpan(span);
        span = new URLSpanNoUnderline(span.getURL());
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}
 
Example 11
Project: ZhaZhaShop   File: WaitMovieAdapter.java   View Source Code Vote up 5 votes
@Override
protected void convert(final BaseViewHolder helper, final WaitMovieBean.DataBean.ComingBean item) {
    helper.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ToastUtil.showShort(mContext, helper.getAdapterPosition() + "");
        }
    });

    // 图片地址不能直接使用,需要进行转换
    String originUrl = item.getImg();
    String imgUrl = originUrl.replace("/w.h/", "/") + "@171w_240h_1e_1c_1l";//后缀为图片大小
    GlideManager.loadImage(mContext, imgUrl, (ImageView) helper.getView(R.id.iv_wait_movie));

    helper.setText(R.id.tv_wait_movie_name, item.getNm())
            .setText(R.id.tv_wait_movie_desc, item.getScm())
            .setText(R.id.tv_wait_movie_wish, String.format("%s人想看", item.getWish()))
            .setText(R.id.tv_wait_movie_major, String.format("主演:%s", item.getStar()));

    TextView tv_wish = helper.getView(R.id.tv_wait_movie_wish);
    Spannable spannable = new SpannableString(tv_wish.getText());
    spannable.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.text_yellow, null)), 0, tv_wish.getText().toString().indexOf("人想看"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv_wish.setText(spannable);

    helper.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MovieDetailActivity.invoke(mContext, item.getId());
        }
    });
}
 
Example 12
Project: AOSP-Kayboard-7.1.2   File: SuggestionStripLayoutHelper.java   View Source Code Vote up 5 votes
private void layoutDebugInfo(final int positionInStrip, final ViewGroup placerView,
        final int x) {
    final TextView debugInfoView = mDebugInfoViews.get(positionInStrip);
    final CharSequence debugInfo = debugInfoView.getText();
    if (debugInfo == null) {
        return;
    }
    placerView.addView(debugInfoView);
    debugInfoView.measure(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    final int infoWidth = debugInfoView.getMeasuredWidth();
    final int y = debugInfoView.getMeasuredHeight();
    ViewLayoutUtils.placeViewAt(
            debugInfoView, x - infoWidth, y, infoWidth, debugInfoView.getMeasuredHeight());
}
 
Example 13
Project: UdacityBakingAndroid   File: RecipeDetailFragment.java   View Source Code Vote up 5 votes
private Intent createShareIntent() {
    String msg = mRecipe.getName() + "\n" + "----\n" +
            getString(R.string.ingredients_title) + ":\n" + "----\n";
    for(TextView ingredient : mIngredientList){
        msg += ingredient.getText() + "\n";
    }
    msg += getString(R.string.steps_title) + ":\n" + "----\n";
    for(Step step : mStepList){
        msg += step.getShortDescription() + "\n";
    }
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, msg);
    return shareIntent;
}
 
Example 14
Project: TurboChat   File: EspressoTestUtils.java   View Source Code Vote up 5 votes
public static ViewAction clickClickableSpan(final CharSequence textToClick) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return instanceOf(TextView.class);
        }

        @Override
        public String getDescription() {
            return "clicking on a ClickableSpan";
        }

        @Override
        public void perform(UiController uiController, View view) {
            TextView textView = (TextView) view;
            SpannableString spannableString = (SpannableString) textView.getText();

            if (spannableString.length() == 0) {
                // TextView is empty, nothing to do
                throw new NoMatchingViewException.Builder()
                        .includeViewHierarchy(true)
                        .withRootView(textView)
                        .build();
            }

            // Get the links inside the TextView and check if we find textToClick
            ClickableSpan[] spans = spannableString.getSpans(0, spannableString.length(), ClickableSpan.class);
            if (spans.length > 0) {
                ClickableSpan spanCandidate;
                for (ClickableSpan span : spans) {
                    spanCandidate = span;
                    int start = spannableString.getSpanStart(spanCandidate);
                    int end = spannableString.getSpanEnd(spanCandidate);
                    CharSequence sequence = spannableString.subSequence(start, end);
                    if (textToClick.toString().equals(sequence.toString())) {
                        span.onClick(textView);
                        return;
                    }
                }
            }

            // textToClick not found in TextView
            throw new NoMatchingViewException.Builder()
                    .includeViewHierarchy(true)
                    .withRootView(textView)
                    .build();

        }
    };
}
 
Example 15
Project: stay-awake-app   File: MainActivity.java   View Source Code Vote up 5 votes
private void setColorSpanOnTextView(TextView view, String fulltext, String subtext, int color) {
    view.setText(fulltext, TextView.BufferType.SPANNABLE);
    Spannable str = (Spannable) view.getText();
    int i = fulltext.indexOf(subtext);
    str.setSpan(new ForegroundColorSpan(color), i, i + subtext.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
 
Example 16
Project: SetupWizardLibCompat   File: SetupWizardLayout.java   View Source Code Vote up 4 votes
public CharSequence getHeaderText() {
    final TextView titleView = getHeaderTextView();
    return titleView != null ? titleView.getText() : null;
}
 
Example 17
Project: CXJPadProject   File: VoucherFragment.java   View Source Code Vote up 4 votes
public String getStrValue(TextView textView) {
    if (!TextUtils.isEmpty(textView.getText()) && null != textView.getText())
        return textView.getText().toString().trim();
    else
        return "";
}
 
Example 18
Project: DarkCalculator   File: AutofitHelper.java   View Source Code Vote up 4 votes
/**
 * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
 */
private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize,
                            int maxLines, float precision) {
    if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) {
        // Don't auto-size since there's no limit on lines.
        return;
    }

    int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
    if (targetWidth <= 0) {
        return;
    }

    CharSequence text = view.getText();
    TransformationMethod method = view.getTransformationMethod();
    if (method != null) {
        text = method.getTransformation(text, view);
    }

    Context context = view.getContext();
    Resources r = Resources.getSystem();
    DisplayMetrics displayMetrics;

    float size = maxTextSize;
    float high = size;
    float low = 0;

    if (context != null) {
        r = context.getResources();
    }
    displayMetrics = r.getDisplayMetrics();

    paint.set(view.getPaint());
    paint.setTextSize(size);

    if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth)
            || getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) {
        size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision,
                displayMetrics);
    }

    if (size < minTextSize) {
        size = minTextSize;
    }

    view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
 
Example 19
Project: ChenYan   File: ClickableSpanEx.java   View Source Code Vote up 4 votes
/**
 * If you implement OnTouchListener, call this one in the onTouch method .
 *
 * @param v The view the touch event has been dispatched to.
 * @param event The MotionEvent object containing full information about the event.
 * @return True if the listener has consumed the event , false otherwise.
 */
public static boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    if (v instanceof TextView) {
        TextView widget = (TextView) v;
        int x = (int) event.getX();
        int y = (int) event.getY();

        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);

        //* Return the text the TextView is displaying. If TextView.setText() was called with
        // * an argument of BufferType.SPANNABLE or BufferType.EDITABLE, you can cast
        // * the return value from this method to Spannable or Editable, respectively.
        // *
        //* Note: The content of the return value should not be modified. If you want
        //* a modifiable one, you should make your own copy first.
        Spannable buffer = (Spannable) widget.getText();
        ClickableSpanEx[] link = buffer.getSpans(off, off, ClickableSpanEx.class);

        if (link.length != 0) {
            if (action == MotionEvent.ACTION_DOWN) {
                Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
                link[0].setTransparent(false);
            }
            else {
                if (action == MotionEvent.ACTION_UP) link[0].onClick(widget);
                link[0].setTransparent(true);
                Selection.removeSelection(buffer);
            }
            return true;
        }

    }
    else {
        Log.e(TAG, "ClickableSpanEx supports TextView only .");
    }
    return false;
}
 
Example 20
Project: LuaViewPlayground   File: AutofitHelper.java   View Source Code Vote up 4 votes
/**
 * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
 */
private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize,
                            int maxLines, float precision) {
    if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) {
        // Don't auto-size since there's no limit on lines.
        return;
    }

    int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
    if (targetWidth <= 0) {
        return;
    }

    CharSequence text = view.getText();
    TransformationMethod method = view.getTransformationMethod();
    if (method != null) {
        text = method.getTransformation(text, view);
    }

    Context context = view.getContext();
    Resources r = Resources.getSystem();
    DisplayMetrics displayMetrics;

    float size = maxTextSize;
    float high = size;
    float low = 0;

    if (context != null) {
        r = context.getResources();
    }
    displayMetrics = r.getDisplayMetrics();

    paint.set(view.getPaint());
    paint.setTextSize(size);

    if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth)
            || getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) {
        size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision,
                displayMetrics);
    }

    if (size < minTextSize) {
        size = minTextSize;
    }

    view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}