Java Code Examples for android.view.View#setBackground()

The following examples show how to use android.view.View#setBackground() . 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: BlurHelper.java    From base-module with Apache License 2.0 6 votes vote down vote up
private static void autoHandleSuccess(final Bitmap bitmap, final BlurConfig blurConfig) {
    if (blurConfig.cache && !TextUtils.isEmpty(blurConfig.cacheKey)) {
        addCacheBitmap(blurConfig.cacheKey, bitmap);
    }
    if (blurConfig.targetView != null && blurConfig.targetView.get() != null) {
        View target = blurConfig.targetView.get();
        if (target instanceof ImageView) {
            ((ImageView)target).setImageBitmap(bitmap);
        } else {
            Drawable drawable = new BitmapDrawable(target.getContext().getResources(), bitmap);
            if (Build.VERSION.SDK_INT <= 16) {
                target.setBackgroundDrawable(drawable);
            } else {
                target.setBackground(drawable);
            }
        }
        if (blurConfig.animAlpha) {
            animate(target, blurConfig.animDuration);
        }
    }
}
 
Example 2
Source File: TagFlowLayout.java    From support with Apache License 2.0 6 votes vote down vote up
private void updateCheckStatus(View view, boolean checked) {
    if (view == null) {
        return;
    }
    // don't reuse drawable for different tag label
    Drawable[] drawables = mDecorator.getBackgroundDrawable();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        view.setBackgroundDrawable(drawables[drawables.length > 1 ? (checked ? 1 : 0) : 0]);
    } else {
        view.setBackground(drawables[drawables.length > 1 ? (checked ? 1 : 0) : 0]);
    }
    final int[] color = mDecorator.getTextColor();
    if (color != null && color.length > 0) {
        ((TextView) view).setTextColor((color[color.length > 1 ? (checked ? 1 : 0) : 0]));
    }
}
 
Example 3
Source File: PlayTransition.java    From Muzesto with GNU General Public License v3.0 5 votes vote down vote up
private View addViewToOverlay(ViewGroup sceneRoot, int width, int height, Drawable background) {
    View view = new NoOverlapView(sceneRoot.getContext());
    view.setBackground(background);
    int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
    int heightSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
    view.measure(widthSpec, heightSpec);
    view.layout(0, 0, width, height);
    sceneRoot.getOverlay().add(view);
    return view;
}
 
Example 4
Source File: DialogUtils.java    From talk-android with MIT License 5 votes vote down vote up
@SuppressLint("NewApi")
public static void setBackgroundCompat(View view, Drawable d) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        //noinspection deprecation
        view.setBackgroundDrawable(d);
    } else {
        view.setBackground(d);
    }
}
 
Example 5
Source File: ChromeSwitchPreference.java    From delion with Apache License 2.0 5 votes vote down vote up
@Override
protected void onBindView(View view) {
    super.onBindView(view);

    if (mDrawDivider) {
        int left = view.getPaddingLeft();
        int right = view.getPaddingRight();
        int top = view.getPaddingTop();
        int bottom = view.getPaddingBottom();
        view.setBackground(DividerDrawable.create(getContext()));
        view.setPadding(left, top, right, bottom);
    }

    SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.switch_widget);
    // On BLU Life Play devices SwitchPreference.setWidgetLayoutResource() does nothing. As a
    // result, the user will see a non-material Switch and switchView will be null, hence the
    // null check below. http://crbug.com/451447
    if (switchView != null) {
        switchView.setChecked(isChecked());
    }

    TextView title = (TextView) view.findViewById(android.R.id.title);
    title.setSingleLine(false);
    if (!mDontUseSummaryAsTitle && TextUtils.isEmpty(getTitle())) {
        TextView summary = (TextView) view.findViewById(android.R.id.summary);
        title.setText(summary.getText());
        title.setVisibility(View.VISIBLE);
        summary.setVisibility(View.GONE);
    }

    if (mManagedPrefDelegate != null) mManagedPrefDelegate.onBindViewToPreference(this, view);
}
 
Example 6
Source File: RippleDrawable.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private static void setBackground(View target, Drawable drawable){
    if(Build.VERSION.SDK_INT > 16){
        target.setBackground(drawable);
    }else{
        target.setBackgroundDrawable(drawable);
    }
}
 
Example 7
Source File: Utils.java    From google-io-2014-compat with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public static void setBackgroundCompat(View v, Drawable drawable) {
    if (hasJellyBean()) {
        v.setBackground(drawable);
    } else {
        v.setBackgroundDrawable(drawable);
    }
}
 
Example 8
Source File: ThemeClass.java    From heads-up with GNU General Public License v3.0 5 votes vote down vote up
protected static void setColor(View view, int color) {
    if (color == 0) return;
    Drawable drawable = view.getBackground();
    if (drawable != null) {
        drawable = drawable.mutate();
        //drawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
        drawable.setColorFilter(getColorFilter(color));
        if (Build.VERSION.SDK_INT >= 16) view.setBackground(drawable);
        else                             view.setBackgroundDrawable(drawable);
    } else {
        view.setBackgroundColor(color);
    }
}
 
Example 9
Source File: HorzItemList.java    From Primary with GNU General Public License v3.0 5 votes vote down vote up
private void setBackground(View item, int drawableId) {
    if (Build.VERSION.SDK_INT >= 21) {
        item.setBackground(mParent.getResources().getDrawable(android.R.drawable.btn_default, mParent.getTheme()));
    } else {
        item.setBackground(mParent.getResources().getDrawable(android.R.drawable.btn_default));
    }

}
 
Example 10
Source File: RippleDrawable.java    From AcDisplay with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private static void setBackground(View target, Drawable drawable) {
    if (Build.VERSION.SDK_INT > 16) {
        target.setBackground(drawable);
    } else {
        target.setBackgroundDrawable(drawable);
    }
}
 
Example 11
Source File: BackgroundHelper.java    From RadioRealButton with Apache License 2.0 5 votes vote down vote up
static void setBackground(View view, Drawable drawable) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        view.setBackground(drawable);
    } else {
        view.setBackgroundDrawable(drawable);
    }
}
 
Example 12
Source File: PreviewMorphAnimator.java    From PreviewSeekBar with Apache License 2.0 5 votes vote down vote up
private void tintViews(PreviewBar previewBar,
                       View morphView,
                       View frameView) {
    int color = previewBar.getScrubberColor();
    if (morphView.getBackgroundTintList() == null
            || morphView.getBackgroundTintList().getDefaultColor() != color) {
        Drawable drawable = DrawableCompat.wrap(morphView.getBackground());
        DrawableCompat.setTint(drawable, color);
        morphView.setBackground(drawable);
        frameView.setBackgroundColor(color);
    }
}
 
Example 13
Source File: RippleHelper.java    From SegmentedButton with Apache License 2.0 5 votes vote down vote up
static void setRipple(View view, int pressedColor, Integer normalColor, int radius) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        view.setBackground(getRippleDrawable(pressedColor, normalColor, radius));
    } else {
        view.setBackgroundDrawable(getStateListDrawable(pressedColor, normalColor, radius));
    }
}
 
Example 14
Source File: ViewUtil.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the background on a view to the provided drawable while retaining the padding
 * of the original view (regardless of whether the provided drawable has its own padding)
 *
 * @param v          The view whose background will be updated
 * @param background A background drawable (can be null to clear the background)
 */
public static void setBackgroundRetainPadding(View v, Drawable background) {
    //Need to transplant the padding due to background affecting it
    int[] padding = {v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(), v.getPaddingBottom()};

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
        v.setBackground(background);
    } else {
        v.setBackgroundDrawable(background);
    }
    v.setPadding(padding[0], padding[1], padding[2], padding[3]);
}
 
Example 15
Source File: SimpleEnvelopesAdapter.java    From budget-envelopes with GNU General Public License v3.0 5 votes vote down vote up
private void changeColor(int pos, View change) {
    Cursor csr = getCursor();
    csr.moveToPosition(pos);
    int color = csr.getInt(csr.getColumnIndexOrThrow("color"));
    if(sdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        change.setBackgroundDrawable(EnvelopesAdapter.getColorStateDrawable(color));
    } else {
        change.setBackground(EnvelopesAdapter.getColorStateDrawable(color));
    }
}
 
Example 16
Source File: WXViewUtils.java    From weex-uikit with MIT License 5 votes vote down vote up
@SuppressWarnings("deprecation")
public static void setBackGround(View view, Drawable drawable){
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN){
    view.setBackgroundDrawable(drawable);
  }
  else{
    view.setBackground(drawable);
  }
}
 
Example 17
Source File: ModSDK21.java    From SwipeBack with GNU General Public License v3.0 4 votes vote down vote up
@TargetApi(21)
public static void afterOnPostCreateSDK21(XC_MethodHook.MethodHookParam mhparams) throws Throwable {
	Class<?> internalStyleable = findClass("com.android.internal.R.styleable", null);
	int[] internalTheme = $(getStaticObjectField(internalStyleable, "Theme"));
	int internalColorPrimary = getStaticIntField(internalStyleable, "Theme_colorPrimaryDark");
	
	SwipeBackActivityHelper helper = $(getAdditionalInstanceField(mhparams.thisObject, "helper"));
	if (helper != null) {
		final Activity activity = $(mhparams.thisObject);

		String packageName = activity.getApplicationInfo().packageName;
		String className = activity.getClass().getName();
		
		mSettings.reload();
		if (!mSettings.getBoolean(packageName, className, Settings.LOLLIPOP_HACK, false))
			return;
		
		ViewGroup root = $(helper.getSwipeBackLayout().getChildAt(0));
		View content = root.getChildAt(0);
		final WindowInsetsColorDrawable bkg = new WindowInsetsColorDrawable(content.getBackground());
		content.setBackground(bkg);
		
		TypedArray a = activity.getTheme().obtainStyledAttributes(internalTheme);
		int primary = a.getColor(internalColorPrimary, 0);
		a.recycle();

		if (primary != 0) {
			bkg.setTopDrawable(new ColorDrawable(primary));
		} else {
			content.setSystemUiVisibility(content.getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
			content.setFitsSystemWindows(true);
		}

		root.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
			@Override
			public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
				bkg.setTopInset(insets.getSystemWindowInsetTop());
				activity.getWindow().setStatusBarColor(0);
				return insets;
			}
		});
	}
}
 
Example 18
Source File: MainActivity.java    From music_player with Open Software License 3.0 4 votes vote down vote up
public void animation_change_color(final int Int) {
    ImageView play_now_back_color = (ImageView) findViewById(R.id.play_now_back_color);
    final RelativeLayout activity_now_play = (RelativeLayout) findViewById(R.id.activity_now_play);
    if (cx == 0) {
        FloatingActionButton play_or_pause = (FloatingActionButton) findViewById(R.id.play_or_pause);
        RelativeLayout seekbar_layout = (RelativeLayout) findViewById(R.id.seekbar_layout);
        RelativeLayout control_layout = (RelativeLayout) findViewById(R.id.control_layout);
        cx = play_or_pause.getLeft() + control_layout.getLeft() + play_or_pause.getWidth() / 2;
        cy = control_layout.getTop() - seekbar_layout.getTop() + play_or_pause.getTop() + play_or_pause.getHeight() / 2;
        finalRadius = Math.max(play_now_back_color.getWidth(), play_now_back_color.getHeight());
    }
    if (cx != 0) {
        Animator anim = ViewAnimationUtils.createCircularReveal(play_now_back_color, cx, cy, 0, finalRadius);
        play_now_back_color.setBackgroundColor(Int);
        anim.setDuration(500);
        anim.start();
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                activity_now_play.setBackgroundColor(Int);
            }
        });
    } else {
        activity_now_play.setBackgroundColor(Int);
    }
    TextView now_on_play_text = (TextView) findViewById(R.id.now_on_play_text);
    now_on_play_text.setTextColor(Int);
    //lrcview字体颜色
    if (ColorUtil.isColorLight(Int)) {
        otherLyricView.setNormalColor(Color.parseColor("#60000000"));
        otherLyricView.setTimelineTextColor(Color.parseColor("#000000"));
        otherLyricView.setCurrentColor(Color.parseColor("#000000"));
    } else {
        otherLyricView.setNormalColor(Color.parseColor("#60FFFFFF"));
        otherLyricView.setTimelineTextColor(Color.parseColor("#FFFFFF"));
        otherLyricView.setCurrentColor(Color.parseColor("#FFFFFF"));
    }

    //歌词背景颜色
    if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
        View bottom = findViewById(R.id.gradient_bottom);
        View top = findViewById(R.id.gradient_top);
        View gradient = findViewById(R.id.gradient);
        top.setBackground(
                ScrimUtil.makeCubicGradientScrimDrawable(Int, //颜色
                        8, //渐变层数
                        Gravity.TOP)); //起始方向
        bottom.setBackground(
                ScrimUtil.makeCubicGradientScrimDrawable(Int, //颜色
                        8, //渐变层数
                        Gravity.BOTTOM)); //起始方向
        gradient.setBackground(
                ScrimUtil.makeCubicGradientScrimDrawable(Int, //颜色
                        8, //渐变层数
                        Gravity.BOTTOM)); //起始方向
    }
}
 
Example 19
Source File: RxToast.java    From RxTools-master with Apache License 2.0 4 votes vote down vote up
public static final void setBackground(@NonNull View view, Drawable drawable) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        view.setBackground(drawable);
    else
        view.setBackgroundDrawable(drawable);
}
 
Example 20
Source File: ColorChooserPreferenceX.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
void setBackgroundCompat(View view, Drawable d) {
    view.setBackground(d);
}