ViewPager with fixed background image

Problem

I want to have an image(such as a phone frame) fixed in its position while I swipe through contents in fragments on ViewPager. An example is “What’s New” or “Features” screen like gif animation below. Or maybe something fancier with your creativity.

viewpager_gif

Approach

It’s simpler than it looks. On top of a regular implementation of fragments ViewPager in an Activity,

  • Make your Fragment layout background transparent
  • Wrap ViewPager and ImageView in FrameLayout within your Activity layout so that you can center the image underneath ViewPager
Code Sample


[Continue reading…]

Implement your own keyboard for Android Wear app

Problem – Key Input in Android Wear

The question is, “how do you take key inputs on Android Wear app?”. I worked on a Wear app that needs to take number inputs. I needed a calculator-like number keyboard with keys for 0-9, -(negative sign), .(decimal), x(backspace), and C(clear).

If you encounter a similar need, this article might help you get an idea for your solution.

Approach – DialogFragment as Keyboard

We can make use of a DialogFragment to represent a keyboard.

keyboard_wear


[Continue reading…]

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