Java Code Examples for androidx.appcompat.widget.AppCompatImageView#setImageResource()

The following examples show how to use androidx.appcompat.widget.AppCompatImageView#setImageResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GuidePagerAdapter.java    From AndroidProject with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    AppCompatImageView imageView = new AppCompatImageView(container.getContext());
    imageView.setPaddingRelative(0, 0, 0,
            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, container.getContext().getResources().getDisplayMetrics()));
    imageView.setImageResource(DRAWABLES[position]);
    container.addView(imageView);
    return imageView;
}
 
Example 2
Source File: RatingBar.java    From Kore with Apache License 2.0 5 votes vote down vote up
private FrameLayout createStar(Context context, AttributeSet attrs, int defStyle) {
    FrameLayout frameLayout = new FrameLayout(getContext());
    frameLayout.setLayoutParams(new LayoutParams(WRAP_CONTENT, MATCH_PARENT));

    AppCompatImageView ivStarBackground = new AppCompatImageView(context, attrs, defStyle);
    ivStarBackground.setLayoutParams(new LayoutParams(WRAP_CONTENT, MATCH_PARENT));
    ivStarBackground.setImageResource(iconResourceId);
    ivStarBackground.setAdjustViewBounds(true);
    ImageViewCompat.setImageTintList(ivStarBackground, ColorStateList.valueOf(backgroundColor));
    frameLayout.addView(ivStarBackground);

    ClipDrawable clipDrawable = new ClipDrawable(
            ContextCompat.getDrawable(context, iconResourceId),
            Gravity.START,
            ClipDrawable.HORIZONTAL);

    AppCompatImageView ivStarForeground = new AppCompatImageView(context, attrs, defStyle);
    ivStarForeground.setLayoutParams(new LayoutParams(WRAP_CONTENT, MATCH_PARENT));
    ivStarForeground.setImageDrawable(clipDrawable);
    ivStarForeground.setAdjustViewBounds(true);
    ImageViewCompat.setImageTintList(ivStarForeground, ColorStateList.valueOf(foregroundColor));
    frameLayout.addView(ivStarForeground);

    clipDrawables.add((ClipDrawable) ivStarForeground.getDrawable());

    return frameLayout;
}
 
Example 3
Source File: FloatbarService.java    From CircleFloatBar with Apache License 2.0 4 votes vote down vote up
private void initView() {

        hideMenuView = LayoutInflater.from(this).inflate(R.layout.dismiss_menu, null);
        imHidingMenu = hideMenuView.findViewById(R.id.btn_menu);


        showMenuView = LayoutInflater.from(this).inflate(R.layout.show_menu, null);
        circleLayout = showMenuView.findViewById(R.id.circle_layout);

        imHome = showMenuView.findViewById(R.id.im_home);
        imTask = showMenuView.findViewById(R.id.im_task);
        imWindows = showMenuView.findViewById(R.id.im_windows);
        imReturn = showMenuView.findViewById(R.id.im_return);
        imWhiteBoard = showMenuView.findViewById(R.id.im_whiteboard);
        imAnnotation = showMenuView.findViewById(R.id.im_annotation);
        //设置CirCleLayout 中心View
        imShowingMenu = new AppCompatImageView(this);
        imShowingMenu.setId(R.id.circle_centerview);
        imShowingMenu.setImageResource(R.drawable.center_20);
        circleLayout.setCenterView(imShowingMenu);

        views = new ArrayList<>();
        views.add(imTask);
        views.add(imReturn);
        views.add(imWindows);
        views.add(imHome);
        views.add(imAnnotation);
        views.add(imWhiteBoard);

        hideMenuView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //init rect touch
                hideMenuHalfSize = hideMenuView.getHeight() / 2;

                hideMenuView.getViewTreeObserver()
                        .removeOnGlobalLayoutListener(this);
            }
        });

        showMenuView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                showMenuHalfSize = showMenuView.getHeight() / 2;

                showMenuView.getViewTreeObserver()
                        .removeOnGlobalLayoutListener(this);
            }
        });
    }
 
Example 4
Source File: AppCompat.java    From HaoReader with GNU General Public License v3.0 4 votes vote down vote up
public static void useCustomIconForSearchView(SearchView searchView, String hint, boolean showSearchHintIcon, boolean showBg) {
    final int normalColor = searchView.getResources().getColor(R.color.colorBarText);
    AppCompatImageView search = searchView.findViewById(androidx.appcompat.R.id.search_button);
    search.setImageResource(R.drawable.ic_search_large_black_24dp);
    setTint(search, normalColor);

    SearchView.SearchAutoComplete searchText = searchView.findViewById(R.id.search_src_text);
    searchText.setTextSize(14f);
    searchText.setPaddingRelative(searchText.getPaddingLeft(), 0, 0, 0);

    final int textSize = Math.round(searchText.getTextSize() * DRAWABLE_SCALE);
    Drawable searchIcon = searchText.getResources().getDrawable(R.drawable.ic_search_black_24dp);
    searchIcon.setBounds(0, 0, textSize, textSize);
    setTint(searchIcon, normalColor);
    searchText.setCompoundDrawablesRelative(searchIcon, null, null, null);
    searchText.setCompoundDrawablePadding(DensityUtil.dp2px(searchText.getContext(), 5));
    searchText.setIncludeFontPadding(false);

    AppCompatImageView close = searchView.findViewById(R.id.search_close_btn);
    close.setImageResource(R.drawable.ic_close_black_24dp);
    setTint(close, normalColor);

    LinearLayout plate = searchView.findViewById(R.id.search_plate);
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) plate.getLayoutParams();
    params.topMargin = DensityUtil.dp2px(plate.getContext(), 6);
    params.bottomMargin = params.topMargin;
    plate.setLayoutParams(params);

    View editFrame = searchView.findViewById(R.id.search_edit_frame);
    params = (LinearLayout.LayoutParams) editFrame.getLayoutParams();
    params.leftMargin = DensityUtil.dp2px(editFrame.getContext(), 4);
    editFrame.setLayoutParams(params);

    int padding = DensityUtil.dp2px(plate.getContext(), 6);
    plate.setPaddingRelative(padding, 0, padding, 0);

    if (showBg) {
        Drawable bag = searchView.getResources().getDrawable(R.drawable.bg_search_field);
        androidx.core.view.ViewCompat.setBackground(plate, bag);
    } else {
        androidx.core.view.ViewCompat.setBackground(plate, null);
    }

    setQueryHintForSearchText(searchText, hint, showSearchHintIcon);
}
 
Example 5
Source File: FirstCardHeaderController.java    From GeometricWeather with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressLint({"SetTextI18n", "InflateParams"})
public FirstCardHeaderController(@NonNull GeoActivity activity, @NonNull Location location) {
    this.activity = activity;
    this.view = LayoutInflater.from(activity).inflate(R.layout.container_main_first_card_header, null);

    AppCompatImageView timeIcon = view.findViewById(R.id.container_main_first_card_header_timeIcon);
    TextView refreshTime = view.findViewById(R.id.container_main_first_card_header_timeText);
    TextClock localTime = view.findViewById(R.id.container_main_first_card_header_localTimeText);
    TextView alert = view.findViewById(R.id.container_main_first_card_header_alert);
    View line = view.findViewById(R.id.container_main_first_card_header_line);

    ThemeManager themeManager = ThemeManager.getInstance(activity);

    if (location.getWeather() != null) {
        this.weather = location.getWeather();

        view.setOnClickListener(v ->
                IntentHelper.startManageActivityForResult(activity, MainActivity.MANAGE_ACTIVITY));
        view.setEnabled(!MainDisplayUtils.isMultiFragmentEnabled(activity));

        if (weather.getAlertList().size() == 0) {
            timeIcon.setEnabled(false);
            timeIcon.setImageResource(R.drawable.ic_time);
        } else {
            timeIcon.setEnabled(true);
            timeIcon.setImageResource(R.drawable.ic_alert);
        }
        ImageViewCompat.setImageTintList(
                timeIcon,
                ColorStateList.valueOf(themeManager.getTextContentColor(activity))
        );
        timeIcon.setOnClickListener(this);

        refreshTime.setText(
                activity.getString(R.string.refresh_at)
                        + " "
                        + Base.getTime(activity, weather.getBase().getUpdateDate())
        );
        refreshTime.setTextColor(themeManager.getTextContentColor(activity));

        long time = System.currentTimeMillis();
        if (TimeZone.getDefault().getOffset(time) == location.getTimeZone().getOffset(time)) {
            // same time zone.
            localTime.setVisibility(View.GONE);
        } else {
            localTime.setVisibility(View.VISIBLE);
            localTime.setTimeZone(location.getTimeZone().getID());
            localTime.setTextColor(themeManager.getTextSubtitleColor(activity));
            localTime.setFormat12Hour(
                    activity.getString(R.string.date_format_widget_long) + ", h:mm aa"
            );
            localTime.setFormat24Hour(
                    activity.getString(R.string.date_format_widget_long) + ", HH:mm"
            );
        }

        if (weather.getAlertList().size() == 0) {
            alert.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
        } else {
            alert.setVisibility(View.VISIBLE);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < weather.getAlertList().size(); i ++) {
                builder.append(weather.getAlertList().get(i).getDescription())
                        .append(", ")
                        .append(
                                DateFormat.getDateTimeInstance(
                                        DateFormat.LONG,
                                        DateFormat.DEFAULT
                                ).format(weather.getAlertList().get(i).getDate())
                        );
                if (i != weather.getAlertList().size() - 1) {
                    builder.append("\n");
                }
            }
            alert.setText(builder.toString());
            alert.setTextColor(themeManager.getTextSubtitleColor(activity));

            line.setVisibility(View.VISIBLE);
            line.setBackgroundColor(themeManager.getRootColor(activity));
        }
        alert.setOnClickListener(this);
    }
}