Posts Tagged ‘Java’

These posts contain code written in Java or are somehow related to Java.

Android: hexadecimal color input using an EditText

Click here to read Android: hexadecimal color input using an EditText

This Android programming tutorial shows how to create a simple application that takes the value of a hexadecimal color at an EditText field and displays it as an ImageView. In order to do so, this post goes into detail on how to correctly parse the EditText String as 32 bit hexadecimal Integer. Additionally, it explains the part of the code that constrains the characters between A to F and 0 to 9 at the EditText. Finally, it tries to solve some of the EditText problems when applying these constrains.

The application in this post has been tested on Android 2.1, 2.3 and 4.0.

So, here’s a screenshot of the example project application in action:

Hexadecimal Color Input

A screenshot of the example project. It’s available for download at the end of the post.

(more…)

Android: Change color of the standard button inside Activity

Click here to read Android: Change color of the standard button inside Activity

This post explains how to change the color of the standard Android button. It also shows how to change the color of the text inside those buttons. It goes into detail on how to create a ColorFilter effect that changes the color of the button using three different approaches . The code featured in this post has been tested on Android 2.1, 2.2, 2.3 and 4.0.

Before starting, a little advice: know that changing the color of the standard Android button through XML is an easier and more straightforward approach. For that, please refer to the link Custom color buttons for Android (9-Patch). If the button color isn’t going to be changed after the application is initialized, then use XML. If that isn’t the case, then do it programmatically, which is what this tutorial focuses on.

So, here’s the code: (more…)

Android: get String resource at another XML namespace

Click here to read Android: get String resource at another XML namespace

This post shows the necessary steps to obtain the correct String resource outside the Android XML namespace. It has been written specifically for dealing with string resources associated with Preferences inflated from XML files, so this post assumes that the reader is familiar with writing customized preferences for Android. Altought, it can probably be used on other situations. This code has been tested on an emulator running Android 2.1 and also on real devices running Android 2.1, 2.2 and 4.0.4.

(more…)

Android: how to rotate a View element

Click here to read Android: how to rotate a View element

Another Android programming tutorial, this time, explaining how to rotate a View element using two distinct approaches. Not only that, but this post also presents a brief explanation on the advantages and disadvantages of each approach. The code featured here has been tested on both the emulator and on a real device running Android 2.1 . Before going any further, please bear in mind that from Android 3.0 (API level 11), a setRotation() method has been added to the View class, therefore, it should be used instead of the code described in this article, which focuses on lower level APIs.

The first and perhaps most direct method of rotating a View element is to create a class that inherits from a View widget that needs to be rotated, for example, the TextView or the Button. Then, inside this class, the onDraw() method must be overridden. (more…)

Android: Creating a two color LED notification

Click here to read Android: Creating a two color LED notification

Another programming tutorial, this time, showing how to make the built-in notification LED on a Android device continuously alternate between two colors. That said, by the time this article is being written, it’s recommended to try this code on a real Android device that has a notification LED instead of running the application on the emulator. Also, the Activity featured below has been created to work on devices with Android 2.0 or later. All code featured in this article is available for download at the end of the post.

To continuously change the colors of the LED, it’s necessary to create and initialize a Notification object that changes the LED colors and post this notification using a handle to the system’s notification service (using a instance of the NotificationManager class). The notification is then canceled, its LED color is changed and the notification is posted again and the process is repeated over and over making the colors swap back and forth. (more…)