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”

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”

Clearing items in MultiSelectListPreference – solution

Hi, today I was working on MultiSelectListPreference in PreferenceFragment screen. I was going to have multi select list where user would be able select items. Each selected item would be describing specific shortcut from my app to OP desktop.
After about half an hour I found out how to restore MultiSelectListPreference with all default items.

Continue reading “Clearing items in MultiSelectListPreference – solution”

Android NavigationDrawer not handling setOnItemClickListener- nothing happens – solution

During my graduation work I had to use navigation drawer for menu list. What is more, I also needed pull to refresh layout. Both items are very sensitive and its order matters where to put them. You might be in situation where you think which one should be in which one. So during rebuilding layout with that two items first I moved pull to refresh layout up, to be a root layout. This showed me unpredictable result.
I was able swipe down to refresh even when I had already opened drawer…

Continue reading “Android NavigationDrawer not handling setOnItemClickListener- nothing happens – solution”

Pull to refresh in project, disabling refresh when we want to scroll down

I have been working for my graduation work on layout `pull to refresh`, called now.. SwipeRefreshLayout. It was connected with NavigationDrawerFragment.
During tests over that two things I discovered I was not able to scroll down my list without calling refresh. I found very interesting solution on stackoverflow. Answer was posted by @Cabezas. Thank You!

Continue reading “Pull to refresh in project, disabling refresh when we want to scroll down”

Pretty nice decimal format for polish currencies

Here is the simple sample code which converts pretty nice for example polish currencies to float and from float to polish format.
These are the spoiler results:
DecimalUtil.getInstance().parse("1 123,44) -> 1123.44
DecimalUtil.getInstance().format(1123.44) -> 1 123,44

Continue reading “Pretty nice decimal format for polish currencies”