Notification Resender. Part 2

Notification Resender - allows Zeroner to resend notifications from all apps

Hi guys!
I’m continuing my series of posts about creating Android app for resending notifications. First part you can read here:  http://dbondarchuk.com/2016/09/22/notification-resender-part-1/

So, we have already created our project in Android studio, with MainActivity layout and Java class.

Lets change Main Activity to show our current settings and allow to add/edit/remove them.

For displaying settings the best option is to use a ListView. But we don’t need just a text in an item. The good ListView item will contain setting name, control to enable/disable setting and remove button:Notification Resender MainActivity

So, we need to add our ListView to the MainActivity. Open main_content.xml layout file and replace its content with following code:

Here we added a text label with text “Your settings:” (add it to the strings.xml file with name “settings_list“: <string name="settings_list">Your settings</string> . Here and below don’t forget add string values (starts with @string/some_id to the strings.xml file)) and id “textView” and a ListView with id “listView“, wich should be placed below textView( android:layout_below="@+id/textView" )

The next step is to create custom view for the ListView item. Create a new Layout Resource file with name “resendsetting_listview_item.xml” and place such code:

Here we are saying, that our item will be contained inside horizontal LinearLayout and will contain TextView for setting name, Switch for enabling/disabling setting and ImageButton for removing with built Android image for red cross sign ( android:background="@android:drawable/ic_delete" )

Okay, we have our custom layout for ListView item and now we need to connect it to our ListView.

First of all, we should create a class for setting model. Lets create a Java class named “ResendSetting“:

Our ResendSetting will contain such properties: id, name, is it enabled or not, title and body for resended notification, some text filter for check if original notification should be ignored, flag that says that this filter should be a Regex filter, delay time for resended notification removal and list of apps, from which notifications could be accepted. It’s pretty simple 🙂

Now, we need create an Adapter, that will allow us to apply our ResendSetting model to our ListView item. Lets create another Java class, called ResendSettingArrayAdapter:

Lets break this on the parts. First of all, we have a class ResendSettingArrayAdapter that extends ArrayAdapter for ResendSetting class. It has a constructor, that takes Context, Resource, and list of Objects (our settings).

When the new item is created, getView method is called and position number of the current item in objects array is passed. Using it, we are loading our item template and assign name and enabled values to corresponding TextView and Switch:

Then, we can add handlers for Switch and Remove buttons:

When, Switch state was chanded, this handler gets an event and process it by setting enabled value for object in position position new value and shows a Snackbar with text, that setting with name ‘objects.get(position).name‘ was enabled/disabled.

Please take attention on how string value is getting on showing Snackbar:

In a context, getString method is called and depends on isChecked value, setting_was_enabled or setting_was_disabled value is taken. Also the name of the setting is passed as argument to these strings. Here an example, how this strings should be defined in strings.xml:

Here, apostrophes ( ‘ ) should be escaped by backslash: \’ .
%1$s – it’s a mask for replacing argument (our setting name), where  %1 means number of argument, and  $s means type of the argument (– for string, d – for digital etc)

For the Remove Button our handler will look like:

When we will click on the Remove Button, a new AlertDialog will be shown with title  R.string.are_you_sure_title, message  R.string.are_you_sure_delete_setting_message, name, default android icon for dialog alert  android.R.drawable.ic_dialog_alert, and two buttons: Yes or No.

For this buttons’ captions we will use default android strings:  android.R.string.yes and  android.R.string.no instead of creating ours.

When user will click on No button, nothing will happen, just alert dialog will close. On the Yes button, we will remove this item from the objects array and show a Snackbar.  adapter.notifyDataSetChanged(); is needed to notify ListView, that items were changed and we need to redraw them.

Ok, we have model, layout and adapter. Now we should connect them into the one thing.

Open the MainActivity.java file and add such code to the onCreate method:

Here, we will create our adapter object with passing current context, id of the ListView item layout and settings into it. Then we will take our ListView by its id and set our adapter to it.

The one thing, that has left – is creating an array of settings. In the next posts, I will tell about working with local database to save/edit/remove/get our settings from it. But now, we can fake it and create a simple ArrayList with settings:

That’s it 🙂 Now we have our main screen with list of our settings 🙂

In the next posts I will tell about adding a new settings, working with database and receiving notifications.

Thanks!

P.S. #1 All source code you can find in my GitHub – https://github.com/dbondarchuk/Notification-Resender

P.S. #2 You can also download app for your bracelet here – https://github.com/dbondarchuk/Notification-Resender/releases

 

 

Leave a Reply