Example usage for android.widget ImageView setImageResource

List of usage examples for android.widget ImageView setImageResource

Introduction

In this page you can find the example usage for android.widget ImageView setImageResource.

Prototype

@android.view.RemotableViewMethod(asyncImpl = "setImageResourceAsync")
public void setImageResource(@DrawableRes int resId) 

Source Link

Document

Sets a drawable as the content of this ImageView.

Usage

From source file:com.gh4a.fragment.PullRequestFragment.java

private void fillStatus(List<CommitStatus> statuses) {
    Map<String, CommitStatus> statusByContext = new HashMap<>();
    for (CommitStatus status : statuses) {
        if (!statusByContext.containsKey(status.getContext())) {
            statusByContext.put(status.getContext(), status);
        }// w  ww .  j  a v a  2s. c  om
    }

    final int statusIconDrawableAttrId, statusLabelResId;
    if (PullRequest.MERGEABLE_STATE_CLEAN.equals(mPullRequest.getMergeableState())) {
        statusIconDrawableAttrId = R.attr.pullRequestMergeOkIcon;
        statusLabelResId = R.string.pull_merge_status_clean;
    } else if (PullRequest.MERGEABLE_STATE_UNSTABLE.equals(mPullRequest.getMergeableState())) {
        statusIconDrawableAttrId = R.attr.pullRequestMergeUnstableIcon;
        statusLabelResId = R.string.pull_merge_status_unstable;
    } else if (PullRequest.MERGEABLE_STATE_DIRTY.equals(mPullRequest.getMergeableState())) {
        statusIconDrawableAttrId = R.attr.pullRequestMergeDirtyIcon;
        statusLabelResId = R.string.pull_merge_status_dirty;
    } else if (statusByContext.isEmpty()) {
        // unknwon status, no commit statuses -> nothing to display
        return;
    } else {
        statusIconDrawableAttrId = R.attr.pullRequestMergeUnknownIcon;
        statusLabelResId = R.string.pull_merge_status_unknown;
    }

    ImageView statusIcon = (ImageView) mListHeaderView.findViewById(R.id.iv_merge_status_icon);
    statusIcon.setImageResource(UiUtils.resolveDrawable(getActivity(), statusIconDrawableAttrId));

    TextView statusLabel = (TextView) mListHeaderView.findViewById(R.id.merge_status_label);
    statusLabel.setText(statusLabelResId);

    ViewGroup statusContainer = (ViewGroup) mListHeaderView.findViewById(R.id.merge_commit_status_container);
    LayoutInflater inflater = getLayoutInflater(null);
    statusContainer.removeAllViews();
    for (CommitStatus status : statusByContext.values()) {
        View statusRow = inflater.inflate(R.layout.row_commit_status, statusContainer, false);

        String state = status.getState();
        final int iconDrawableAttrId;
        if (CommitStatus.STATE_ERROR.equals(state) || CommitStatus.STATE_FAILURE.equals(state)) {
            iconDrawableAttrId = R.attr.commitStatusFailIcon;
        } else if (CommitStatus.STATE_SUCCESS.equals(state)) {
            iconDrawableAttrId = R.attr.commitStatusOkIcon;
        } else {
            iconDrawableAttrId = R.attr.commitStatusUnknownIcon;
        }
        ImageView icon = (ImageView) statusRow.findViewById(R.id.iv_status_icon);
        icon.setImageResource(UiUtils.resolveDrawable(getActivity(), iconDrawableAttrId));

        TextView context = (TextView) statusRow.findViewById(R.id.tv_context);
        context.setText(status.getContext());

        TextView description = (TextView) statusRow.findViewById(R.id.tv_desc);
        description.setText(status.getDescription());

        statusContainer.addView(statusRow);
    }
    mListHeaderView.findViewById(R.id.merge_commit_no_status)
            .setVisibility(statusByContext.isEmpty() ? View.VISIBLE : View.GONE);

    mListHeaderView.findViewById(R.id.merge_status_container).setVisibility(View.VISIBLE);
}

From source file:com.lastorder.pushnotifications.data.ImageDownloader.java

/**
 * Same as download but the image is always downloaded and the cache is not used.
 * Kept private at the moment as its interest is not clear.
 *///  www  . j  a  va  2s.  c  o  m
private void forceDownload(String url, ImageView imageView, ProgressBar bar) {
    // State sanity: url is guaranteed to never be null in DownloadedDrawable and cache keys.
    if (url == null || !url.contains(".") || url.length() <= 6 || url.contains("null")) {
        bar.setVisibility(8);
        imageView.setImageResource(R.drawable.ic_launcher);
        imageView.setVisibility(0);
        return;
    }

    if (cancelPotentialDownload(url, imageView)) {
        BitmapDownloaderTask task = new BitmapDownloaderTask(imageView, bar);
        DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
        imageView.setImageDrawable(downloadedDrawable);
        imageView.setMinimumHeight(156);
        task.execute(url);
    }
}

From source file:com.android.calendar.event.EventLocationAdapter.java

@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
    View view = convertView;/*from   ww w.j  a  v a2  s.  co m*/
    if (view == null) {
        view = mInflater.inflate(R.layout.location_dropdown_item, parent, false);
    }
    final Result result = getItem(position);
    if (result == null) {
        return view;
    }

    // Update the display name in the item in auto-complete list.
    TextView nameView = (TextView) view.findViewById(R.id.location_name);
    if (nameView != null) {
        if (result.mName == null) {
            nameView.setVisibility(View.GONE);
        } else {
            nameView.setVisibility(View.VISIBLE);
            nameView.setText(result.mName);
        }
    }

    // Update the address line.
    TextView addressView = (TextView) view.findViewById(R.id.location_address);
    if (addressView != null) {
        addressView.setText(result.mAddress);
    }

    // Update the icon.
    final ImageView imageView = (ImageView) view.findViewById(R.id.icon);
    if (imageView != null) {
        if (result.mDefaultIcon == null) {
            imageView.setVisibility(View.INVISIBLE);
        } else {
            imageView.setVisibility(View.VISIBLE);
            imageView.setImageResource(result.mDefaultIcon);

            // Save the URI on the view, so we can check against it later when updating
            // the image.  Otherwise the async image update with using 'convertView' above
            // resulted in the wrong list items being updated.
            imageView.setTag(result.mContactPhotoUri);
            if (result.mContactPhotoUri != null) {
                Bitmap cachedPhoto = mPhotoCache.get(result.mContactPhotoUri);
                if (cachedPhoto != null) {
                    // Use photo in cache.
                    imageView.setImageBitmap(cachedPhoto);
                } else {
                    // Asynchronously load photo and update.
                    asyncLoadPhotoAndUpdateView(result.mContactPhotoUri, imageView);
                }
            }
        }
    }
    return view;
}

From source file:org.hfoss.posit.android.functionplugin.reminder.SetReminder.java

/**
 * Attaches the alarmIcon to all Finds in ListView that currently have an
 * associated Reminder, which is indicated by the Find's
 * is_adhoc field being set to REMINDER_SET or REMINDER_NOTIFIED.
 * /*  ww w .ja v a 2 s  .c om*/
 * @param context -- the context from which this method is called
 * @param find -- the current Find
 * @param view -- the calling activity's contentView, which is needed
 * to access the UI's sub-views.
 * 
 */
public void listFindCallback(Context context, Find find, View view) {
    Log.i(TAG, "listFindCallback");

    if (find.getIs_adhoc() == REMINDER_SET || find.getIs_adhoc() == this.REMINDER_NOTIFIED) {
        ImageView alarmIcon = new ImageView(context);
        alarmIcon.setImageResource(R.drawable.reminder_alarm);
        RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.list_row_rl);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(25, 25);
        lp.addRule(RelativeLayout.BELOW, R.id.status);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        lp.setMargins(0, 6, 0, 0);
        rl.addView(alarmIcon, lp);
        alarmIcon.setVisibility(ImageView.VISIBLE);
    }
}

From source file:com.sunho.nating.fragments.DetailPlaceFragment.java

private void bindFragment(View parent) {
    Bundle args = getArguments();/*from w ww . j  a  v a  2 s  .co m*/
    if (args == null) {
        return;
    }

    ImageView image = (ImageView) parent.findViewById(R.id.image);
    image.setImageResource(args.getInt(ARG_RESOURCE_ID));
    TextView title = (TextView) parent.findViewById(R.id.title);
    title.setText(args.getString(ARG_TITLE));
    listView = (ListView) parent.findViewById(R.id.list);

    sp1 = (Spinner) parent.findViewById(R.id.spinner1);
    sp2 = (Spinner) parent.findViewById(R.id.spinner2);
    sp3 = (Spinner) parent.findViewById(R.id.spinner3);

    final String[] sp1_data = getResources().getStringArray(R.array.areaCode_1);
    ArrayAdapter<String> sp1_adapter = new ArrayAdapter<String>(context,
            android.R.layout.simple_spinner_dropdown_item, sp1_data);
    sp1.setAdapter(sp1_adapter);

    //First Adapter
    firstPosition = 0;

    final String[] sp2_data = getResources().getStringArray(R.array.sigunguCode_1);
    ArrayAdapter<String> sp2_adapter = new ArrayAdapter<String>(context,
            android.R.layout.simple_spinner_dropdown_item, sp2_data);
    sp2.setAdapter(sp2_adapter);
    sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            secondePosition = position;
            switch (position) {
            case 0:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_1);
                break;
            case 1:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_2);
                break;
            case 2:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_3);
                break;
            case 3:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_4);
                break;
            case 4:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_5);
                break;
            case 5:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_6);
                break;
            case 6:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_7);
                break;
            case 7:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_8);
                break;
            case 8:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_9);
                break;
            case 9:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_10);
                break;
            case 10:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_11);
                break;
            case 11:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_12);
                break;
            case 12:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_13);
                break;
            case 13:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_14);
                break;
            case 14:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_15);
                break;
            case 15:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_16);
                break;
            case 16:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_17);
                break;
            case 17:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_18);
                break;
            case 18:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_19);
                break;
            case 19:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_20);
                break;
            case 20:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_21);
                break;
            case 21:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_22);
                break;
            case 22:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_23);
                break;
            case 23:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_24);
                break;
            case 24:
                sp3_data = getResources().getStringArray(R.array.dongCode_1_25);
                break;

            default:
                break;
            }

            ArrayAdapter<String> sp3_adapter = new ArrayAdapter<String>(context,
                    android.R.layout.simple_spinner_dropdown_item, sp3_data);
            sp3.setAdapter(sp3_adapter);
            sp3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                    result = sp2_data[secondePosition] + " " + sp3_data[position];
                    Log.d("Theme", "Selected Location : " + result);
                    totalURL = makeURL(1, result);
                    placeList.clear();
                    pageCount = 1;
                    adapter.isEndFalse();
                    adapter.resetServerListSize(0);
                    adapter.notifyDataSetChanged();
                    new GetDataTask().execute();
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    listView.setDividerHeight(0);
    adapter = new CustomListAdapter(MainActivity.mActivity, placeList) {
        @Override
        public View getDataRow(int position, View convertView, ViewGroup parent) {
            return convertView;
        }
    };
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.mContext, placeList.get(position).getOldAddress(), 0).show();
            moveFragment(position);
        }
    });
    listView.setOnScrollListener(new EndlessScrollListener() {
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            super.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
            //lastListitem = (totalItemCount - 1 > 0) && (firstVisibleItem + visibleItemCount >= totalItemCount - 1);
        }

        @Override
        public void onLoadMore(int page, int totalItemsCount) {
            // Triggered only when new data needs to be appended to the list
            // Add whatever code is needed to append new items to your AdapterView

            Log.d(TAG, "==ListView Information==");
            Log.d(TAG, "PageNumber : " + page);
            Log.d(TAG, "TotalCount : " + totalItemsCount);
            Log.d(TAG, "adpaterCount : " + adapter.getCount());
            Log.d(TAG, "========================");
            new GetDataTask().execute();

        }
    });
}

From source file:com.mumu.pokemongogo.HeadService.java

private void configSpeed() {
    ImageView iv = mHeadIconList.get(IDX_SPEED_ICON).getImageView();
    if (mWalkSpeed == 1.0) {
        mWalkSpeed = 2.0;/*from   ww  w .j a v a2  s  .c o  m*/
        iv.setImageResource(R.drawable.ic_two);
    } else if (mWalkSpeed == 2.0) {
        mWalkSpeed = 4.0;
        iv.setImageResource(R.drawable.ic_three);
    } else if (mWalkSpeed == 4.0) {
        mWalkSpeed = 8.0;
        iv.setImageResource(R.drawable.ic_four);
    } else if (mWalkSpeed == 8.0) {
        mWalkSpeed = 32.0;
        iv.setImageResource(R.drawable.ic_five);
    } else if (mWalkSpeed == 32.0) {
        mWalkSpeed = 128.0;
        iv.setImageResource(R.drawable.ic_six);
    } else if (mWalkSpeed == 128.0) {
        mWalkSpeed = 0.7;
        iv.setImageResource(R.drawable.ic_slow);
    } else if (mWalkSpeed == 0.7) {
        mWalkSpeed = 1.0;
        iv.setImageResource(R.drawable.ic_one);
    }
    mFakeLocationManager.setSpeed(mWalkSpeed);
}

From source file:com.bigpigs.fragments.SearchFragment.java

private Bitmap getMarkerBitmapFromView(@DrawableRes int resId) {

    View customMarkerView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.custom_marker, null);
    ImageView markerImageView = (ImageView) customMarkerView.findViewById(R.id.marker_icon);
    markerImageView.setImageResource(resId);
    customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
    customMarkerView.buildDrawingCache();
    Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(),
            customMarkerView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
    Drawable drawable = customMarkerView.getBackground();
    if (drawable != null)
        drawable.draw(canvas);/* ww  w . j a  v a2s .  c o m*/
    customMarkerView.draw(canvas);
    return returnedBitmap;
}

From source file:android_network.hetnet.vpn_service.AdapterAccess.java

@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
    // Get values
    final long id = cursor.getLong(colID);
    final int version = cursor.getInt(colVersion);
    final int protocol = cursor.getInt(colProtocol);
    final String daddr = cursor.getString(colDaddr);
    final int dport = cursor.getInt(colDPort);
    long time = cursor.getLong(colTime);
    int allowed = cursor.getInt(colAllowed);
    int block = cursor.getInt(colBlock);
    long sent = cursor.isNull(colSent) ? -1 : cursor.getLong(colSent);
    long received = cursor.isNull(colReceived) ? -1 : cursor.getLong(colReceived);
    int connections = cursor.isNull(colConnections) ? -1 : cursor.getInt(colConnections);

    // Get views/*from  w w w.  j  a  v a2s . c  o m*/
    TextView tvTime = (TextView) view.findViewById(R.id.tvTime);
    ImageView ivBlock = (ImageView) view.findViewById(R.id.ivBlock);
    final TextView tvDest = (TextView) view.findViewById(R.id.tvDest);
    LinearLayout llTraffic = (LinearLayout) view.findViewById(R.id.llTraffic);
    TextView tvConnections = (TextView) view.findViewById(R.id.tvConnections);
    TextView tvTraffic = (TextView) view.findViewById(R.id.tvTraffic);

    // Set values
    tvTime.setText(new SimpleDateFormat("dd HH:mm").format(time));
    if (block < 0)
        ivBlock.setImageDrawable(null);
    else {
        ivBlock.setImageResource(block > 0 ? R.drawable.host_blocked : R.drawable.host_allowed);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Drawable wrap = DrawableCompat.wrap(ivBlock.getDrawable());
            DrawableCompat.setTint(wrap, block > 0 ? colorOff : colorOn);
        }
    }

    tvDest.setText(
            Util.getProtocolName(protocol, version, true) + " " + daddr + (dport > 0 ? "/" + dport : ""));

    if (Util.isNumericAddress(daddr))
        new AsyncTask<String, Object, String>() {
            @Override
            protected void onPreExecute() {
                ViewCompat.setHasTransientState(tvDest, true);
            }

            @Override
            protected String doInBackground(String... args) {
                try {
                    return InetAddress.getByName(args[0]).getHostName();
                } catch (UnknownHostException ignored) {
                    return args[0];
                }
            }

            @Override
            protected void onPostExecute(String addr) {
                tvDest.setText(Util.getProtocolName(protocol, version, true) + " >" + addr
                        + (dport > 0 ? "/" + dport : ""));
                ViewCompat.setHasTransientState(tvDest, false);
            }
        }.execute(daddr);

    if (allowed < 0)
        tvDest.setTextColor(colorText);
    else if (allowed > 0)
        tvDest.setTextColor(colorOn);
    else
        tvDest.setTextColor(colorOff);

    llTraffic.setVisibility(connections > 0 || sent > 0 || received > 0 ? View.VISIBLE : View.GONE);
    if (connections > 0)
        tvConnections.setText(context.getString(R.string.msg_count, connections));

    if (sent > 1024 * 1204 * 1024L || received > 1024 * 1024 * 1024L)
        tvTraffic.setText(context.getString(R.string.msg_gb, (sent > 0 ? sent / (1024 * 1024 * 1024f) : 0),
                (received > 0 ? received / (1024 * 1024 * 1024f) : 0)));
    else if (sent > 1204 * 1024L || received > 1024 * 1024L)
        tvTraffic.setText(context.getString(R.string.msg_mb, (sent > 0 ? sent / (1024 * 1024f) : 0),
                (received > 0 ? received / (1024 * 1024f) : 0)));
    else
        tvTraffic.setText(context.getString(R.string.msg_kb, (sent > 0 ? sent / 1024f : 0),
                (received > 0 ? received / 1024f : 0)));
}

From source file:com.master.metehan.filtereagle.AdapterAccess.java

@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
    // Get values
    final long id = cursor.getLong(colID);
    final int version = cursor.getInt(colVersion);
    final int protocol = cursor.getInt(colProtocol);
    final String daddr = cursor.getString(colDaddr);
    final int dport = cursor.getInt(colDPort);
    long time = cursor.getLong(colTime);
    int allowed = cursor.getInt(colAllowed);
    int block = cursor.getInt(colBlock);
    long sent = cursor.isNull(colSent) ? -1 : cursor.getLong(colSent);
    long received = cursor.isNull(colReceived) ? -1 : cursor.getLong(colReceived);
    int connections = cursor.isNull(colConnections) ? -1 : cursor.getInt(colConnections);

    // Get views/*www .  j a  v  a  2s  . c  o  m*/
    TextView tvTime = (TextView) view.findViewById(R.id.tvTime);
    ImageView ivBlock = (ImageView) view.findViewById(R.id.ivBlock);
    final TextView tvDest = (TextView) view.findViewById(R.id.tvDest);
    LinearLayout llTraffic = (LinearLayout) view.findViewById(R.id.llTraffic);
    TextView tvConnections = (TextView) view.findViewById(R.id.tvConnections);
    TextView tvTraffic = (TextView) view.findViewById(R.id.tvTraffic);

    // Set values
    tvTime.setText(new SimpleDateFormat("dd HH:mm").format(time));
    if (block < 0)
        ivBlock.setImageDrawable(null);
    else {
        ivBlock.setImageResource(block > 0 ? R.drawable.host_blocked : R.drawable.host_allowed);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Drawable wrap = DrawableCompat.wrap(ivBlock.getDrawable());
            DrawableCompat.setTint(wrap, block > 0 ? colorOff : colorOn);
        }
    }

    tvDest.setText(
            Util.getProtocolName(protocol, version, true) + " " + daddr + (dport > 0 ? "/" + dport : ""));

    if (Util.isNumericAddress(daddr) && tvDest.getTag() == null)
        new AsyncTask<String, Object, String>() {
            @Override
            protected void onPreExecute() {
                tvDest.setTag(id);
            }

            @Override
            protected String doInBackground(String... args) {
                try {
                    return InetAddress.getByName(args[0]).getHostName();
                } catch (UnknownHostException ignored) {
                    return args[0];
                }
            }

            @Override
            protected void onPostExecute(String addr) {
                Object tag = tvDest.getTag();
                if (tag != null && (Long) tag == id)
                    tvDest.setText(Util.getProtocolName(protocol, version, true) + " >" + addr
                            + (dport > 0 ? "/" + dport : ""));
                tvDest.setTag(null);
            }
        }.execute(daddr);

    if (allowed < 0)
        tvDest.setTextColor(colorText);
    else if (allowed > 0)
        tvDest.setTextColor(colorOn);
    else
        tvDest.setTextColor(colorOff);

    llTraffic.setVisibility(connections > 0 || sent > 0 || received > 0 ? View.VISIBLE : View.GONE);
    if (connections > 0)
        tvConnections.setText(context.getString(R.string.msg_count, connections));

    if (sent > 1024 * 1204 * 1024L || received > 1024 * 1024 * 1024L)
        tvTraffic.setText(context.getString(R.string.msg_gb, (sent > 0 ? sent / (1024 * 1024 * 1024f) : 0),
                (received > 0 ? received / (1024 * 1024 * 1024f) : 0)));
    else if (sent > 1204 * 1024L || received > 1024 * 1024L)
        tvTraffic.setText(context.getString(R.string.msg_mb, (sent > 0 ? sent / (1024 * 1024f) : 0),
                (received > 0 ? received / (1024 * 1024f) : 0)));
    else
        tvTraffic.setText(context.getString(R.string.msg_kb, (sent > 0 ? sent / 1024f : 0),
                (received > 0 ? received / 1024f : 0)));
}

From source file:com.example.helloworld.ouhkfyp.tab.SlidingTabLayout.java

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

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

        if (tabIcon != null) {
            tabIcon.setImageResource(adapter.getIconResId(i));
        }

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