Password EditText

Introduction

Ever wished that Android had a built-in Password EditText with a Show/Hide button? No? Well, here’s a tutorial for one anyway.

Password EditText

 

Building a custom Password Edit Text

In order to get a Password EditText we’ll first need to create a custom view extending a EditText. Let’s create a new PasswordEditText.java class:

Next, we’ll need to define some custom attributes for our view. Let’s start off by defining its: action text, colour and size. To make things easier, we’ll need to be able to define these attributes in the view’s layout.xml file. As a result, we’ll need to define these attributes as styleables, by creating a new /res/values/attrs.xml file:

Next, we’ll need to associate these attributes with our custom view. So, let’s start off by giving them some default values:

We’ll now need to initialize them when our view is created:

Awesome, all we need now is the Show/Hide action text. The EditText (being an extension of TextView) has built-in Compound Drawables and we’ll use those to house our action text. Since the end goal is to insert an editable message (SHOW / HIDE) in our custom view, we’ll just need to find a way to convert a String into a drawable – this is where the magic happens. We’ll create TextView and style it with our custom attributes (text, colour, size). Once we’re happy with it, we just essentially take a screenshot of it, convert that into a drawable and assign it as a Compound Drawable:

Great, now we’ve got a custom drawable inside our view. We just need a way to handle touch events to determine what happens when users click it. To do this, our custom view will need to implement a View.OnTouchListener to handle this event. Then, all we have to do is get our drawable’s dimensions  and decide what happens when a touch event is registered within those coordinates:

Wrapping things up

And that’s basically it. We’ve created a custom Password EditText, defined it’s attributes, added a custom editable drawable and defined what happens when users tap it. This is how the final PasswordEditText.java class looks like:

We just need to add it to res/layout/activity_main.xml and see it in action:

Notice that we’re setting our custom attributes in a XML layout file. We can also do the same programmatically:

 

That’s it. Check out the full source code here.

 

Share this on:

Logcat Recorder

Introduction

Logcat is an Android mechanism used to collect and view system debug output. This is extremely useful for debugging and QA testing, as it provides valuable information about what the apps are doing.

Under normal conditions, logcat is visible via Android Device Monitor or directly executed from the ADB shell via the logcat command. The purpose of this tutorial is to present a simple mechanism to programmatically capture and filter logcat entries.

Logcat recorder

First thing’s first, we need a nice way to handle the logcat recorder’s status (recording, new log entry, idle). As a result, we create a simple OnLogcatRecorderListener.java interface to define these events:

Next, we need to define a basic LogcatRecorder.java class and introduce our listener:

Now, we need a mechanism to start() recording the logcat. This will essentially create a new thread (to avoid locking the main UI thread), containing a Runtime Process to execute the logcat shell command and continuously record its output:

We first need to clear the current log via the “logcat -c” runtime process call. Once the log is clear, we can start recording it Runtime.getRuntime().exec(“logcat“) and, optionally, add a filter to our recorder Runtime.getRuntime().exec(“logcat | grep ” + filter)Whilst recording, we call our interface’s onNewLogEntry() method to pass on each new log entry.

To wrap this up, we also need a method to stop() the recording process:

In order to do this, we first check our recorder’s state and simply destroy the logging Runtime Process. Once the recording is done, our interface will serve the full list of recorded log entries via onLogcatRecorderListener.onStopRecording(log.toString());

Finally

We just need to add the LogcatRecorder to a new Activity. First, we define ../res/layout/activity_main.xml:

Finally, we define MainActivity.java and add a new instance of our LogcatRecorder to it:

 

Get the full project on GitHub.

Share this on: