I have a screen in my app that loads an image behind a transparent status bar. Sometimes these images are light enough for the status bar icons and text to get lost in the image.
How do you set the status bar to light/dark based on the colors of the image underneath it? I'm trying to keep the status bar completely transparent and not add a background.
You can use this solution to change the statusbar color based on your background :
// Set the background and text colors of a toolbar given a
// bitmap image to match
public void setToolbarColor(Bitmap bitmap) {
// Generate the palette and get the vibrant swatch
// See the createPaletteSync() method
// from the code snippet above
Palette p = createPaletteSync(bitmap);
Palette.Swatch vibrantSwatch = p.getVibrantSwatch();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Load default colors
int backgroundColor = ContextCompat.getColor(getContext(),
R.color.default_title_background);
int textColor = ContextCompat.getColor(getContext(),
R.color.default_title_color);
// Check that the Vibrant swatch is available
if(vibrantSwatch != null){
backgroundColor = vibrantSwatch.getRgb();
textColor = vibrantSwatch.getTitleTextColor();
}
// Set the toolbar background and text colors
toolbar.setBackgroundColor(backgroundColor);
toolbar.setTitleTextColor(textColor);
}
Now, simply generate bitmap from your background and then use it with this method.
For more info, visit the official developers site - Color palette API
Related
i making a gallery aplication in android in which the image is in full screen.
So it used below line
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
Now this works fine , i mean the image is now full screen but problem arises when the image background is white as status bar icon color is white so its looks like there is no status bar .
To solve this i tries setting status bar color like this.
window.setStatusBarColor(Color.TRANSPARENT)
But this also not works .
I have tried setting other values like.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
But this also not works ! Can you please help me how can i make status bar color transparent while setting the
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
Thanks in advance
setting the flag to FLAG_LAYOUT_NO_LIMITS makes the status bar completely transparent and unable to have any color, remove those flags and then change the color.
EDIT: how I achieved what you want(that is the status bar to be inside the app and have a layer of some color and certain transparency):
private fun changeStatusBarToOpaque() {
with(window) {
clearFlags(FLAG_LAYOUT_NO_LIMITS)
clearFlags(TRANSLUCENT_STATUS)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
statusBarColor = getCompatColor(R.color.opacityWhite)
addFlags(FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
}
}
}
and your theme should have these two tags
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
Try this in Activity before set content view
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
getWindow().setStatusBarColor(getFactorColor(getResources().getColor(R.color.action_bar_color), 0.4f));
}
where getFactorColor method is
public static int getFactorColor(int color, float factor) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= factor;
color = Color.HSVToColor(hsv);
return color;
}
Add this method
public static void setStatusBarColor(int color, Activity context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = context.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(context.getResources().getColor(color));
}
}
and call it like Utilities.setStatusBarColor(R.color.someColor, context);
android have Transparent color Color.TRANSPARENT
or add this in your colors.xml
<color name="trans">#00FFFFFF</color>
hope this will help!
In most graphic programs like Photoshop and Gimp there is a colorize function so you can easily color a gray scale image with a color of your choice. I want to do the same thing with an image in my Android application. I have been looking into the setColorFilter function. This is what I tried.
ImageView border = (ImageView) findViewById(R.id.imageView);
int color = Color.parseColor("#F57F17"); // the color to use
border.setColorFilter(color, PorterDuff.Mode.OVERLAY);
This does exactly what I need. The only problem is that it also colors the transparent areas in the image. I want the transparent areas to stay transparent.
Is there any way to achieve this?
You are looking for SRC_ATOP:
ImageView border = (ImageView) findViewById(R.id.imageView);
int color = Color.parseColor("#F57F17"); // the color to use
border.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
Colorize the drawable then set it back.
I do this with binding and an adapter to override image colors of drawables.
Example, I change the drawable color based on selected status below:
#JvmStatic
#BindingAdapter("backgroundDrawableChange")
fun backgroundDrawableChange(view: RelativeLayout, isSelected: Boolean){
var bgDrawable: LayerDrawable = view.background as LayerDrawable
var shape: GradientDrawable = bgDrawable.findDrawableByLayerId(R.id.shapeId) as GradientDrawable
shape.setColor(Color.parseColor((if (isSelected) YACustomPreference.getInstance(view.context).getPrimaryColorHex() else YACustomPreference.getInstance(view.context).getWhite())))
}
Here is another example of overriding menu item colors when you need more dynamic control.
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_info_fragment, menu)
try{
//get drawable filter, change color, and reassign
var filterDrawable = menu.findItem(R.id.action_filter).icon
filterDrawable = DrawableCompat.wrap(filterDrawable)
DrawableCompat.setTint(filterDrawable, Color.parseColor(YACustomPreference.getInstance(activity!!).getPrimaryTextColorHex()))
menu.findItem(R.id.action_filter).icon = filterDrawable
//get searchview drawable change color and setup listener
mSearchView = menu.findItem(R.id.action_search)?.actionView as SearchView
mSearchView?.setOnQueryTextListener(this)
val searchDrawableImageView = mSearchView?.findViewById<View>(androidx.appcompat.R.id.search_button) as ImageView
val searchDrawable = ResourcesCompat.getDrawable(resources, R.drawable.ic_search_action, null)
DrawableCompat.setTint(searchDrawable!!, Color.parseColor(YACustomPreference.getInstance(activity!!).getPrimaryTextColorHex()))
searchDrawableImageView.setImageDrawable(searchDrawable)
}catch (ex: Exception){
A35Log.e(TAG, "Error updating SearchView drawable icon")
}
super.onCreateOptionsMenu(menu, inflater)
}
That should hopefully help you get what you are doing across the finish line.
Happy Coding.
Having a round button in xml, I want to change its color when clicking on it. I do it with this code, but it changes to square shape again, not to the original round shape:
button1.setBackgroundColor(Color.BLUE);
Does anyone know how I can change the button's shape as well?
GradientDrawable shape = new GradientDrawable();
shape.setCornerRadius( 8 );
// add some color
// You can add your random color generator here
// and set color
if (i % 2 == 0) {
shape.setColor(Color.RED);
} else {
shape.setColor(Color.BLUE);
}
// now find your view and add background to it
findViewById( R.id.my_view ).setBackground(shape);
I use pallete class to set random background color to textview, and some images cannot generate color and return color like gray. I find that some image file work with getMutedColor and some other work with getVibrantColor and others.
Here is my layout. I use ImageView to show bitmap and a TextView below it to show name with random background.
How can we detect whether image file work with `getMutedColor` or `getVibrantColor` or others?
Here is my code I try so far:
public void generateColor(Bitmap mypic){
mImageView.setImageBitmap(result);
Palette p = Palette.from(result).generate();
mTextView.setBackgroundColor(p.getVibrantColor(default_color));
}
I am appreciate for any help.
I'd simply check for null.
public int getSwatch(Bitmap b){
Palette p = Palette.from(b).generate();
Palette.Swatch swatch;
if((swatch = p.getVibrantSwatch()) != null){
return swatch.getRgb();
}
if((swatch = p.getLightMutedSwatch()) != null){
return swatch.getRgb();
}
.
.
.
return Color.WHITE;
}
Color.WHITE at the end is the fallback color. Should Palette be unable to find a color, you will still get WHITE as a result. You can swap it with any color you wish.
I am implementing Toolbar along and Navigation drawer.I am customized my tool bar with my own views.I have my own menu(Hamburger) icon in my toolbar to open navigation drawer and I am showing badge count on my menu icon(Hamburger).So i would like to hide default Hamburger icon.
I have tried like this:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
Please help me.Thanks in advance.
call .setDrawerIndicatorEnabled(false); on your ActionBarDrawerToggle
This works for me:
public void setLogo(String imageUrl) {
ViewGroup toolbarView = (ViewGroup) toolbar.findViewById(R.id.toolbar);
// set an arbitrary logo so the support library adds an ImageView to the toolbar.
// without this, there would be no ImageView elements in the toolbar
// and the following loop would not work
// `empty_drawable` is a simple rectangle of size 100x100 with a bg color
// matching that of the toolbar
// the size really matters because the smaller you set this image, the next logo
// you set will be of that size. If your arbitrary drawable is of size 1x1, then
// the next logo you set will be of the same size. At least that is what happens
// for me when I load an image with Glide.
// Your drawable should have a background color too. I chose the toolbar's
// background color. If you don't set a background color, your app would crash on Kitkat.
toolbar.setLogo(R.drawable.empty_drawable);
for (int i = 0; i < toolbarView.getChildCount(); i++) {
if ((toolbarView.getChildAt(i) instanceof ImageView)) {
ImageView logoView = (ImageView) toolbarView.getChildAt(i);
if (!TextUtils.isEmpty(imageUrl)) {
// this way I can download an image and set the logo that way
Glide.with(MainActivity.this).load(imageUrl).into(logoView);
// I do this because there is no space between logo and the toolbar title
logoView.setPadding(0, 0, 30, 0);
// and this is because of the `else` block
logoView.setVisibility(View.VISIBLE);
} else {
// pass null to `setLogo()` to hide the logo
logoView.setVisibility(View.GONE);
logoView.setPadding(0, 0, 0, 0);
}
break;
}
}
}
And this is my toolbar:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:animateLayoutChanges="true"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="#string/app_name"
tools:ignore="UnusedAttribute" />
The android:animateLayoutChanges="true" part makes all this really nice.
And tools:ignore="UnusedAttribute" is because my app's minimum SDK is set to 9.
Try this in your activity's onCreate:
getActionBar().setIcon(
new ColorDrawable(getResources().getColor(android.R.color.transparent)));
If you're using the AppCompat, replace getActionBar with getSupportActionBar.