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.jei.widgettest.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  ww  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);

    // Weight ?? .
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    textView.setWidth(size.x / mViewPager.getAdapter().getCount());

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

    return textView;
}

From source file:com.yanshi.my36kr.common.view.slidingTab.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 www .  ja v a2 s  .co m*/
        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 (screenX != 0)
                tabTitleView.setWidth(screenX);
        }

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

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

        tabTitleView.setText(adapter.getPageTitle(i));
        tabView.setOnClickListener(tabClickListener);

        mTabStrip.addView(tabView);
    }
}

From source file:toolbarslidingtab.SlidingTabLayout.java

private void populateTabStrip() {
    final MyFragPagerAdapter adapter = (MyFragPagerAdapter) mViewPager.getAdapter();
    final View.OnClickListener tabClickListener = new TabClickListener();

    for (int i = 0; i < adapter.getCount(); i++) {
        View tabView = null;//from ww  w .ja  va  2  s. c  o m
        ImageView tableImg = null;
        TextView tabTitleView = null;
        ImageView tableUnderln = 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);

            tableImg = (ImageView) tabView.findViewById(mTabViewImageVId);
            tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
            tabTitleView.setWidth(screenX);
            tableUnderln = (ImageView) tabView.findViewById(mUnderLineId);

        }

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

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

        tabTitleView.setText(adapter.getPageTitle(i));
        if (tableImg != null)
            tableImg.setImageResource(adapter.getImageId(i));
        tabView.setOnClickListener(tabClickListener);

        mTabStrip.addView(tabView);
    }
}

From source file:org.mozilla.mozstumbler.client.mapview.MapFragment.java

private void initTextView(int textViewId, String bound) {
    TextView textView = (TextView) mRootView.findViewById(textViewId);
    Paint textPaint = textView.getPaint();
    int width = (int) Math.ceil(textPaint.measureText(bound));
    textView.setWidth(width);
    android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(width,
            android.widget.LinearLayout.LayoutParams.MATCH_PARENT);
    textView.setLayoutParams(params);/*  w ww  .jav  a 2 s  . c o m*/
    textView.setText("0");
}

From source file:at.alladin.rmbt.android.map.overlay.RMBTBalloonOverlayView.java

public void setBalloonData(final RMBTBalloonOverlayItem item, final ViewGroup parent) {
    // map our custom item data to fields
    //        title.setText(item.getTitle());
    resultItems = item.getResultItems();

    resultListView.removeAllViews();//from  w w w  .  j  a v  a2 s . c  o  m

    final float scale = getResources().getDisplayMetrics().density;

    final int leftRightItem = Helperfunctions.dpToPx(5, scale);
    final int topBottomItem = Helperfunctions.dpToPx(3, scale);

    final int leftRightDiv = Helperfunctions.dpToPx(0, scale);
    final int topBottomDiv = Helperfunctions.dpToPx(0, scale);
    final int heightDiv = Helperfunctions.dpToPx(1, scale);

    final int topBottomImg = Helperfunctions.dpToPx(1, scale);

    if (resultItems != null && resultItems.length() > 0) {

        for (int i = 0; i < 1; i++)
            // JSONObject resultListItem;
            try {
                final JSONObject result = resultItems.getJSONObject(i);

                final LayoutInflater resultInflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                final View resultView = resultInflater.inflate(R.layout.balloon_overlay_listitem, parent);

                final LinearLayout measurementLayout = (LinearLayout) resultView
                        .findViewById(R.id.resultMeasurementList);
                measurementLayout.setVisibility(View.GONE);

                final LinearLayout netLayout = (LinearLayout) resultView.findViewById(R.id.resultNetList);
                netLayout.setVisibility(View.GONE);

                final TextView measurementHeader = (TextView) resultView.findViewById(R.id.resultMeasurement);
                measurementHeader.setVisibility(View.GONE);

                final TextView netHeader = (TextView) resultView.findViewById(R.id.resultNet);
                netHeader.setVisibility(View.GONE);

                final TextView dateHeader = (TextView) resultView.findViewById(R.id.resultDate);
                dateHeader.setVisibility(View.GONE);

                dateHeader.setText(result.optString("time_string"));

                final JSONArray measurementArray = result.getJSONArray("measurement");

                final JSONArray netArray = result.getJSONArray("net");

                for (int j = 0; j < measurementArray.length(); j++) {

                    final JSONObject singleItem = measurementArray.getJSONObject(j);

                    final LinearLayout measurememtItemLayout = new LinearLayout(context); // (LinearLayout)measurememtItemView.findViewById(R.id.measurement_item);

                    measurememtItemLayout.setLayoutParams(
                            new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

                    measurememtItemLayout.setGravity(Gravity.CENTER_VERTICAL);
                    measurememtItemLayout.setPadding(leftRightItem, topBottomItem, leftRightItem,
                            topBottomItem);

                    final TextView itemTitle = new TextView(context, null, R.style.balloonResultItemTitle);
                    itemTitle.setLayoutParams(
                            new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 0.4f));
                    itemTitle.setTextAppearance(context, R.style.balloonResultItemTitle);
                    itemTitle.setWidth(0);
                    itemTitle.setGravity(Gravity.LEFT);
                    itemTitle.setText(singleItem.getString("title"));

                    measurememtItemLayout.addView(itemTitle);

                    final ImageView itemClassification = new ImageView(context);
                    itemClassification.setLayoutParams(
                            new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                                    android.view.ViewGroup.LayoutParams.MATCH_PARENT, 0.1f));
                    itemClassification.setPadding(0, topBottomImg, 0, topBottomImg);
                    // itemClassification.set setGravity(Gravity.LEFT);

                    itemClassification.setImageDrawable(getResources().getDrawable(
                            Helperfunctions.getClassificationImage(singleItem.getInt("classification"))));

                    measurememtItemLayout.addView(itemClassification);

                    final TextView itemValue = new TextView(context, null, R.style.balloonResultItemValue);
                    itemValue.setLayoutParams(
                            new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f));
                    itemValue.setTextAppearance(context, R.style.balloonResultItemValue);
                    itemValue.setWidth(0);
                    itemValue.setGravity(Gravity.LEFT);
                    itemValue.setText(singleItem.getString("value"));

                    measurememtItemLayout.addView(itemValue);

                    measurementLayout.addView(measurememtItemLayout);

                    final View divider = new View(context);
                    divider.setLayoutParams(new LinearLayout.LayoutParams(
                            android.view.ViewGroup.LayoutParams.MATCH_PARENT, heightDiv, 1));
                    divider.setPadding(leftRightDiv, topBottomDiv, leftRightDiv, topBottomDiv);

                    divider.setBackgroundResource(R.drawable.bg_trans_light_10);

                    measurementLayout.addView(divider);

                    measurementLayout.invalidate();
                }

                for (int j = 0; j < netArray.length(); j++) {

                    final JSONObject singleItem = netArray.getJSONObject(j);

                    final LinearLayout netItemLayout = new LinearLayout(context); // (LinearLayout)measurememtItemView.findViewById(R.id.measurement_item);

                    netItemLayout.setLayoutParams(
                            new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
                    netItemLayout.setPadding(leftRightItem, topBottomItem, leftRightItem, topBottomItem);

                    netItemLayout.setGravity(Gravity.CENTER_VERTICAL);

                    final TextView itemTitle = new TextView(context, null, R.style.balloonResultItemTitle);
                    itemTitle.setLayoutParams(
                            new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 0.4f));
                    itemTitle.setTextAppearance(context, R.style.balloonResultItemTitle);
                    itemTitle.setWidth(0);
                    itemTitle.setGravity(Gravity.LEFT);
                    itemTitle.setText(singleItem.getString("title"));

                    netItemLayout.addView(itemTitle);

                    final ImageView itemClassification = new ImageView(context);
                    itemClassification.setLayoutParams(
                            new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                                    android.view.ViewGroup.LayoutParams.MATCH_PARENT, 0.1f));
                    itemClassification.setPadding(0, topBottomImg, 0, topBottomImg);

                    itemClassification.setImageDrawable(
                            context.getResources().getDrawable(R.drawable.traffic_lights_none));
                    netItemLayout.addView(itemClassification);

                    final TextView itemValue = new TextView(context, null, R.style.balloonResultItemValue);
                    itemValue.setLayoutParams(
                            new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f));
                    itemValue.setTextAppearance(context, R.style.balloonResultItemValue);
                    itemValue.setWidth(0);
                    itemValue.setGravity(Gravity.LEFT);
                    itemValue.setText(singleItem.optString("value", null));

                    netItemLayout.addView(itemValue);

                    netLayout.addView(netItemLayout);

                    final View divider = new View(context);
                    divider.setLayoutParams(new LinearLayout.LayoutParams(
                            android.view.ViewGroup.LayoutParams.MATCH_PARENT, heightDiv, 1));
                    divider.setPadding(leftRightDiv, topBottomDiv, leftRightDiv, topBottomDiv);

                    divider.setBackgroundResource(R.drawable.bg_trans_light_10);

                    netLayout.addView(divider);

                    netLayout.invalidate();
                }

                measurementHeader.setVisibility(View.VISIBLE);
                netHeader.setVisibility(View.VISIBLE);

                measurementLayout.setVisibility(View.VISIBLE);
                netLayout.setVisibility(View.VISIBLE);

                dateHeader.setVisibility(View.VISIBLE);

                resultListView.addView(resultView);

                Log.d(DEBUG_TAG, "View Added");
                // codeText.setText(resultListItem.getString("sync_code"));

            } catch (final JSONException e) {
                e.printStackTrace();
            }

        progessBar.setVisibility(View.GONE);
        emptyView.setVisibility(View.GONE);

        resultListView.setVisibility(View.VISIBLE);

        resultListView.invalidate();
    } else {
        Log.i(DEBUG_TAG, "LEERE LISTE");
        progessBar.setVisibility(View.GONE);
        emptyView.setVisibility(View.VISIBLE);
        emptyView.setText(context.getString(R.string.error_no_data));
        emptyView.invalidate();
    }
}

From source file:org.mixare.MixListView.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //      mixCtx = MixView.ctx;
    dataView = MixView.dataView;/*from   www .j av  a  2 s  . com*/
    ctx = this;
    mixContext = dataView.getContext();

    switch (list) {
    case 1:
        adapter = new ListItemAdapter(this);
        //adapter.colorSource(getDataSource());
        getListView().setTextFilterEnabled(true);

        setListAdapter(adapter);
        break;

    case 2:
        selectedItem = new Vector<Marker>();
        listViewMenu = new Vector<SpannableString>();
        DataHandler jLayer = dataView.getDataHandler();
        /*add all marker items to a title and a URL Vector*/
        for (int i = 0; i < jLayer.getMarkerCount(); i++) {
            Marker ma = jLayer.getMarker(i);
            if (ma.isActive()) {
                listViewMenu.add(new SpannableString(ma.getTitle()));
                selectedItem.add(ma);
            }
        }

        if (dataView.isFrozen()) {

            TextView searchNotificationTxt = new TextView(this);
            searchNotificationTxt.setVisibility(View.VISIBLE);
            searchNotificationTxt.setText(getString(DataView.SEARCH_ACTIVE_1) + " "
                    + DataSourceList.getDataSourcesStringList() + getString(DataView.SEARCH_ACTIVE_2));
            searchNotificationTxt.setWidth(MixView.dWindow.getWidth());

            searchNotificationTxt.setPadding(10, 2, 0, 0);
            searchNotificationTxt.setBackgroundColor(Color.DKGRAY);
            searchNotificationTxt.setTextColor(Color.WHITE);

            getListView().addHeaderView(searchNotificationTxt);

        }

        setListAdapter(
                new ArrayAdapter<SpannableString>(this, android.R.layout.simple_list_item_1, listViewMenu));
        getListView().setTextFilterEnabled(true);
        break;
    case 3:
        String addr = this.getIntent().getExtras().getString("address");
        Map<String, Location> locs = DataInterface.getRouteSuggestions(addr);

        suggestionItems = new Vector<String>();
        suggestionLocations = new Vector<Location>();

        for (Map.Entry<String, Location> l : locs.entrySet()) {
            suggestionItems.add(l.getKey());
            suggestionLocations.add(l.getValue());
        }

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, suggestionItems));
        getListView().setTextFilterEnabled(true);
        break;
    }
}

From source file:by.airport.airport_timetable.helpers.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)}.// ww w .ja v  a2 s .  com
 */
protected TextView createDefaultTabView(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);

    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.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    TypedValue outValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
    textView.setBackgroundResource(outValue.resourceId);
    textView.setAllCaps(true);

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);
    textView.setWidth(size.x / 2);

    return textView;
}

From source file:scroll.com.rdemo.scroll.PagerSlidingTabStrip.java

private void addTextTab(final int position, String title) {
    TextView tab = new TextView(getContext());
    tab.setText(title);/*from ww w  .  j  ava  2  s.  c  o  m*/
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();
    tab.setTextSize(tabTextSize);
    if (tabTextWidth > 0) {
        int tabWidth = tabTextWidth;
        if (tabTextAugmenter > 0 && title.length() > 3) {
            tabWidth += tabTextAugmenter * (title.length() - 2);
        }
        tab.setWidth(tabWidth);
    }
    addTab(position, tab);
}

From source file:de.tobiasbielefeld.solitaire.games.Game.java

/**
 * Create a textView and add it to the given layout (game content). Used to add custom texts
 * to a game. This also sets the text apperance to AppCompat and the gravity to center.
 * The width and height is also measured, so you can use it directly.
 *
 * @param width The width to apply to the
 * @param layout he textView will be added to this layout
 * @param context Context to create view
 *///from   w  ww  . j  av  a 2s  .  c o m
protected void addTextViews(int count, int width, RelativeLayout layout, Context context) {

    for (int i = 0; i < count; i++) {
        TextView textView = new TextView(context);
        textView.setWidth(width);
        TextViewCompat.setTextAppearance(textView, R.style.TextAppearance_AppCompat);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(Color.rgb(0, 0, 0));
        layout.addView(textView);
        textView.measure(0, 0);
        textViews.add(textView);
    }
}

From source file:com.oldfeel.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)}./*ww  w. j  ava  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);

    TypedValue outValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
    textView.setBackgroundResource(outValue.resourceId);
    textView.setAllCaps(true);

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

    int count = mViewPager.getAdapter().getCount();
    if (count > 0 && count <= 4) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        textView.setWidth(size.x / count);
    }
    return textView;
}