Example usage for android.widget TextView setMaxWidth

List of usage examples for android.widget TextView setMaxWidth

Introduction

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

Prototype

@android.view.RemotableViewMethod
public void setMaxWidth(int maxPixels) 

Source Link

Document

Sets the width of the TextView to be at most maxPixels wide.

Usage

From source file:Main.java

private static void addText(Activity activity, String text, LinearLayout layout, int maxWidth) {
    TextView textView = new TextView(activity);
    textView.setTextSize(textSize);//from  www. j av a  2s .c  o  m
    textView.setMaxWidth(maxWidth);
    textView.setEllipsize(TextUtils.TruncateAt.END);
    textView.setText(text + "  ");
    layout.addView(textView);

}

From source file:com.amazonaws.demo.messageboard.MessageQueueAdapter.java

public View getView(int pos, View convertView, ViewGroup parent) {
    TextView messageText = new TextView(parent.getContext());
    messageText.setText(this.getMessageText(pos));
    messageText.setGravity(Gravity.LEFT);
    messageText.setPadding(10, 10, 10, 10);
    messageText.setMaxWidth(200);
    messageText.setMinWidth(200);/*ww w  .  j  a v  a  2  s  .  c o m*/
    messageText.setTextSize(16);

    return messageText;
}

From source file:com.uzmap.pkg.uzmodules.uzBMap.mode.Billboard.java

private TextView title() {
    TextView title = new TextView(context);
    title.setSingleLine();//from  w  w w  . j  ava 2s  . c om
    title.setEllipsize(TruncateAt.END);
    title.setMaxWidth(UZCoreUtil.dipToPix(maxWidth) - 2 * iconMarginLeft + iconSize);
    title.setText(getTitle());
    title.setTextColor(getTitleColor());
    title.setTextSize(getTitleSize());
    title.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
    return title;
}

From source file:com.uzmap.pkg.uzmodules.uzBMap.mode.Billboard.java

private TextView subTitle() {
    TextView title = new TextView(context);
    title.setSingleLine();//w w  w . j  av a  2s. c o  m
    title.setEllipsize(TruncateAt.END);
    title.setMaxWidth(UZCoreUtil.dipToPix(maxWidth) - 2 * iconMarginLeft + iconSize);
    title.setText(getSubTitle());
    title.setTextColor(getSubTitleColor());
    title.setTextSize(getSubTitleSize());
    title.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
    return title;
}

From source file:org.nativescript.widgets.TabLayout.java

/**
 * Create a default view to be used for tabs.
 *//*from  w  w w  .j a v  a  2s . c  o m*/
protected View createDefaultTabView(Context context, TabItemSpec tabItem) {
    float density = getResources().getDisplayMetrics().density;
    int padding = (int) (TAB_VIEW_PADDING_DIPS * density);

    LinearLayout ll = new LinearLayout(context);
    ll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    ll.setGravity(Gravity.CENTER);
    ll.setOrientation(LinearLayout.VERTICAL);
    TypedValue outValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
    ll.setBackgroundResource(outValue.resourceId);

    ImageView imgView = new ImageView(context);
    imgView.setScaleType(ScaleType.FIT_CENTER);
    LinearLayout.LayoutParams imgLP = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    imgLP.gravity = Gravity.CENTER;
    imgView.setLayoutParams(imgLP);

    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setMaxWidth((int) (TEXT_MAX_WIDTH * density));
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setEllipsize(TextUtils.TruncateAt.END);
    textView.setAllCaps(true);
    textView.setMaxLines(2);
    textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    textView.setPadding(padding, 0, padding, 0);

    this.setupItem(ll, textView, imgView, tabItem);

    ll.addView(imgView);
    ll.addView(textView);
    return ll;
}

From source file:com.landenlabs.all_devtool.ConsoleFragment.java

private TextView addTextView(RelativeLayout relLayout, int belowId, int rightId, int widthDp, int heightDp,
        int padLeft) {
    float scale = Resources.getSystem().getDisplayMetrics().density;

    int widthParam = (widthDp != 0) ? dpToPx(widthDp) : RelativeLayout.LayoutParams.WRAP_CONTENT;
    int heightParam = (heightDp != 0) ? dpToPx(heightDp) : RelativeLayout.LayoutParams.WRAP_CONTENT;
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(widthParam, heightParam);

    // params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    if (belowId != 0)
        params.addRule(RelativeLayout.BELOW, belowId);
    if (rightId != 0) {
        if (rightId > 0)
            params.addRule(RelativeLayout.ALIGN_RIGHT, rightId);
        else//  w w w.  jav  a 2 s  .  c om
            params.addRule(RelativeLayout.RIGHT_OF, -rightId);
    }

    // relLayout.setPadding(padLeft,  0,  0,  0);
    params.setMargins(padLeft, 0, 0, 0);

    TextView textView = new TextView(relLayout.getContext());
    textView.setLines(1);
    if (widthDp > 0)
        textView.setMaxWidth(dpToPx(widthDp));

    if (Build.VERSION.SDK_INT >= 17) {
        textView.setId(View.generateViewId());
    } else {
        textView.setId(sNextId++);
    }

    relLayout.addView(textView, params);
    return textView;
}

From source file:com.example.appdetail_optimization.PagerSlidingTabStrip.java

@SuppressWarnings("deprecation")
private void addTextTab(final int position, String title) {

    TextView tab = new TextView(getContext());
    tab.setText(title);//from   ww w .j a  va  2  s  .co m
    tab.setGravity(Gravity.CENTER);
    tab.setPadding(0, 0, 0, 0);

    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    int width = wm.getDefaultDisplay().getWidth();
    tab.setMaxWidth(width / tabCount - tabPadding * 2);
    tab.setEllipsize(TruncateAt.END);
    tab.setSingleLine();
    addTab(position, tab);
}

From source file:com.sonvp.tooltip.Tooltip.java

@Override
public boolean onPreDraw() {
    container.getViewTreeObserver().removeOnPreDrawListener(this);

    Context context = container.getContext();
    if (!(context instanceof Activity)) {
        return false;
    }//from   ww w. j av a2 s  .co  m
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int displayWidth = displayMetrics.widthPixels;
    int displayHeight = displayMetrics.heightPixels;
    int displayTop = getStatusBarHeight();

    int anchorTop = rectAnchorView.top;
    int anchorLeft = rectAnchorView.left;
    int anchorWidth = anchorView.getWidth();
    int anchorHeight = anchorView.getHeight();

    int textWidth = viewTooltip.getWidth();
    //default height 1 line
    int textHeight = viewTooltip.getHeight();
    int arrowWidth = arrow.getWidth();
    int arrowHeight = arrow.getHeight();

    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(displayWidth, View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

    if (gravity == Gravity.TOP || gravity == Gravity.BOTTOM) {
        int width = Math.max(textWidth, arrowWidth);
        int height = textHeight + arrowHeight;

        int leftPadding;
        int topPadding;

        if (gravity == Gravity.TOP) {
            topPadding = anchorTop - height;
        } else {
            // gravity == Gravity.BOTTOM
            topPadding = anchorTop + anchorHeight;
        }

        int anchorHorizontalCenter = anchorLeft + anchorWidth / 2;
        int left = anchorHorizontalCenter - width / 2;
        int right = left + width;
        leftPadding = Math.max(0, right > displayWidth ? displayWidth - width : left);

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) arrow.getLayoutParams();
        layoutParams.leftMargin = anchorHorizontalCenter - leftPadding - arrowWidth / 2;
        arrow.setLayoutParams(layoutParams);
        popupWindow.update(leftPadding, topPadding, container.getWidth(), container.getHeight());

        pivotX = width / 2;
        pivotY = gravity == Gravity.TOP ? height : 0;
    } else {
        // gravity == Gravity.LEFT || gravity == Gravity.RIGHT

        int width = textWidth + arrowWidth;

        int leftPadding;
        int topPadding;
        int rightPadding = 0;

        if (gravity == Gravity.LEFT) {
            leftPadding = Math.max(0, anchorLeft - width);
            leftPadding += (int) builder.toolTipMargin;
            rightPadding = displayWidth - anchorLeft;
        } else {
            // gravity == Gravity.RIGHT
            leftPadding = anchorLeft + anchorWidth;
            rightPadding = (int) builder.toolTipMargin;
        }

        if (viewTooltip instanceof TextView) {
            TextView text = (TextView) viewTooltip;
            text.setMaxWidth(displayWidth - rightPadding - leftPadding - arrowWidth);
            viewTooltip.measure(widthMeasureSpec, heightMeasureSpec);
            textHeight = viewTooltip.getMeasuredHeight(); // height multi line
        }

        int height = Math.max(textHeight, arrowHeight);

        int anchorVerticalCenter = anchorTop + anchorHeight / 2;
        int top = anchorVerticalCenter - height / 2;
        int bottom = top + height;

        if (builder.arrowGravity == Gravity.TOP) {
            top = anchorTop;
            bottom = anchorTop + height;
        } else if (builder.arrowGravity == Gravity.BOTTOM) {
            top = anchorTop + anchorHeight - height;
        }

        topPadding = Math.max(0,
                bottom > displayHeight ? displayHeight - height - (int) builder.toolTipMargin : top);
        topPadding = Math.max(0,
                topPadding < displayTop ? displayTop + (int) builder.toolTipMargin : topPadding);

        container.measure(widthMeasureSpec, heightMeasureSpec);
        int popupWidth = container.getMeasuredWidth();
        int popupHeight = container.getMeasuredHeight();
        popupWindow.update(leftPadding, topPadding, popupWidth, popupHeight);

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) arrow.getLayoutParams();
        layoutParams.topMargin = anchorVerticalCenter - topPadding - arrowHeight / 2;
        arrow.setLayoutParams(layoutParams);

        pivotX = gravity == Gravity.LEFT ? popupWidth : 0;
        pivotY = anchorVerticalCenter - topPadding;
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
        container.setAlpha(0.0F);
        container.setPivotX(pivotX);
        container.setPivotY(pivotY);
        container.setScaleX(0.0F);
        container.setScaleY(0.0F);
        container.animate().setDuration(ANIMATION_DURATION).scaleX(1.0F).scaleY(1.0F).alpha(1.0F);
    } else {
        AnimationSet animationSet = new AnimationSet(true);
        animationSet.setDuration(ANIMATION_DURATION);
        animationSet.addAnimation(new AlphaAnimation(0.0F, 1.0F));
        animationSet.addAnimation(new ScaleAnimation(0.0F, 1.0F, 0.0F, 1.0F, pivotX, pivotY));
        container.startAnimation(animationSet);
    }

    return false;
}

From source file:ru.adios.budgeter.widgets.DataTableLayout.java

private int addDataRow(Iterable<String> dataSet, int rowId, Optional<Consumer<TextView>> optional) {
    final Context context = getContext();

    if (rowsPerSet > 1) {
        addView(constructRowSeparator(rowsPerSet), rowId++);
    }/* w w w .  j a v a  2 s.  c  om*/

    int i = 0;
    boolean firstInner = true, fistRow = true;
    TableRow currentRow = constructRow(context, itemsInFirstRow * 2f);
    for (final String str : dataSet) {
        if (i > 0 && (fistRow ? i % itemsInFirstRow == 0 : i % itemsPerInnerRow == 0)) {
            i = 0;
            fistRow = false;
            addView(currentRow, rowId++);
            currentRow = constructRow(context, itemsPerInnerRow * 2f);
            firstInner = true;
        }

        final TextView textView;
        if (firstInner) {
            textView = createSpyingColumnForTableRow(str, rowId, 2f, context);
            firstInner = false;
        } else {
            textView = createColumnForTableRow(str, 2f, context);
        }
        final Optional<Integer> maxWidth = dataStore.getMaxWidthForData(i);
        if (maxWidth.isPresent()) {
            textView.setMaxWidth(UiUtils.dpAsPixels(context, maxWidth.get()));
        }

        if (optional.isPresent()) {
            optional.get().accept(textView);
        }

        currentRow.addView(textView);

        i++;
    }

    addView(currentRow, rowId++);

    return rowId;
}

From source file:com.thelastcrusade.soundstream.util.MusicListAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View element = convertView;/*  w  w w. ja v  a2 s.  c o  m*/

    if (element == null) {
        LayoutInflater inflater = (LayoutInflater) this.mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        element = inflater.inflate(R.layout.song_item, null);
    }

    View userColor = (View) element.findViewById(R.id.user_color);
    TextView title = (TextView) element.findViewById(R.id.title);
    TextView album = (TextView) element.findViewById(R.id.album);
    TextView artist = (TextView) element.findViewById(R.id.artist);
    ImageButton addButton = (ImageButton) element.findViewById(R.id.btn_add_to_playlist);

    String macAddress = metadataList.get(position).getMacAddress();

    User user = users.getUserByMACAddress(macAddress);
    if (user != null) {
        userColor.setBackgroundColor(user.getColor());
    } else {
        Log.wtf(TAG, "User with mac address " + macAddress + " not found.  Using default color.");
        userColor.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
    }

    //set the default sizes
    userColor.setMinimumHeight((int) mContext.getResources().getDimension(R.dimen.song_height));
    title.setSingleLine(true);
    artist.setSingleLine(true);
    album.setSingleLine(true);

    addButton.setTag(position);
    addButton.setContentDescription(
            ContentDescriptionUtils.addToPlaylistAppendSongTitle(metadataList.get(position)));

    title.setText(metadataList.get(position).getTitle());
    artist.setText(metadataList.get(position).getArtist());
    album.setText(metadataList.get(position).getAlbum());

    artist.setMaxWidth(parent.getWidth() / 2);
    /*
    final GestureDetectorCompat songGesture = new GestureDetectorCompat(mContext, new SongGestureListener(element));
    element.setOnTouchListener(new View.OnTouchListener() {       
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      return songGesture.onTouchEvent(event);
    }
    });*/
    return element;
}