Example usage for android.content Context getDrawable

List of usage examples for android.content Context getDrawable

Introduction

In this page you can find the example usage for android.content Context getDrawable.

Prototype

@Nullable
public final Drawable getDrawable(@DrawableRes int id) 

Source Link

Document

Returns a drawable object associated with a particular resource ID and styled for the current theme.

Usage

From source file:com.android.tv.settings.MainFragment.java

private void updateAccounts() {
    if (mAccountsGroup == null) {
        return;//from  w ww. ja v  a2s  . co m
    }

    final Set<String> touchedAccounts = new ArraySet<>(mAccountsGroup.getPreferenceCount());

    final AccountManager am = AccountManager.get(getContext());
    final AuthenticatorDescription[] authTypes = am.getAuthenticatorTypes();
    final ArrayList<String> allowableAccountTypes = new ArrayList<>(authTypes.length);
    final Context themedContext = getPreferenceManager().getContext();

    for (AuthenticatorDescription authDesc : authTypes) {
        final Context targetContext;
        try {
            targetContext = getContext().createPackageContext(authDesc.packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Authenticator description with bad package name", e);
            continue;
        } catch (SecurityException e) {
            Log.e(TAG, "Security exception loading package resources", e);
            continue;
        }

        // Main title text comes from the authenticator description (e.g. "Google").
        String authTitle = null;
        try {
            authTitle = targetContext.getString(authDesc.labelId);
            if (TextUtils.isEmpty(authTitle)) {
                authTitle = null; // Handled later when we add the row.
            }
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Authenticator description with bad label id", e);
        }

        // There exist some authenticators which aren't intended to be user-facing.
        // If the authenticator doesn't have a title or an icon, don't present it to
        // the user as an option.
        if (authTitle != null || authDesc.iconId != 0) {
            allowableAccountTypes.add(authDesc.type);
        }

        Account[] accounts = am.getAccountsByType(authDesc.type);
        if (accounts == null || accounts.length == 0) {
            continue; // No point in continuing; there aren't any accounts to show.
        }

        // Icon URI to be displayed for each account is based on the type of authenticator.
        Drawable authImage = null;
        try {
            authImage = targetContext.getDrawable(authDesc.iconId);
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Authenticator has bad resources", e);
        }

        // Display an entry for each installed account we have.
        for (final Account account : accounts) {
            final String key = "account_pref:" + account.type + ":" + account.name;
            Preference preference = findPreference(key);
            if (preference == null) {
                preference = new Preference(themedContext);
            }
            preference.setTitle(authTitle != null ? authTitle : account.name);
            preference.setIcon(authImage);
            preference.setSummary(authTitle != null ? account.name : null);
            preference.setFragment(AccountSyncFragment.class.getName());
            AccountSyncFragment.prepareArgs(preference.getExtras(), account);

            touchedAccounts.add(key);
            preference.setKey(key);

            mAccountsGroup.addPreference(preference);
        }
    }

    for (int i = 0; i < mAccountsGroup.getPreferenceCount();) {
        final Preference preference = mAccountsGroup.getPreference(i);
        final String key = preference.getKey();
        if (touchedAccounts.contains(key) || TextUtils.equals(KEY_ADD_ACCOUNT, key)) {
            i++;
        } else {
            mAccountsGroup.removePreference(preference);
        }
    }

    // Never allow restricted profile to add accounts.
    final Preference addAccountPref = findPreference(KEY_ADD_ACCOUNT);
    if (addAccountPref != null) {
        addAccountPref.setOrder(Integer.MAX_VALUE);
        if (isRestricted()) {
            addAccountPref.setVisible(false);
        } else {
            Intent i = new Intent().setComponent(new ComponentName("com.android.tv.settings",
                    "com.android.tv.settings.accounts.AddAccountWithTypeActivity"));
            i.putExtra(AddAccountWithTypeActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY,
                    allowableAccountTypes.toArray(new String[allowableAccountTypes.size()]));

            // If there are available account types, show the "add account" button.
            addAccountPref.setVisible(!allowableAccountTypes.isEmpty());
            addAccountPref.setIntent(i);
        }
    }
}

From source file:org.gateshipone.malp.application.listviewitems.FileListItem.java

/**
 * Extracts the information from a MPDTrack.
 * @param track Track to show the view for.
 * @param context Context used for String extraction
 *//*  ww  w.j av a 2 s . c  o  m*/
public void setTrack(MPDTrack track, Context context) {
    if (track != null) {
        String trackNumber;

        if (track.getAlbumDiscCount() > 0) {
            trackNumber = String.valueOf(track.getDiscNumber()) + '-' + String.valueOf(track.getTrackNumber());
        } else {
            trackNumber = String.valueOf(track.getTrackNumber());
        }

        /* Extract the information from the track */
        mNumberView.setText(trackNumber);

        int trackLength = track.getLength();
        if (trackLength > 0) {
            // Get the preformatted duration of the track.
            mDurationView.setText(FormatHelper.formatTracktimeFromS(track.getLength()));
            mDurationView.setVisibility(VISIBLE);
        } else {
            mDurationView.setVisibility(GONE);
        }
        // Get track title
        String trackTitle = track.getTrackTitle();

        // If no trackname is available (e.g. streaming URLs) show path
        if (null == trackTitle || trackTitle.isEmpty()) {
            trackTitle = FormatHelper.getFilenameFromPath(track.getPath());
        }
        mTitleView.setText(trackTitle);

        // additional information (artist + album)
        String trackInformation;

        String trackAlbum = track.getTrackAlbum();

        // Check which information is available and set the separator accordingly.
        if (!track.getTrackArtist().isEmpty() && !track.getTrackAlbum().isEmpty()) {
            trackInformation = track.getTrackArtist()
                    + context.getResources().getString(R.string.track_item_separator) + trackAlbum;
        } else if (track.getTrackArtist().isEmpty() && !track.getTrackAlbum().isEmpty()) {
            trackInformation = trackAlbum;
        } else if (track.getTrackAlbum().isEmpty() && !track.getTrackArtist().isEmpty()) {
            trackInformation = track.getTrackArtist();
        } else {
            trackInformation = track.getPath();
        }

        mAdditionalInfoView.setText(trackInformation);
        mSeparator.setVisibility(VISIBLE);
        mAdditionalInfoView.setVisibility(VISIBLE);
        mNumberView.setVisibility(VISIBLE);
    } else {
        /* Show loading text */
        mSeparator.setVisibility(GONE);
        mTitleView.setText(getResources().getText(R.string.track_item_loading));
        mNumberView.setVisibility(GONE);
        mDurationView.setVisibility(GONE);
        mAdditionalInfoView.setVisibility(GONE);
    }

    if (mShowIcon) {
        Drawable icon = context.getDrawable(R.drawable.ic_file_48dp);

        if (icon != null) {
            // get tint color
            int tintColor = ThemeUtils.getThemeColor(context, android.R.attr.textColor);
            // tint the icon
            DrawableCompat.setTint(icon, tintColor);
        }
        mItemIcon.setImageDrawable(icon);
    }

}

From source file:org.telegram.ui.ChangePhoneHelpActivity.java

@Override
public View createView(Context context) {
    actionBar.setBackButtonImage(R.drawable.ic_ab_back);
    actionBar.setAllowOverlayTitle(true);

    TLRPC.User user = UserConfig.getCurrentUser();
    String value;//from w w w  .  ja v  a 2 s  .c  om
    if (user != null && user.phone != null && user.phone.length() != 0) {
        value = PhoneFormat.getInstance().format("+" + user.phone);
    } else {
        value = LocaleController.getString("NumberUnknown", R.string.NumberUnknown);
    }

    actionBar.setTitle(value);
    actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
        @Override
        public void onItemClick(int id) {
            if (id == -1) {
                finishFragment();
            }
        }
    });

    fragmentView = new RelativeLayout(context);
    fragmentView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });

    RelativeLayout relativeLayout = (RelativeLayout) fragmentView;

    ScrollView scrollView = new ScrollView(context);
    relativeLayout.addView(scrollView);
    RelativeLayout.LayoutParams layoutParams3 = (RelativeLayout.LayoutParams) scrollView.getLayoutParams();
    layoutParams3.width = LayoutHelper.MATCH_PARENT;
    layoutParams3.height = LayoutHelper.WRAP_CONTENT;
    layoutParams3.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
    scrollView.setLayoutParams(layoutParams3);

    LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setPadding(0, AndroidUtilities.dp(20), 0, AndroidUtilities.dp(20));
    scrollView.addView(linearLayout);
    ScrollView.LayoutParams layoutParams = (ScrollView.LayoutParams) linearLayout.getLayoutParams();
    layoutParams.width = ScrollView.LayoutParams.MATCH_PARENT;
    layoutParams.height = ScrollView.LayoutParams.WRAP_CONTENT;
    linearLayout.setLayoutParams(layoutParams);

    ImageView imageView = new ImageView(context);
    imageView.setImageResource(R.drawable.phone_change);
    linearLayout.addView(imageView);
    LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) imageView.getLayoutParams();
    layoutParams2.width = LayoutHelper.WRAP_CONTENT;
    layoutParams2.height = LayoutHelper.WRAP_CONTENT;
    layoutParams2.gravity = Gravity.CENTER_HORIZONTAL;
    imageView.setLayoutParams(layoutParams2);

    TextView textView = new TextView(context);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
    textView.setGravity(Gravity.CENTER_HORIZONTAL);
    //textView.setTextColor(0xff212121);

    try {
        textView.setText(AndroidUtilities
                .replaceTags(LocaleController.getString("PhoneNumberHelp", R.string.PhoneNumberHelp)));
    } catch (Exception e) {
        FileLog.e("tmessages", e);
        textView.setText(LocaleController.getString("PhoneNumberHelp", R.string.PhoneNumberHelp));
    }
    linearLayout.addView(textView);
    layoutParams2 = (LinearLayout.LayoutParams) textView.getLayoutParams();
    layoutParams2.width = LayoutHelper.WRAP_CONTENT;
    layoutParams2.height = LayoutHelper.WRAP_CONTENT;
    layoutParams2.gravity = Gravity.CENTER_HORIZONTAL;
    layoutParams2.leftMargin = AndroidUtilities.dp(20);
    layoutParams2.rightMargin = AndroidUtilities.dp(20);
    layoutParams2.topMargin = AndroidUtilities.dp(56);
    textView.setLayoutParams(layoutParams2);

    textView = new TextView(context);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
    textView.setGravity(Gravity.CENTER_HORIZONTAL);
    textView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent) /*0xff4d83b3*/);
    textView.setText(LocaleController.getString("PhoneNumberChange", R.string.PhoneNumberChange));
    textView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    textView.setPadding(AndroidUtilities.dp(16), AndroidUtilities.dp(12), AndroidUtilities.dp(16),
            AndroidUtilities.dp(12));
    textView.setBackground(context.getDrawable(R.drawable.list_selector));
    linearLayout.addView(textView);
    layoutParams2 = (LinearLayout.LayoutParams) textView.getLayoutParams();
    layoutParams2.width = LayoutHelper.WRAP_CONTENT;
    layoutParams2.height = LayoutHelper.WRAP_CONTENT;
    layoutParams2.gravity = Gravity.CENTER_HORIZONTAL;
    layoutParams2.leftMargin = AndroidUtilities.dp(20);
    layoutParams2.rightMargin = AndroidUtilities.dp(20);
    layoutParams2.topMargin = AndroidUtilities.dp(46);
    textView.setLayoutParams(layoutParams2);

    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (getParentActivity() == null) {
                return;
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
            builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
            builder.setMessage(LocaleController.getString("PhoneNumberAlert", R.string.PhoneNumberAlert));
            builder.setPositiveButton(LocaleController.getString("OK", R.string.OK),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            presentFragment(new ChangePhoneActivity(), true);
                        }
                    });
            builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
            showDialog(builder.create());
        }
    });

    return fragmentView;
}