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>

GCM notification with custom sound – shorter version (part)

Here is a short example how to create notification with default stuff but different sound:

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("title")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("message"))
.setContentText("message")
.setAutoCancel(true)
.setSound(soundUri)
.setDefaults(NotificationCompat.DEFAULT_ALL ^ NotificationCompat.DEFAULT_SOUND);

The most important part is here:

.setDefaults(NotificationCompat.DEFAULT_ALL ^ NotificationCompat.DEFAULT_SOUND)

Symbol

^

says “except”, so the last part would say “give me default notification except sound setting”

Custom checkable list item with checkbox (layout+code)

Here is simple example how to create simple checkable list item with checbox.

This a code for widget:

public class CheckableTextView extends LinearLayout {

private TextView checkLabel;
private CheckBox checkBox;

public CheckableTextView(Context context) {
super(context);
}

public CheckableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();

}

public CheckableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

private void init() {
View v = inflate(getContext(), R.layout.widget_checkabletextview_merged, this);
checkLabel = ((TextView) v.findViewById(R.id.checkLabel));
checkBox = ((CheckBox) v.findViewById(R.id.checkBox));

v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
checkBox.setChecked(!checkBox.isChecked());
}
});
}

public void setText(CharSequence c) {
checkLabel.setText(c);
}

public void setText(String c) {
checkLabel.setText(c);
}

public void setChecked(boolean status)
{
checkBox.setChecked(status);
}

}

This is the widget’s layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:background="?android:attr/selectableItemBackground">

<CheckedTextView
android:id="@+id/checkLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="Sample text" />

<CheckBox
android:clickable="false"
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />

</LinearLayout>

Example of one item:

layout-2015-01-07-115016