Example usage for android.widget TextView setWidth

List of usage examples for android.widget TextView setWidth

Introduction

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

Prototype

@android.view.RemotableViewMethod
public void setWidth(int pixels) 

Source Link

Document

Sets the width of the TextView to be exactly pixels wide.

Usage

From source file:com.av.benzandroid.views.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.// w  w w .j av  a2  s . com
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    //Fit tabs in parent view width
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    textView.setWidth(size.x / mRowCount);

    textView.setTextColor(DEFAULT_TEXTVIEW_COLOR);
    //        textView.setTextColor(SELECTED_TEXTVIEW_COLOR);
    return textView;
}

From source file:com.syaona.petalierapp.view.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}./*  w w w  .j a v  a  2s  .com*/
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(false);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    //Fit tabs in parent view width
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    textView.setWidth(size.x / mRowCount);

    textView.setTextColor(DEFAULT_TEXTVIEW_COLOR);
    //        textView.setTextColor(SELECTED_TEXTVIEW_COLOR);
    return textView;
}

From source file:view.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}./*from w w w .j  ava 2  s  . c om*/
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);
    //TODO
    //upgreat it
    //I've added it to make them even
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    textView.setWidth(size.x / 2);

    return textView;
}

From source file:com.example.fabian.matchit.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.//www.j  a va 2 s.  c o m
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    //textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setTextColor(getResources().getColor(R.color.white));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);
    // Text evenredig verdelen
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    textView.setWidth(size.x / 3);

    return textView;
}

From source file:com.example.rachid.myapplication.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.//from  w  w  w  . ja v  a2s .c o  m
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    // AADIDO: Ajustar pantalla
    // ---------------------------------------------------------------------------------------
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    textView.setWidth(size.x / 3);
    // ---------------------------------------------------------------------------------------

    return textView;
}

From source file:com.sudeep23.lollipoptabs.fixedtab.FixedTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.// ww  w .  j  a  v a 2s. c o m
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    int width = 0;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
        Point size = new Point();
        display.getSize(size);
        width = size.x;
    } else {
        width = display.getWidth(); // deprecated
    }
    textView.setWidth(width / tabCount);

    return textView;
}

From source file:agilec.ikeaswipe.utils.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}.//from   ww w .  ja v  a2 s. c  o  m
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    // TODO: Use setCustomTabView(int, int) instead of the following modification to createDefaultTabView.
    // Get the screen size.
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;

    // Get the number of tabs in the viewPager.
    final PagerAdapter adapter = mViewPager.getAdapter();

    // Set the width of the textViews to fill up the entire screen.
    textView.setWidth(width / adapter.getCount());

    return textView;
}

From source file:com.juick.android.MessagesFragment.java

public void lastPrepareMessages(final List<JuickMessage> list, final Runnable then) {
    databaseGetter.getService(new Utils.ServiceGetter.Receiver<DatabaseService>() {
        @Override// w ww .  j  ava 2 s.com
        public void withService(final DatabaseService service) {
            new Thread("processLastReads cont") {
                @Override
                public void run() {
                    for (JuickMessage juickMessage : list) {
                        // we can use service inside here, because getMessageReadStatus0 is thread-safe
                        final DatabaseService.MessageReadStatus messageReadStatus0 = service
                                .getMessageReadStatus0(juickMessage.getMID());
                        if (messageReadStatus0.read) {
                            juickMessage.read = true;
                            juickMessage.readComments = messageReadStatus0.nreplies;
                        }
                    }

                    final Activity act = getActivity();

                    if (act != null) {
                        if (sp.getBoolean("text_accelerated", false)) {
                            // accelerate text draw

                            final Bitmap bm = Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8);
                            while (MessagesFragment.this.getView().getMeasuredWidth() == 0) {
                                try {
                                    Thread.sleep(100);
                                } catch (InterruptedException e) {
                                    e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
                                }
                            }

                            TextView tv = new TextView(act);
                            tv.setTextSize(JuickMessagesAdapter.getTextSize(act));
                            MainActivity.restyleChildrenOrWidget(tv);
                            final int outerViewWidth = MessagesFragment.this.getView().getMeasuredWidth();
                            final int width = outerViewWidth - 6;
                            tv.setWidth(width);
                            tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT));
                            final ArrayList<JuickMessagesAdapter.CanvasPainter> stuff = new ArrayList<JuickMessagesAdapter.CanvasPainter>();

                            Canvas canvas = new Canvas(bm) {
                                @Override
                                public void drawText(char[] text, int index, int count, float x, float y,
                                        Paint paint) {
                                    super.drawText(text, index, count, x, y, paint); //To change body of overridden methods use File | Settings | File Templates.
                                }

                                @Override
                                public void drawText(String text, float x, float y, Paint paint) {
                                    super.drawText(text, x, y, paint); //To change body of overridden methods use File | Settings | File Templates.
                                }

                                @Override
                                public void drawText(String text, int start, int end, float x, float y,
                                        Paint paint) {
                                    super.drawText(text, start, end, x, y, paint); //To change body of overridden methods use File | Settings | File Templates.
                                }

                                @Override
                                public void drawText(CharSequence text, int start, int end, float x, float y,
                                        Paint paint) {
                                    super.drawText(text, start, end, x, y, paint); //To change body of overridden methods use File | Settings | File Templates.
                                }

                                @Override
                                public void drawVertices(VertexMode mode, int vertexCount, float[] verts,
                                        int vertOffset, float[] texs, int texOffset, int[] colors,
                                        int colorOffset, short[] indices, int indexOffset, int indexCount,
                                        Paint paint) {
                                    super.drawVertices(mode, vertexCount, verts, vertOffset, texs, texOffset,
                                            colors, colorOffset, indices, indexOffset, indexCount, paint); //To change body of overridden methods use File | Settings | File Templates.
                                }

                                @Override
                                public void drawPosText(char[] text, int index, int count, float[] pos,
                                        Paint paint) {
                                    super.drawPosText(text, index, count, pos, paint); //To change body of overridden methods use File | Settings | File Templates.
                                }

                                @Override
                                public void drawPosText(String text, float[] pos, Paint paint) {
                                    super.drawPosText(text, pos, paint); //To change body of overridden methods use File | Settings | File Templates.
                                }

                                @Override
                                public void drawTextOnPath(char[] text, int index, int count, Path path,
                                        float hOffset, float vOffset, Paint paint) {
                                    super.drawTextOnPath(text, index, count, path, hOffset, vOffset, paint); //To change body of overridden methods use File | Settings | File Templates.
                                }

                                @Override
                                public void drawTextOnPath(String text, Path path, float hOffset, float vOffset,
                                        Paint paint) {
                                    super.drawTextOnPath(text, path, hOffset, vOffset, paint); //To change body of overridden methods use File | Settings | File Templates.
                                }

                                public void drawTextRun(final CharSequence text, final int start, final int end,
                                        final int contextStart, final int contextEnd, final float x,
                                        final float y, final int dir, final Paint paint) {
                                    stuff.add(new JuickMessagesAdapter.CanvasPainter() {

                                        Paint paintClone = getPaintFromCache(paint);

                                        @Override
                                        public void paintOnCanvas(Canvas c, Paint workPaint) {
                                            try {
                                                drawTextRun.invoke(c, text, start, end, contextStart,
                                                        contextEnd, x, y, dir, paintClone);
                                            } catch (Throwable t) {
                                            }
                                        }
                                    });
                                }

                                @Override
                                public boolean getClipBounds(Rect bounds) {
                                    bounds.top = 0;
                                    bounds.bottom = 100000;
                                    bounds.left = 0;
                                    bounds.right = width;
                                    return true;
                                }
                            };

                            for (JuickMessage juickMessage : list) {
                                JuickMessagesAdapter.ParsedMessage pm = (JuickMessagesAdapter.ParsedMessage) juickMessage.parsedText;
                                if (pm != null && pm.textContent != null)
                                    tv.setText(pm.textContent);
                                stuff.clear();
                                tv.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
                                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                                tv.draw(canvas);
                                if (stuff.size() > 0) {
                                    juickMessage.parsedUI = new JuickMessagesAdapter.RenderedText(
                                            outerViewWidth, tv.getMeasuredHeight(),
                                            new ArrayList<JuickMessagesAdapter.CanvasPainter>(stuff));
                                } else {
                                    juickMessage.parsedUI = null;
                                }
                            }
                        }
                        act.runOnUiThread(then);
                    }
                }
            }.start();
        }
    });
}

From source file:com.duckduckgo.mobile.android.views.SlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}./*from www .ja va2 s .c o  m*/
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.TOP | Gravity.CENTER);
    textView.setTextAppearance(context, R.style.TabTitle);
    textView.setHeight((int) getResources().getDimension(R.dimen.actionbar_tab_height2));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    int halfPadding = (int) (padding * 0.5);
    //textView.setPadding(padding, padding, padding, padding);
    textView.setPadding(padding, halfPadding, padding, padding);

    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    int width;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        Point size = new Point();
        display.getSize(size);
        width = size.x;
    } else {
        width = display.getWidth();
    }

    if (width > getResources().getDimension(R.dimen.tab_small)
            || getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        textView.setWidth(width / mViewPager.getAdapter().getCount());
    }

    return textView;
}

From source file:org.mythdroid.activities.Guide.java

/**
 * Generate a TableRow from the provided Channel
 * @param ch Channel to generate a row for
 * @return populated TableRow representing the channel
 *///from w w  w.j a  v a  2s  . c o m
@SuppressLint("DefaultLocale")
private TableRow getRowFromChannel(Channel ch) {

    final TableRow row = new TableRow(this);
    row.setLayoutParams(rowLayout);

    TextView tv = new TextView(this);
    tv.setBackgroundColor(0xffe0e0f0);
    tv.setTextColor(0xff202020);
    tv.setPadding(4, 4, 4, 4);
    tv.setMaxLines(2);
    tv.setTag(ch.ID);
    tv.setText(ch.num + " " + ch.callSign); //$NON-NLS-1$
    tv.setOnClickListener(chanClickListener);
    tv.setOnLongClickListener(chanLongClickListener);
    tv.setLayoutParams(chanLayout);

    row.addView(tv);

    LayoutParams layout = null;

    Program[] progs = ch.programs.toArray(new Program[ch.programs.size()]);
    int numprogs = progs.length;

    for (int i = 0; i < numprogs; i++) {

        if (progs[i].StartTime.equals(later))
            continue;

        tv = new TextView(this);
        layout = new LayoutParams(this, null);
        layout.topMargin = layout.bottomMargin = layout.leftMargin = layout.rightMargin = 1;
        layout.height = rowHeight;

        String cat = catPat.matcher(progs[i].Category.toLowerCase()).replaceAll(""); //$NON-NLS-1$

        try {
            tv.setBackgroundColor(Category.valueOf(cat).color());
        } catch (IllegalArgumentException e) {
            tv.setBackgroundColor(Category.unknown.color());
        }

        tv.setTextColor(0xfff0f0f0);
        tv.setPadding(4, 4, 4, 4);
        tv.setMaxLines(2);
        tv.setText(progs[i].Title);
        setStatusDrawable(tv, progs[i]);

        int width = setLayoutParams(layout, progs[i]) * colWidth;
        if (width < 1)
            continue;

        tv.setWidth(width);
        tv.setLayoutParams(layout);
        tv.setTag(progs[i]);
        tv.setOnClickListener(progClickListener);
        row.addView(tv);

    }

    return row;

}