/** * Helper method to size text dynamically depending on the text size in * relation to the width of the text view */ private void layoutText() { if (getText().equals("")) return; Paint paint = getPaint(); if (mTextSize != 0f) paint.setTextSize(mTextSize); //if min text size is the same as normal size, just leave if (mMinTextSize == getTextSize()) return; float textWidth = paint.measureText(getText().toString()); float boxWidth = getWidth() - getPaddingLeft() - getPaddingRight(); // if the view doesn't exist, the box width will be 0 or negative if (boxWidth <= 0f) return; float textSize = getTextSize(); if (textWidth > boxWidth){ float scaled = textSize * boxWidth / textWidth; //scaled = scaled*0.9f; if (scaled < mMinTextSize) scaled = mMinTextSize; paint.setTextSize(scaled); mTextSize = textSize; } } }
@Override protected void onTextChanged(CharSequence text, int start, int before, int after) { super.onTextChanged(text, start, before, after); layoutText(); }
mResultPreview.setHorizontallyScrolling(true); mResultPreview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mResultPreview.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) {
/** * Grabs newest data from Calculator, updates the main display, and gives an * option to scroll down the result list * * @param updateResult pass true to update result list * @param instantScroll pass true to scroll instantly, otherwise use animation */ private void updateScreen(boolean updateResult, boolean instantScroll) { mDisplay.updateTextFromCalc(); //Update EditText view //will preview become visible during this screen update? boolean makePreviewVisible = !mCalc.isSolved() && !mCalc.isPreviewEmpty() && !mCalc.isUnitSelected(); //if preview just appeared, move the history list up so the last item //doesn't get hidden by the preview if (mResultPreview.getVisibility() != View.VISIBLE && makePreviewVisible){ updateResult = true; instantScroll = true; } mResultPreview.setVisibility(makePreviewVisible ? View.VISIBLE : View.GONE); updatePreviewText(ContextCompat.getColor(mAppContext, R.color.preview_si_suffix_text_color)); //if we hit equals, update result list if (updateResult) mResultListFrag.refresh(instantScroll); }
private void setUpTextView(Context context, AttributeSet attrs) { //grab custom resource variable TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DynamicText, 0, 0); try { mMinTextSize = ta.getDimension(R.styleable.DynamicText_minimumTextSize, getTextSize()); } finally { ta.recycle(); } //Log.d("DYN", "mStartingTextSize = " + getTextSize()); }
@Override public boolean onLongClick(View v) { CharSequence copiedText = mResultPreview.getText(); ClipboardManager clipboard = (ClipboardManager) mAppContext.getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setPrimaryClip(ClipData.newPlainText(null, copiedText)); ViewUtils.toast("Copied: \"" + copiedText + "\"", mAppContext); return true; } });
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if (changed) layoutText(); }