Example usage for android.widget TextView setCompoundDrawables

List of usage examples for android.widget TextView setCompoundDrawables

Introduction

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

Prototype

public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right,
        @Nullable Drawable bottom) 

Source Link

Document

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text.

Usage

From source file:org.onebusaway.android.ui.ArrivalsListAdapterStyleB.java

@Override
protected void initView(final View view, CombinedArrivalInfoStyleB combinedArrivalInfoStyleB) {
    final ArrivalInfo stopInfo = combinedArrivalInfoStyleB.getArrivalInfoList().get(0);
    final ObaArrivalInfo arrivalInfo = stopInfo.getInfo();
    final Context context = getContext();

    LayoutInflater inflater = LayoutInflater.from(context);

    TextView routeName = (TextView) view.findViewById(R.id.routeName);
    TextView destination = (TextView) view.findViewById(R.id.routeDestination);

    // TableLayout that we will fill with TableRows of arrival times
    TableLayout arrivalTimesLayout = (TableLayout) view.findViewById(R.id.arrivalTimeLayout);
    arrivalTimesLayout.removeAllViews();

    Resources r = view.getResources();

    ImageButton starBtn = (ImageButton) view.findViewById(R.id.route_star);
    starBtn.setColorFilter(r.getColor(R.color.theme_primary));

    ImageButton mapImageBtn = (ImageButton) view.findViewById(R.id.mapImageBtn);
    mapImageBtn.setColorFilter(r.getColor(R.color.theme_primary));

    ImageButton routeMoreInfo = (ImageButton) view.findViewById(R.id.route_more_info);
    routeMoreInfo.setColorFilter(r.getColor(R.color.switch_thumb_normal_material_dark));

    starBtn.setImageResource(//  w  w w.j  a  v  a2s .  c o m
            stopInfo.isRouteAndHeadsignFavorite() ? R.drawable.focus_star_on : R.drawable.focus_star_off);

    starBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Show dialog for setting route favorite
            RouteFavoriteDialogFragment dialog = new RouteFavoriteDialogFragment.Builder(
                    stopInfo.getInfo().getRouteId(), stopInfo.getInfo().getHeadsign())
                            .setRouteShortName(stopInfo.getInfo().getShortName())
                            .setRouteLongName(stopInfo.getInfo().getRouteLongName())
                            .setStopId(stopInfo.getInfo().getStopId())
                            .setFavorite(!stopInfo.isRouteAndHeadsignFavorite()).build();

            dialog.setCallback(new RouteFavoriteDialogFragment.Callback() {
                @Override
                public void onSelectionComplete(boolean savedFavorite) {
                    if (savedFavorite) {
                        mFragment.refreshLocal();
                    }
                }
            });
            dialog.show(mFragment.getFragmentManager(), RouteFavoriteDialogFragment.TAG);
        }
    });

    // Setup map
    mapImageBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mFragment.showRouteOnMap(stopInfo);
        }
    });

    // Setup more
    routeMoreInfo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mFragment.showListItemMenu(view, stopInfo);
        }
    });

    routeName.setText(arrivalInfo.getShortName());
    destination.setText(MyTextUtils.toTitleCase(arrivalInfo.getHeadsign()));

    // Loop through the arrival times and create the TableRows that contains the data
    for (int i = 0; i < combinedArrivalInfoStyleB.getArrivalInfoList().size(); i++) {
        final ArrivalInfo arrivalRow = combinedArrivalInfoStyleB.getArrivalInfoList().get(i);
        final ObaArrivalInfo tempArrivalInfo = arrivalRow.getInfo();
        long scheduledTime = tempArrivalInfo.getScheduledArrivalTime();

        // Create a new row to be added
        final TableRow tr = (TableRow) inflater.inflate(R.layout.arrivals_list_tr_template_style_b, null);

        // Layout and views to inflate from XML templates
        RelativeLayout layout;
        TextView scheduleView, estimatedView, statusView;
        View divider;

        if (i == 0) {
            // Use larger styled layout/view for next arrival time
            layout = (RelativeLayout) inflater.inflate(R.layout.arrivals_list_rl_template_style_b_large, null);
            scheduleView = (TextView) inflater
                    .inflate(R.layout.arrivals_list_tv_template_style_b_schedule_large, null);
            estimatedView = (TextView) inflater
                    .inflate(R.layout.arrivals_list_tv_template_style_b_estimated_large, null);
            statusView = (TextView) inflater.inflate(R.layout.arrivals_list_tv_template_style_b_status_large,
                    null);
        } else {
            // Use smaller styled layout/view for further out times
            layout = (RelativeLayout) inflater.inflate(R.layout.arrivals_list_rl_template_style_b_small, null);
            scheduleView = (TextView) inflater
                    .inflate(R.layout.arrivals_list_tv_template_style_b_schedule_small, null);
            estimatedView = (TextView) inflater
                    .inflate(R.layout.arrivals_list_tv_template_style_b_estimated_small, null);
            statusView = (TextView) inflater.inflate(R.layout.arrivals_list_tv_template_style_b_status_small,
                    null);
        }

        // Set arrival times and status in views
        scheduleView.setText(DateUtils.formatDateTime(context, scheduledTime,
                DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT));
        if (arrivalRow.getPredicted()) {
            long eta = arrivalRow.getEta();
            if (eta == 0) {
                estimatedView.setText(R.string.stop_info_eta_now);
            } else {
                estimatedView.setText(eta + " min");
            }
        } else {
            estimatedView.setText(R.string.stop_info_eta_unknown);
        }
        statusView.setText(arrivalRow.getStatusText());
        int colorCode = arrivalRow.getColor();
        statusView.setBackgroundResource(R.drawable.round_corners_style_b_status);
        GradientDrawable d = (GradientDrawable) statusView.getBackground();
        d.setColor(context.getResources().getColor(colorCode));

        int alpha;
        if (i == 0) {
            // Set next arrival
            alpha = (int) (1.0f * 255); // X percent transparency
        } else {
            // Set smaller rows
            alpha = (int) (.35f * 255); // X percent transparency
        }
        d.setAlpha(alpha);

        // Set padding on status view
        int pSides = UIUtils.dpToPixels(context, 5);
        int pTopBottom = UIUtils.dpToPixels(context, 2);
        statusView.setPadding(pSides, pTopBottom, pSides, pTopBottom);

        // Add TextViews to layout
        layout.addView(scheduleView);
        layout.addView(statusView);
        layout.addView(estimatedView);

        // Make sure the TextViews align left/center/right of parent relative layout
        RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) scheduleView.getLayoutParams();
        params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params1.addRule(RelativeLayout.CENTER_VERTICAL);
        scheduleView.setLayoutParams(params1);

        RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) statusView.getLayoutParams();
        params2.addRule(RelativeLayout.CENTER_IN_PARENT);
        // Give status view a little extra margin
        int p = UIUtils.dpToPixels(context, 3);
        params2.setMargins(p, p, p, p);
        statusView.setLayoutParams(params2);

        RelativeLayout.LayoutParams params3 = (RelativeLayout.LayoutParams) estimatedView.getLayoutParams();
        params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params3.addRule(RelativeLayout.CENTER_VERTICAL);
        estimatedView.setLayoutParams(params3);

        // Add layout to TableRow
        tr.addView(layout);

        // Add the divider, if its not the first row
        if (i != 0) {
            int dividerHeight = UIUtils.dpToPixels(context, 1);
            divider = inflater.inflate(R.layout.arrivals_list_divider_template_style_b, null);
            divider.setLayoutParams(
                    new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, dividerHeight));
            arrivalTimesLayout.addView(divider);
        }

        // Add click listener
        tr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mFragment.showListItemMenu(tr, arrivalRow);
            }
        });

        // Add TableRow to container layout
        arrivalTimesLayout.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.MATCH_PARENT));
    }

    // Show or hide reminder for this trip
    ContentValues values = null;
    if (mTripsForStop != null) {
        values = mTripsForStop.getValues(arrivalInfo.getTripId());
    }
    if (values != null) {
        String reminderName = values.getAsString(ObaContract.Trips.NAME);

        TextView reminder = (TextView) view.findViewById(R.id.reminder);
        if (reminderName.length() == 0) {
            reminderName = context.getString(R.string.trip_info_noname);
        }
        reminder.setText(reminderName);
        Drawable d = reminder.getCompoundDrawables()[0];
        d = DrawableCompat.wrap(d);
        DrawableCompat.setTint(d.mutate(), view.getResources().getColor(R.color.theme_primary));
        reminder.setCompoundDrawables(d, null, null, null);
        reminder.setVisibility(View.VISIBLE);
    } else {
        // Explicitly set reminder to invisible because we might be reusing
        // this view.
        View reminder = view.findViewById(R.id.reminder);
        reminder.setVisibility(View.GONE);
    }
}

From source file:com.insthub.ecmobile.component.PagerSlidingTabStrip.java

/** add one text tab */
private void addTextTab(final int position, String title, int res) {
    TextView tab = new TextView(getContext());
    tab.setText(title);/* w ww .  j  a va 2  s .  c  o m*/
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();
    Drawable drawable = getResources().getDrawable(res);
    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
    tab.setCompoundDrawables(drawable, null, null, null);
    addTab(position, tab);
}

From source file:com.example.ysh.myapplication.view.readview.TocListAdapter.java

@Override
public void convert(EasyLVHolder holder, int position, BookMixAToc.mixToc.Chapters chapters) {
    TextView tvTocItem = holder.getView(R.id.tvTocItem);
    tvTocItem.setText(chapters.title);/*from w ww  . ja  v a2  s  .c o  m*/
    Drawable drawable;
    if (currentChapter == position + 1) {
        tvTocItem.setTextColor(ContextCompat.getColor(mContext, R.color.light_red));
        drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_toc_item_activated);
    } else if (isEpub || FileUtils.getChapterFile(bookId, position + 1).length() > 10) {
        tvTocItem.setTextColor(ContextCompat.getColor(mContext, R.color.light_black));
        drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_toc_item_download);
    } else {
        tvTocItem.setTextColor(ContextCompat.getColor(mContext, R.color.light_black));
        drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_toc_item_normal);
    }
    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
    tvTocItem.setCompoundDrawables(drawable, null, null, null);
}

From source file:com.common.widget.pageindicator.C_TabPageIndicator.java

/**
 * Icon? /*from w  w w.  j a  va2 s.com*/
 *
 * @param view
 * @param iconResId
 */
private void setTabIcon(TextView view, int iconResId) {
    Drawable drawable = context.getResources().getDrawable(iconResId);
    int h = drawable.getIntrinsicHeight(); // 
    int hs = drawable.getMinimumHeight(); // 
    // int crop = h - hs; // ?
    int crop = h / 5;
    Rect cRect = new Rect(crop, crop, h - crop, h - crop);// ?
    // drawable.setBounds(crop, crop, h - crop, h - crop); // 
    // view.setCompoundDrawablesWithIntrinsicBounds(0, drawable, 0, 0);
    // view.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end,
    view.setCompoundDrawables(null, drawable, null, null);
}

From source file:com.liushengfan.test.customerviewgroup.view.PagerSlidingTabStrip.java

private void addTextTab(final int position, String title) {

    TextView tab = new TextView(getContext());
    tab.setText(title);/*from ww  w. j  a va2  s. co  m*/
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();
    // add arrow
    if (compoundTabIndex != -1 && compoundDrawable != null && position == compoundTabIndex) {

        int h = DensityUtil.dip2px(getContext(), tabTextSize - 2);

        compoundDrawable.setBounds(0, 0, h / 3, h);

        tab.setCompoundDrawables(null, null, compoundDrawable, null);
    }
    addTab(position, tab);
}

From source file:com.android.inputmethod.latin.suggestions.SuggestionStripLayoutHelper.java

private int layoutPunctuationsAndReturnStartIndexOfMoreSuggestions(
        final PunctuationSuggestions punctuationSuggestions, final ViewGroup stripView) {
    final int countInStrip = Math.min(punctuationSuggestions.size(), PUNCTUATIONS_IN_STRIP);
    for (int positionInStrip = 0; positionInStrip < countInStrip; positionInStrip++) {
        if (positionInStrip != 0) {
            // Add divider if this isn't the left most suggestion in suggestions strip.
            addDivider(stripView, mDividerViews.get(positionInStrip));
        }/*www  . j  a  v  a 2  s. com*/

        final TextView wordView = mWordViews.get(positionInStrip);
        final String punctuation = punctuationSuggestions.getLabel(positionInStrip);
        // {@link TextView#getTag()} is used to get the index in suggestedWords at
        // {@link SuggestionStripView#onClick(View)}.
        wordView.setTag(positionInStrip);
        wordView.setText(punctuation);
        wordView.setContentDescription(punctuation);
        wordView.setTextScaleX(1.0f);
        wordView.setCompoundDrawables(null, null, null, null);
        wordView.setTextColor(mColorAutoCorrect);
        stripView.addView(wordView);
        setLayoutWeight(wordView, 1.0f, mSuggestionsStripHeight);
    }
    mMoreSuggestionsAvailable = (punctuationSuggestions.size() > countInStrip);
    return countInStrip;
}

From source file:net.cloudpath.xpressconnect.screens.ScreenBase.java

private void setIcon(int paramInt, TextView paramTextView) {
      switch (paramInt) {
      default://from ww  w  .ja  va2 s  . c o  m
          Drawable localDrawable3 = getResources().getDrawable(this.mParcelHelper.getDrawableId("xpc_blank"));
          localDrawable3.setBounds(0, 0, 20, 20);
          paramTextView.setCompoundDrawables(localDrawable3, null, null, null);
          return;
      case 1:
          Drawable localDrawable2 = getResources()
                  .getDrawable(this.mParcelHelper.getDrawableId("xpc_applicationtabarrow"));
          localDrawable2.setBounds(0, 0, 20, 20);
          paramTextView.setCompoundDrawables(localDrawable2, null, null, null);
          return;
      case 2:
      }
      Drawable localDrawable1 = getResources()
              .getDrawable(this.mParcelHelper.getDrawableId("xpc_applicationtabcheck"));
      localDrawable1.setBounds(0, 0, 20, 20);
      paramTextView.setCompoundDrawables(localDrawable1, null, null, null);
  }

From source file:com.achep.acdisplay.ui.widgets.notification.NotificationActions.java

/**
 * Sets new actions.//  ww w.j a v  a  2s .  c  o  m
 *
 * @param notification the host notification
 * @param actions      the actions to set
 */
public void setActions(@Nullable OpenNotification notification, @Nullable Action[] actions) {
    Check.getInstance().isInMainThread();

    mRemoteInputsMap.clear();
    mActionsMap.clear();
    hideRii();

    if (actions == null) {
        // Free actions' container.
        removeAllViews();
        return;
    } else {
        assert notification != null;
    }

    int count = actions.length;
    View[] views = new View[count];

    // Find available views.
    int childCount = getChildCount();
    int a = Math.min(childCount, count);
    for (int i = 0; i < a; i++) {
        views[i] = getChildAt(i);
    }

    // Remove redundant views.
    for (int i = childCount - 1; i >= count; i--) {
        removeViewAt(i);
    }

    LayoutInflater inflater = null;
    for (int i = 0; i < count; i++) {
        final Action action = actions[i];
        View root = views[i];

        if (root == null) {
            // Initialize layout inflater only when we really need it.
            if (inflater == null) {
                inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                assert inflater != null;
            }

            root = inflater.inflate(getActionLayoutResource(), this, false);
            root = onCreateActionView(root);
            // We need to keep all IDs unique to make
            // TransitionManager.beginDelayedTransition(viewGroup, null)
            // work correctly!
            root.setId(getChildCount() + 1);
            addView(root);
        }

        mActionsMap.put(root, action);

        int style = Typeface.NORMAL;
        root.setOnLongClickListener(null);
        if (action.intent != null) {
            root.setEnabled(true);
            root.setOnClickListener(mActionsOnClick);

            RemoteInput remoteInput = getRemoteInput(action);
            if (remoteInput != null) {
                mRemoteInputsMap.put(action, remoteInput);
                root.setOnLongClickListener(mActionsOnLongClick);

                // Highlight the action
                style = Typeface.ITALIC;
            }
        } else {
            root.setEnabled(false);
            root.setOnClickListener(null);
        }

        // Get message view and apply the content.
        TextView textView = root instanceof TextView ? (TextView) root
                : (TextView) root.findViewById(android.R.id.title);
        textView.setText(action.title);
        if (mTypeface == null)
            mTypeface = textView.getTypeface();
        textView.setTypeface(mTypeface, style);

        Drawable icon = NotificationUtils.getDrawable(getContext(), notification, action.icon);
        if (icon != null)
            icon = onCreateActionIcon(icon);

        if (Device.hasJellyBeanMR1Api()) {
            textView.setCompoundDrawablesRelative(icon, null, null, null);
        } else {
            textView.setCompoundDrawables(icon, null, null, null);
        }
    }
}

From source file:com.example.administrator.mengbaofushiji.extras.SlidingTabLayout.java

private void populateTabStrip() {
    removeOldSelection(); // add those two lines
    oldSelection = null;/*from w w w . j av a 2s.c om*/
    final PagerAdapter adapter = mViewPager.getAdapter();
    final OnClickListener tabClickListener = new TabClickListener();

    for (int i = 0; i < adapter.getCount(); i++) {
        View tabView = null;
        TextView tabTitleView = null;
        if (mTabViewLayoutId != 0) {
            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;
        }

        tabTitleView.setText(adapter.getPageTitle(i));
        if (mTabViewTextViewImgs != null && mTabViewTextViewImgs.length > 0) {
            Drawable drawable = getResources().getDrawable(mTabViewTextViewImgs[i]);
            /// ??,??.
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tabTitleView.setCompoundDrawables(drawable, null, null, null);
        }
        tabView.setOnClickListener(tabClickListener);
        mTabStrip.addView(tabView);
    }
}

From source file:com.tpos.widget.pagertab.PagerSlidingTabStrip.java

private void addIconTextTab(final int position, int resId, String title) {

    TextView tab = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.item_tabstrip_view, null);
    // new TextView(getContext());
    tab.setText(title);/*from  w w w.j a  v  a  2 s  . co  m*/
    tab.setGravity(Gravity.CENTER);
    tab.setSingleLine();

    Drawable tDrawable = null;
    try {
        tDrawable = getResources().getDrawable(resId);
    } catch (NotFoundException e) {
        e.printStackTrace();
        tDrawable = null;
    }
    if (tDrawable != null) {
        tDrawable.setBounds(0, 0, (int) (tDrawable.getIntrinsicWidth() * 0.5),
                (int) (tDrawable.getIntrinsicHeight() * 0.5));
        tab.setCompoundDrawables(null, tDrawable, null, null);
    }

    addTab(position, tab);

}