Android Viewpager EdgeEffect custom Color - android

I am trying to customize the EdgeEffect in the Viewpager of my App. The aim was to replace the blue ics Overscroll EdgeEffect i.e. with a custom red one. So at first i edited overscroll_edge and the corresponding overscroll_glow. Then I put them both into the /res/drawable directory of my app. Additionally i copied the EdgeEffect Source File to the /src/android/widget/ directory of my app. The only change i made in EdgeEffect was to import com.my.application.R instead of com.android.internal.R.
But Android just won't use my custom android.widget.EdgeEffect instead of the one in Android System, so the Viewpager EdgeEffect stays constantly blue. Am I missing something?

You can set the EdgeEffect color of the ViewPager with some reflection:
public static void setEdgeGlowColor(ViewPager viewPager, int color) {
try {
Class<?> clazz = ViewPager.class;
for (String name : new String[] {
"mLeftEdge", "mRightEdge"
}) {
Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
Object edge = field.get(viewPager); // android.support.v4.widget.EdgeEffectCompat
Field fEdgeEffect = edge.getClass().getDeclaredField("mEdgeEffect");
fEdgeEffect.setAccessible(true);
setEdgeEffectColor((EdgeEffect) fEdgeEffect.get(edge), color);
}
} catch (Exception ignored) {
}
}
public static void setEdgeEffectColor(EdgeEffect edgeEffect, int color) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
edgeEffect.setColor(color);
return;
}
Field edgeField = EdgeEffect.class.getDeclaredField("mEdge");
Field glowField = EdgeEffect.class.getDeclaredField("mGlow");
edgeField.setAccessible(true);
glowField.setAccessible(true);
Drawable mEdge = (Drawable) edgeField.get(edgeEffect);
Drawable mGlow = (Drawable) glowField.get(edgeEffect);
mEdge.setColorFilter(color, PorterDuff.Mode.SRC_IN);
mGlow.setColorFilter(color, PorterDuff.Mode.SRC_IN);
mEdge.setCallback(null); // free up any references
mGlow.setCallback(null); // free up any references
} catch (Exception ignored) {
}
}

You have to implement ViewPager, PagerAdapter, FragmentstatePagerAdapter, EdgeEffectCompat, EdgeEffectCompatIcs and EdgeEffect in a package of your own app (for example com.yourapp.viewpager). Only changes made was adjusting the imports and packages names. Copy and edit resources files to res/drawable of your app and et voila, it work's.

I can suggest a very simple way, but hackish:
int glowDrawableId = context.getResources().getIdentifier("overscroll_glow", "drawable", "android");
Drawable androidGlow = context.getResources().getDrawable(glowDrawableId);
androidGlow.setColorFilter(brandColor, PorterDuff.Mode.MULTIPLY);
I took advantage of the fact that the glow effect is actually a shared Drawable and applied a filter on it: http://evendanan.net/android/branding/2013/12/09/branding-edge-effect/

Related

CollapsingToolbarLayout's setCollapsedTitleTextColor and setExpandedTitleColor don't do anything

I'm trying to set the expanded and collapsed CollapsingToolbarLayout's title text color to be different, but no matter what I do, it is always white.
Here is the code in question:
mCollapsingToolbar.setCollapsedTitleTextColor(getResources.getColor(R.color.foo));
mCollapsingToolbar.setExpandedTitleColor(getResources.getColor(R.color.bar));
In my layout XML file, I'm not specifying any color styles on either the AppBarLayout, the CollapsingToolbarLayout, or the Toolbar itself.
Is there some sort of interference with my Activity's theme settings?
Thanks!
I think you need to try below code:
private void changeCollapsedTitleTextColor(CollapsingToolbarLayout collapsingToolbarLayout) {
try {
final Field field = collapsingToolbarLayout.getClass().getDeclaredField("mCollapsingTextHelper");
field.setAccessible(true);
final Object object = field.get(collapsingToolbarLayout);
final Field tpf = object.getClass().getDeclaredField("mTextPaint");
tpf.setAccessible(true);
((TextPaint) tpf.get(object)).setColor(getResources().getColor(R.color.your_color));
} catch (Exception ignored) {
}
}
I found here.
I hope it may help you.

Android AppCompat 23.1.0 Tint Compound Drawable

I was using the method below to properly tint compound drawables with android.support.design 23.0.1 . Now that they released 23.1.0 it doesn't work anymore on api LVL16, all my drawables are black.
Anyone has a suggestion ?
private void setCompoundColor(TextView view) {
Drawable drawable = view.getCompoundDrawables()[0];
Drawable wrap = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(wrap, ContextCompat.getColor(this, R.color.primaryLighter2));
DrawableCompat.setTintMode(wrap, PorterDuff.Mode.SRC_IN);
wrap = wrap.mutate();
view.setCompoundDrawablesRelativeWithIntrinsicBounds(wrap, null, null, null);
}
Thanks.
I faced the same problem last week, and it turns out in the AppCompatTextView v23.1.0, compound drawables are automatically tinted.
Here is the solution I found, with more explications on why I did this below. Its not very clean but at least it enables you to tint your compound drawables !
SOLUTION
Put this code in a helper class or in your custom TextView/Button :
/**
* The app compat text view automatically sets the compound drawable tints for a static array of drawables ids.
* If the drawable id is not in the list, the lib apply a null tint, removing the custom tint set before.
* There is no way to change this (private attributes/classes, only set in the constructor...)
*
* #param object the object on which to disable default tinting.
*/
public static void removeDefaultTinting(Object object) {
try {
// Get the text helper field.
Field mTextHelperField = object.getClass().getSuperclass().getDeclaredField("mTextHelper");
mTextHelperField.setAccessible(true);
// Get the text helper object instance.
final Object mTextHelper = mTextHelperField.get(object);
if (mTextHelper != null) {
// Apply tint to all private attributes. See AppCompat source code for usage of theses attributes.
setObjectFieldToNull(mTextHelper, "mDrawableStartTint");
setObjectFieldToNull(mTextHelper, "mDrawableEndTint");
setObjectFieldToNull(mTextHelper, "mDrawableLeftTint");
setObjectFieldToNull(mTextHelper, "mDrawableTopTint");
setObjectFieldToNull(mTextHelper, "mDrawableRightTint");
setObjectFieldToNull(mTextHelper, "mDrawableBottomTint");
}
} catch (NoSuchFieldException e) {
// If it doesn't work, we can do nothing else. The icons will be white, we will see it.
e.printStackTrace();
} catch (IllegalAccessException e) {
// If it doesn't work, we can do nothing else. The icons will be white, we will see it.
e.printStackTrace();
}
}
/**
* Set the field of an object to null.
*
* #param object the TextHelper object (class is not accessible...).
* #param fieldName the name of the tint field.
*/
private static void setObjectFieldToNull(Object object, String fieldName) {
try {
Field tintField;
// Try to get field from class or super class (depends on the implementation).
try {
tintField = object.getClass().getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
tintField = object.getClass().getSuperclass().getDeclaredField(fieldName);
}
tintField.setAccessible(true);
tintField.set(object, null);
} catch (NoSuchFieldException e) {
// If it doesn't work, we can do nothing else. The icons will be white, we will see it.
e.printStackTrace();
} catch (IllegalAccessException e) {
// If it doesn't work, we can do nothing else. The icons will be white, we will see it.
e.printStackTrace();
}
}
Then you can call removeDefaultTinting(this); on each constructor of your class extending AppCompatTextView or AppCompatButton. For example :
public MyCustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
removeDefaultTinting(this);
}
With this, code working with v23.0.1 should work on v23.1.0.
I am not satisfied by the use of reflection to change attributes in the AppCompat lib, but this is the only way I found to use tinting on compound drawables with v23.1.0. Hopefully someone will find a better solution, or compound drawable tinting will be added to the AppCompat public methods.
UPDATE
I found another simpler solution : this bug occurs only if you set compound drawables using xml. Do not set them in xml, then set them in your code and it will work. The faulty code being in the constructor, setting drawables after it has been called is not affected.
EXPLICATIONS
In AppCompatTextView constructor, a text helper is initialized :
mTextHelper.loadFromAttributes(attrs, defStyleAttr);
mTextHelper.applyCompoundDrawablesTints();
In the TextHelper loadFromAttributes function, a tint list is created for each compound drawable. As you can see, mDrawableXXXTint.mHasTintList is always set to true. mDrawableXXXTint.mTintList is the tint color that will be applied, and is only get from hardcoded values of AppCompat. For your custom drawables, it will always be null. So you end up with a tint having a null "tint list".
TypedArray a = context.obtainStyledAttributes(attrs, VIEW_ATTRS, defStyleAttr, 0);
final int ap = a.getResourceId(0, -1);
// Now read the compound drawable and grab any tints
if (a.hasValue(1)) {
mDrawableLeftTint = new TintInfo();
mDrawableLeftTint.mHasTintList = true;
mDrawableLeftTint.mTintList = tintManager.getTintList(a.getResourceId(1, 0));
}
if (a.hasValue(2)) {
mDrawableTopTint = new TintInfo();
mDrawableTopTint.mHasTintList = true;
mDrawableTopTint.mTintList = tintManager.getTintList(a.getResourceId(2, 0));
}
...
The problem is that this tint is applied in the constructor, and each time a drawable is set or changed :
#Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mBackgroundTintHelper != null) {
mBackgroundTintHelper.applySupportBackgroundTint();
}
if (mTextHelper != null) {
mTextHelper.applyCompoundDrawablesTints();
}
}
So if you apply a tint to a compound drawable, and then call a super method such as view.setCompoundDrawablesRelativeWithIntrinsicBounds, the text helper will apply its null tint to your drawable, removing everything you've done...
Finally, here is the function applying the tint :
final void applyCompoundDrawableTint(Drawable drawable, TintInfo info) {
if (drawable != null && info != null) {
TintManager.tintDrawable(drawable, info, mView.getDrawableState());
}
}
The TintInfo in parameters is the mDrawableXXXTint attribute of the texthelper class. As you can see, if it is null, no tint is applied. Setting all drawable tint attributes to null prevents AppCompat from applying its tint, and enables you to do wathever you want with the drawables.
I didn't find a clean way of blocking this behavior or getting it to apply the tint I want. All attributes are private, with no getters.
You can try something like this
ContextCompat.getDrawable(context, R.drawable.cool_icon)?.apply {
setTint(ContextCompat.getColor(context, R.color.red))
}

Android View.getBackgroundColor #XXXXXXXX

The background color of my View is #FFFFFFFF. I want to get this through code. I do not want to just put #FFFFFFFF into the method because I will be changing the background through code, so this value will change all the time.
public void toggleEraser() {
ImageView btnEraser = (ImageView) this.findViewById(R.id.imgEraser);
ImageView btnBrush = (ImageView) this.findViewById(R.id.imgBrush);
if (erase) {
btnEraser.setImageResource(R.drawable.greyeraser);
btnBrush.setImageResource(R.drawable.brush);
} else {
btnEraser.setImageResource(R.drawable.eraser);
btnBrush.setImageResource(R.drawable.greybrush);
}
erase = !erase;
if (erase){
//Here is the problem
drawView.setColor(//drawView.getBackgroundColor());
}
else
drawView.setColor(brushColor);
drawView.setErase(erase);
}
store the color you want 0xFFFFFFFF in somevariable and then
change drawView.setColor( to drawView.setBackgroundColor(somevariable);
Check out this answer: Get the background color of a button in android. it is a little different but can lead you on the right path.

Change HorizontalScrollView's light color - OverScrollMode

At the end of the HorizontalScrollView, a light appears to show that the scroll content has ended. Is there a way to change this color? It is appearing in my phone as a yellow one. I have already set the HorizontalScrollView's background color to the one that I desire, but this "end-of-scroll" light isn't the one I want.
EDIT:
I just noticed that this light appears due to the onOverScrollMode (since API level 9 - see this link). Is there a way to set OVER_SCROLL_NEVER and also keep the compatibility with the Eclair versions? Or even better: to set the color of this light (if it appears)?
Unfortunately there is no simple way to set the color for the OverScroll EdgeEffect.
To safely set OVER_SCROLL_NEVER and remain compatible with early SDK revs, you can introspect for the setOverScrollMode method and if found call it. (tested on 3.1 and 2.2)
setContentView(R.layout.main);
// find scroll view
HorizontalScrollView hscroll = (HorizontalScrollView)findViewById(R.id.hscroll);
try {
// look for setOverScrollMode method
Method setOverScroll = hscroll.getClass().getMethod("setOverScrollMode", new Class[] { Integer.TYPE } );
if (setOverScroll != null) {
try {
// if found call it (OVER_SCROLL_NEVER == 2)
setOverScroll.invoke(hscroll, 2);
} catch (InvocationTargetException ite) {
} catch (IllegalAccessException ie) {
}
}
} catch (NoSuchMethodException nsme) {
}
Although this question has already been answered, I wanted to add a couple of more ways that you can go about setting the overScrollMode attribute.
1) Create a "layout-v10" folder and put your alternate xml layout with the overScrollMode attribute set as desired.
2) If #1 would mean copying a ton of files and duplication, you can alternatively create a style for the HorizontalScrollView.
You can set the EdgeEffect color using reflection. The following will work from API 14+:
public static void setEdgeGlowColor(final HorizontalScrollView hsv, final int color) {
try {
final Class<?> clazz = HorizontalScrollView.class;
for (final String name : new String[] {
"mEdgeGlowLeft", "mEdgeGlowRight"
}) {
final Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
setEdgeEffectColor((EdgeEffect) field.get(hsv), color);
}
} catch (final Exception ignored) {
}
}
public static void setEdgeEffectColor(final EdgeEffect edgeEffect, final int color) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
edgeEffect.setColor(color);
return;
}
final Field edgeField = EdgeEffect.class.getDeclaredField("mEdge");
edgeField.setAccessible(true);
final Field glowField = EdgeEffect.class.getDeclaredField("mGlow");
glowField.setAccessible(true);
final Drawable mEdge = (Drawable) edgeField.get(edgeEffect);
final Drawable mGlow = (Drawable) glowField.get(edgeEffect);
mEdge.setColorFilter(color, PorterDuff.Mode.SRC_IN);
mGlow.setColorFilter(color, PorterDuff.Mode.SRC_IN);
mEdge.setCallback(null); // free up any references
mGlow.setCallback(null); // free up any references
} catch (final Exception ignored) {
}
}

ListView top highlight on scrolling

The border displays a default color (that's orange on my Nexus S) while scrolling a ListView to the limit. How to change that color?
I really don't know how to explain it. Just look at this picture:
So, how to change the highlight color when the ListView scrolling to the border? using themes or styles
The solution is to use setOverscrollFooter(null) and setOverscrollHeader(null).
The documentation is here !
You can also set it directly in the XML :
<ListView android:overScrollMode="never" />
Or specify the footer and the header :
<ListView
android:overscrollHeader="#null"
android:overscrollFooter="#null" />
N.B. : There is also a property fadingEdge that may interest you.
"Overscroll" methodes are supported starting API level 9
Finally I found the solution.
setOverscrollFooter(null) and setOverscrollHeader(null) does not work. At least on 2.3.*. Setting attributes from *.xml doesn't help too.
setOverScrollMode(View.OVER_SCROLL_NEVER) causes glitchy scrolling. At least on 2.3.*.
The only solution that really works involves the use of Java Reflection.
It works even with ugly custom Samsung listviews with bounce overscroll effect.
Here is a snippet:
#Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
//onOverScrolled method must be overrided, or we will see the background of the listview when overscroll fast.
}
private void removeOverscrollEffect() {
try {
Class<?> superClass = getClass().getSuperclass().getSuperclass();
Field field = superClass.getDeclaredField("mEdgeGlowTop");
field.setAccessible(true);
Object edgeGlowTop = field.get(this);
if (edgeGlowTop != null) {
Class<? extends Object> edgeClass = edgeGlowTop.getClass();
Field edgeDrawable = edgeClass.getDeclaredField("mEdge");
edgeDrawable.setAccessible(true);
edgeDrawable.set(edgeGlowTop, new ColorDrawable(Color.TRANSPARENT));
Field glowDrawable = edgeClass.getDeclaredField("mGlow");
glowDrawable.setAccessible(true);
glowDrawable.set(edgeGlowTop, new ColorDrawable(Color.TRANSPARENT));
field.set(this, edgeGlowTop);
}
Field fieldBottom = superClass.getDeclaredField("mEdgeGlowBottom");
fieldBottom.setAccessible(true);
Object edgeGlowBottom = fieldBottom.get(this);
if (edgeGlowBottom != null) {
Class<? extends Object> edgeClassBottom = edgeGlowBottom.getClass();
Field edgeDrawableBottom = edgeClassBottom.getDeclaredField("mEdge");
edgeDrawableBottom.setAccessible(true);
edgeDrawableBottom.set(edgeGlowBottom, new ColorDrawable(Color.TRANSPARENT));
Field glowDrawableBottom = edgeClassBottom.getDeclaredField("mGlow");
glowDrawableBottom.setAccessible(true);
glowDrawableBottom.set(edgeGlowBottom, new ColorDrawable(Color.TRANSPARENT));
fieldBottom.set(this, edgeGlowBottom);
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
I hope this helps.
Here is a nice article on ListView Backgrounds Optimization.
To fix this issue, all you have to do is either disable the cache color hint optimization, if you use a non-solid color background, or set the hint to the appropriate solid color value. You can do this from code (see setCacheColorHint(int)) or preferably from XML, by using the android:cacheColorHint attribute. To disable the optimization, simply use the transparent color #00000000. The following screenshot shows a list with android:cacheColorHint="#00000000"
Use it in XML file--
<ListView ---
android:fadingEdge="none"
---</ListView>
EDITED:
Using fading edges may introduce noticeable performance degradations and should be used only when required by the application's visual design. To request fading edges with API level 14 and above, use the android:requiresFadingEdge attribute instead.
Check this API link
I used kord's answer until it stopped working in Lollipop, so I changed into this:
try {
Class<?> superClass = getClass().getSuperclass().getSuperclass();
Field field = superClass.getDeclaredField("mEdgeGlowTop");
field.setAccessible(true);
field.set(this, new NoEdgeEffect(getContext()));
Field fieldBottom = superClass.getDeclaredField("mEdgeGlowBottom");
fieldBottom.setAccessible(true);
fieldBottom.set(this, new NoEdgeEffect(getContext()));
} catch (Exception e) {
}
class NoEdgeEffect extends EdgeEffect
{
public NoEdgeEffect(Context context) {
super(context);
}
public boolean draw(Canvas canvas) {
// Do nothing
return false;
}
}
you can use android:listSelector="#002234".
In above value can be any color code that you can find on internet easily.

Categories

Resources