NewFiveFour | Blog | Portfolio

Android: Custom View's onLayout and onMeasure methods

If you want to layout a custom view’s children yourself, as opposed to making the custom view extend LinearLayout or similar, you need to implement the onLayout and onMeasure methods of ViewGroup.


Let’s first look at onLayout(). This tells your custom view’s children where they should lay themselves out:

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  childCount = getChildCount();
  for(int i=0; i<childCound;i++) {
    View v = getChildAt(i);

This method is called by the ViewGroup, passing in the parent’s dimensions.

The ‘left’ and ‘top’ is the left and right from the custom view’s parent, this is normally zero unless you have set margins on your custom view.

Then we get the number of children and are about to process each in the loop.

The method we must call on each of the views’ in the loop is

v.layout(left, top, right, bottom). 

If ‘left’ or ‘top’ are 0 this means right at the left and top edge of the parent.

If you have defined some padding on your custom view, you must include these in your layout call, otherwise the padding will be in front of the view.


Before you can call methods such like getMeasuredWidth() on your ViewGroup’s children, however, you must tell them how to measure themselves in onMeasure:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

The two parameters passed in are int values which represent the mode and side of the width and height.

Let’s say you passed in match_parent to the layout_width parameter in your View’s XML, and the width of your view’s parent is 400, then the following would be true:

int mode = MeasureSpec.getMode(widthMeasureSpec) // mode == View.MesaureSpec.EXACTLY
int size = MeasureSpec.getSize(widthMeasureSpec) // size == 400

In this method you must call either its super method or setMeasuredDimension() with the width specifications and height specification either passed into the method or created with MeasureSpec.makeMeasureSpec(size, mode).

You must also call the measure(widthSpec, heightSpec) on the child views too.

  for(int i=0; i<childCound;i++) {
    View v = getChildAt(i);
    v.measure(widthSpec, heightSpec);

If you wanted to give each view the same measurements as its parents (BUT WHY???) you could give it its parent’s measure specs, or create your own via the makeMeasureSpec above, perhaps by using a division of the parent’s getMeasuredWidth(), taking into account any padding using the getPadding*() methods.

android android-custom-view

Android: Setting network latency

You can create network latency on either the emulator via telnet. Telnetting to port 5554 on localhost will give you access to the first emulator connected. ( see there for more information)

Then type ‘network status’ to see what’s currently set. ‘network delay [num]’ allows you to set it by a predefined about. ‘newwork delay gprs’ allows you to set the delay to the range gprs would give you.

 $ telnet localhost 5554
 Connected to localhost. Escape character is '^]'.
 Android Console: type 'help' for a list of commands
 network status
 Current network status:
   download speed:          0 bits/s (0.0 KB/s)
   upload speed:            0 bits/s (0.0 KB/s)
   minimum latency:  0 ms
   maximum latency:  0 ms
 network delay gprs
 network status
 Current network status:
   download speed:          0 bits/s (0.0 KB/s)
   upload speed:            0 bits/s (0.0 KB/s)
   minimum latency:  150 ms
   maximum latency:  550 ms
 network delay 7000
 network status
 Current network status:
   download speed:          0 bits/s (0.0 KB/s)
   upload speed:            0 bits/s (0.0 KB/s)
   minimum latency:  7000 ms
   maximum latency:  7000 ms
android android-networking

A simple markdown blog using three lines of bash

Imagine you’ve got a load of markdown files such as etc.

And that you have a command, markdown, that converts markdown to html. (This exists: look it up).

This will convert all your markdown files to HTML and put them in a directory called tmp which you will have to create.

for i in `ls *.md`; do    markdown $i > tmp/`basename $i .md`.html; done

Now this next command will surround all your new HTML files with a <div class='entry'> tag and output the whole lot into entries.html.

ls tmp/* | xargs -n 1 awk 'BEGIN {print "<div class=\"entry\">" } {print $0} END {print "</div>"}' > entries.html

Finally create a template.html file with the lines <!-- insert here --> somewhere in the middle.

Then this final command will insert everything in entries.html into your template.html file and output the result into a new file, index.html

sed -e '/<!-- insert here/r entries.html' template.html > index.html
unix bash

Insert one file into the middle of another using Sed

Let’s say you have a file called file1. And let’s also say you have a file called file2.

And within file1 you have this text in the middle of your file: Yusef Lateef.

If you run this command,

sed -e '/Yusef Lateef/r file2' file1

Then below Yusef Lateef in file1 you will find the content of file2.

unix sed

Linux: Connect to open and closed networks from the command line with a single command.

You can easily connect to an open network though

iwconfig wlan0 essid "THE NETWORK NAME"

Closed networks work with wpa_supplicant. You normally send a configuration file with that but you can simulate the file with a named pipe:

wpa_supplicant -iwlan0 -c <(echo -e 'network={\n ssid="THE NETWORK NAME" \n psk="THE PASSWORD" \n}') -B

wlan0 is your network interface name. -B means go into the background. The \ns in the configuration file is needed sadly and so we pass -e to echo to interpret them.

iwconfig should show your new connection but prehaps only after a second.

After this dhclient wlan0 will get you a dhcp address and you’ll be ready to go. You may want to kill any prior instances of dhclient.

unix wifi

Page 2 of 84
Previous Next