NewFiveFour | Blog | Portfolio


Vim: rebind escape and caps lock on the command line

If you are using vim a lot, and touch type, you may find reaching up to press escape all the time ruins your typing.

You can rebind escape to caps lock, and vice versa, throughout all your X apps using xmodmap.

Use this in ~/.Xmodmap

clear Lock
keycode 9 = Caps_Lock
keycode 0x42 = Escape

And then use the command, probably in your ~/.bashrc, xmodmap ~/.Xmodmap to do the rebind.

vim unix

Vim: command line mode

First set -o vi and you can press escape (or whatever you’d bound that to) to use the vim commands on the current commanded typed in on the command line.

You can also press v and it will open up vim and you can edit the command in the full vim editor.

You made need to put EDITOR=vim in your .bashrc file.

vim unix

Use html to order a border with a title around things

You can group items with a border in HTML using fieldset and you can also give that border a title using legend.

The title breaks the border briefly at the top.

Here’s an example which you can use to impress your friends, family, dogs, cats and potential parliamentary representatives.

<fieldset style="border: 1px dashed black">
  <legend>Ladies and gentlemen we are floating in space</legend>
  Sup
</fieldset>
css html

HTML, CSS: Make a div float in the middle of page

First make the position of the div fixed. We’ll place it at the bottom of our screen by setting the bottom to 5px.

We make it so the div starts from the middle of the screen by setting the left property to 50%.

But this won’t make it exactly in the middle of the screen, of course, since this mean it starts from the middle of the screen, not that its centre point is in the middle of the screen.

To properly centre the div use the transform property to move the div half its width to the left.

  <div
    style="position: fixed; bottom: 5px; left: 50%; transform: translateX(-50%);">
    I am centered. Rejoice.
  </div>
css html

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 the ViewGroup class.

onLayout()

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

@Override
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.

onMeasure()

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

@Override
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

Page 3 of 80
Previous Next