Archive for 'Android' Category

Spinner-like TextView: A Dialog Spinner that displays default “Select” text

Problem – Setting default text to a Spinner

Spinner out of the box does not allow you to set a value that displays by default and not appear in the list of options.

Approach – Custom view

We can imitate the behavior of Spinner with the following ideas:

  • Have a TextView that visually looks like a Spinner
  • Display a default text(e.g. “Select”) at first initialization.
  • When it is pressed, display a AlertDialog.
  • When an option is selected, update the text on the TextView.

default_text
dialog


[Continue reading…]

Handling click events within AdapterView such as ListView and GridView

Problem – Events within row/cell

We want to have an AdapterView, which is extended by ListView and GridView, with clickable buttons inside its row or cell while still being able to tap on the row/cell itself.

When an AdapterView contains clickable views such as buttons, the listener registered to the clickable view will take over the click event disallowing onItemClick on AdapterView to fire.

As a quick fix, we could just define OnClickListener for the buttons in the adapter class, but this would separate the code for responding to events into 2 places, for instance, activity and adapter. To keep the code clean, we want to write the listener callback in one place.


[Continue reading…]

How to create an Android Battery Widget

I will go over how to create a simple battery widget for Android. The complete project used in this tutorial can be downloaded from my github. I decided to write this because I had some hard time looking for information to learn how to write a battery widget. Hopefully this will help someone.

Implementation

This widget displays the battery level with the time interval specified in the code. I use alarm to periodically check if the battery level has changed. Since we don’t want to waste the battery power, we want to keep the attempts to check the battery level to minimum.

When the widget is enabled, it starts a service that wraps 3 BroadcastReceiver objects to watch for 1. screen off, 2. screen on, and 3. when the user is passed through lock screen.

[Continue reading…]

Pages:<12

Categories