Example usage for android.widget TextView getWidth

List of usage examples for android.widget TextView getWidth

Introduction

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

Prototype

@ViewDebug.ExportedProperty(category = "layout")
public final int getWidth() 

Source Link

Document

Return the width of your view.

Usage

From source file:org.chromium.chrome.browser.toolbar.ToolbarPhone.java

private Property<TextView, Integer> buildUrlScrollProperty(final View containerView,
        final boolean isContainerRtl) {
    // If the RTL-ness of the container view changes during an animation, the scroll values
    // become invalid.  If that happens, snap to the ending position and no longer update.
    return new Property<TextView, Integer>(Integer.class, "scrollX") {
        private boolean mRtlStateInvalid;

        @Override//from  ww  w.  java  2s  .c o  m
        public Integer get(TextView view) {
            return view.getScrollX();
        }

        @Override
        public void set(TextView view, Integer scrollX) {
            if (mRtlStateInvalid)
                return;
            boolean rtl = ApiCompatibilityUtils.isLayoutRtl(containerView);
            if (rtl != isContainerRtl) {
                mRtlStateInvalid = true;
                if (!rtl || mUrlBar.getLayout() != null) {
                    scrollX = 0;
                    if (rtl) {
                        scrollX = (int) view.getLayout().getPrimaryHorizontal(0);
                        scrollX -= view.getWidth();
                    }
                }
            }
            view.setScrollX(scrollX);
        }
    };
}

From source file:com.tekinarslan.material.sample.customui.slidingtab_new.SlidingTabLayout.java

private void populateTabStrip() {

    final PagerAdapter adapter = mViewPager.getAdapter();
    final OnClickListener tabClickListener = new TabClickListener();

    for (int i = 0; i < adapter.getCount(); i++) {
        View tabView = null;/*from  ww w  . ja v  a 2 s.c  om*/
        TextView tabTitleView = null;

        if (mTabViewLayoutId != 0) {
            // If there is a custom tab view layout id set, try and inflate
            // it
            tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false);
            tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
        }

        if (tabView == null) {
            tabView = createDefaultTabView(getContext());
        }

        if (tabTitleView == null && TextView.class.isInstance(tabView)) {
            tabTitleView = (TextView) tabView;
        }

        if (mDistributeEvenly) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams();
            lp.width = 0;
            lp.weight = 1;
        }

        tabTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TExt_Text_Size);
        tabTitleView.setText(adapter.getPageTitle(i));
        tabView.setOnClickListener(tabClickListener);

        //         tabTitleView.setTextColor(TITLE_TEXT_DEFAULT_COLOR);
        //         tabTitleView.setTextColor(R.drawable.xpt_qfmycollection_tab_title_text);
        tabTitleView.setSelected(false);
        if (i == 0) {
            tabTitleView.setTextColor(TITLE_TEXT_SELECTED_COLOR);
            //            tabTitleView.setSelected(true);
        }

        String desc = mContentDescriptions.get(i, null);
        if (desc != null) {
            tabView.setContentDescription(desc);
        }

        if (lockedWidth) {
            // ?
            int tabWidth = viewWidth / mViewPager.getAdapter().getCount();//?
            //                int tabWidth = tabView.getWidth();
            //                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(tabWidth, LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            //                params.weight=1;
            //                params.width=0;
            mTabStrip.addView(tabView, params);
            LogUtils.d("tabViewWidth  " + tabView.getWidth() + "  tabWidth  " + tabWidth + "  tabTitleView "
                    + tabTitleView.getText() + "  tabTitleView.width " + tabTitleView.getWidth());

        } else {
            LogUtils.d("tabView  " + tabView);
            mTabStrip.addView(tabView);
        }
    }

}

From source file:com.android.app.MediaPlaybackActivity.java

public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    TextView tv = textViewForContainer(v);
    if (tv == null) {
        return false;
    }/*from  w w w.j a v a2 s. c o  m*/
    if (action == MotionEvent.ACTION_DOWN) {
        v.setBackgroundColor(0xff606060);
        mInitialX = mLastX = (int) event.getX();
        mDraggingLabel = false;
    } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        v.setBackgroundColor(0);
        if (mDraggingLabel) {
            Message msg = mLabelScroller.obtainMessage(0, tv);
            mLabelScroller.sendMessageDelayed(msg, 1000);
        }
    } else if (action == MotionEvent.ACTION_MOVE) {
        if (mDraggingLabel) {
            int scrollx = tv.getScrollX();
            int x = (int) event.getX();
            int delta = mLastX - x;
            if (delta != 0) {
                mLastX = x;
                scrollx += delta;
                if (scrollx > mTextWidth) {
                    // scrolled the text completely off the view to the left
                    scrollx -= mTextWidth;
                    scrollx -= mViewWidth;
                }
                if (scrollx < -mViewWidth) {
                    // scrolled the text completely off the view to the right
                    scrollx += mViewWidth;
                    scrollx += mTextWidth;
                }
                tv.scrollTo(scrollx, 0);
            }
            return true;
        }
        int delta = mInitialX - (int) event.getX();
        if (Math.abs(delta) > mTouchSlop) {
            // start moving
            mLabelScroller.removeMessages(0, tv);

            // Only turn ellipsizing off when it's not already off, because it
            // causes the scroll position to be reset to 0.
            if (tv.getEllipsize() != null) {
                tv.setEllipsize(null);
            }
            Layout ll = tv.getLayout();
            // layout might be null if the text just changed, or ellipsizing
            // was just turned off
            if (ll == null) {
                return false;
            }
            // get the non-ellipsized line width, to determine whether scrolling
            // should even be allowed
            mTextWidth = (int) tv.getLayout().getLineWidth(0);
            mViewWidth = tv.getWidth();
            if (mViewWidth > mTextWidth) {
                tv.setEllipsize(TruncateAt.END);
                v.cancelLongPress();
                return false;
            }
            mDraggingLabel = true;
            tv.setHorizontalFadingEdgeEnabled(true);
            v.cancelLongPress();
            return true;
        }
    }
    return false;
}