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”

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”

Working example of setting alarm with repeating stuff, checking if alarm was set with pendingIntent

Here is short and quite (imho) understanding part of code:

//starting #1
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getActivity(), MyReceiver.class);
intent.setAction(MyReceiver.ACTION_ALARM_RECEIVER);//my custom string action name
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);//used unique ID as 1001
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), aroundInterval, pendingIntent);//first start will start asap

#1 – first we are getting alarm service. We create own intention and setting own action to it to be able handle in onReceive(), in our receiver class. Then the intention is putting into pendingIntent. The complete pendingIntent is used by alarmManager for a start.
I used System.currentTimeMillis() because I wanted start repeating asap.
I used flag PendingIntent.FLAG_CANCEL_CURRENT for pendingIntent but it might be also PendingIntent.FLAG_UPDATE_CURRENT. It is important to use the same flag everywhere, but when you want stop it, you use PendingIntent.FLAG_NO_CREATE. I will write about it later.
Number 1001 in pendingIntent was used just to be know that our intention has unique ID.

Continue reading “Working example of setting alarm with repeating stuff, checking if alarm was set with pendingIntent”

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

Smaller ratingboard with clickable area

During my one day project for a client I had to use (I hate it) android’s ratingbar component. One of the pointed task in project was to use a bit smaller object with 5 stars than it is as default. What is more it had to be place it in every item in listview.
So what’s the problem? “All you have to do is to use param

style="?"

” – I thought. But it has not been the final solution yet..

Continue reading “Smaller ratingboard with clickable area”