Horizontal progressbar with custom color

activity_main.xml

<progressbar android:id="@+id/progressBar"
style="?android:progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="0dp"
android:max="100"
android:padding="0dp"
tools:progress="40"
android:progressDrawable="@drawable/progress_drawable"
app:layout_collapseMode="pin"/>

MainActivity.java

ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressBar);

//somewhere when scrollin
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

int lastVisiblePosition = mLayoutManager.findLastVisibleItemPosition();
int allItem = mLayoutManager.getItemCount();

if (allItem > 0) {
double v = ((double) lastVisiblePosition / (double) (allItem - 1)) * 100;
if (mProgressBar != null)
mProgressBar.setProgress((int) v);
}
}
});

progress_drawable.xml

< ?xml version="1.0" encoding="utf-8"?>
<layer -list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<shape>
<solid android:color="@android:color/transparent"></solid>
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="@color/badge_2"></solid>
</shape>
</clip>
</item>
</layer>

Converting transparency color to hex

Here is very interesting post I have found on stackoverflow. I though it is worth to repost here.

Android uses Hex ARGB values, which are formatted as #AARRGGBB. That first pair of letters, the AA, represent the Alpha Channel. You must convert your decimal opacity values to a Hexdecimal value. Here are the steps:

Alpha Hex Value Process

  1. Take your opacity as a decimal value and multiply it by 255. So, if you have a block that is 50% opaque the decimal value would be .5. For example: .5 x 255 = 127.5
  2. The fraction won’t convert to hex, so you must round your number up or down to the nearest whole number. For example: 127.5 rounds up to 128; 55.25 rounds down to 55.
  3. Enter your decimal value in a decimal to hexadecimal converter, like this http://www.binaryhexconverter.com/decimal-to-hex-converter, and convert your values
  4. If you only get back a single value, prefix it with a zero. For example, if you’re trying to get 5% opacity and your going through this process you’ll end up with the hex value of D. Add a zero in front of it so it appears as 0D.

That’s how you find the alpha channel value. I’ve taken the liberty to put together a list of values for you. Enjoy!

Hex Opacity Values

  • 100% — FF
  • 95% — F2
  • 90% — E6
  • 85% — D9
  • 80% — CC
  • 75% — BF
  • 70% — B3
  • 65% — A6
  • 60% — 99
  • 55% — 8C
  • 50% — 80
  • 45% — 73
  • 40% — 66
  • 35% — 59
  • 30% — 4D
  • 25% — 40
  • 20% — 33
  • 15% — 26
  • 10% — 1A
  • 5% — 0D
  • 0% — 00

Better style for stackoverflow front page – grasemonkey script

Zrzut ekranu 2015-01-26 10.29.45

Here is script for better front page style.

[javascript]
// ==UserScript==
// @name Better style for stackoverflow front page
// @namespace
// @description changes background of front page and interesting items
// @include *stackoverflow.com*
// @version 1
// @grant GM_addStyle
// ==/UserScript==
document.body.style.background = “#f0f0f0”;
GM_addStyle(‘.tagged-interesting {background-color:#f5f5f5; padding:5px}’);
[/javascript]

Working example – changing drawable background in runtime, dynamically

Here is a short example code how to do it:
I’ve created xml file shape.xml and one method for flexible changing color of shape.

shape.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@android:color/holo_blue_bright" />
<corners android:radius="30dp" />
<size
android:height="15dp"
android:width="15dp" />
</shape>

And here is a method for flexible changing color:

Continue reading “Working example – changing drawable background in runtime, dynamically”