Java: Adding a Element under an Element (or Converting a Element to a Node)

When using Document a Element can only be appended with a Node, but what if we have a Element ?

Easy, Element extends Node so just pass the Element along and all is well

import org.w3c.dom.Document;
import org.w3c.dom.Element;
 
Document doc;
Element newElement;
Element oldElement; //code to find that
newElement = doc.createElement("tag");
oldElement.appendChild(newElement);

Java: Creating a enum and converting from a string to the Enum

Here is a simple example of how create a Enum and how to convert from a String to the Enum (the common reason for doing this would be is we at some point have for OurEnum.toString() and now wishes to get back to the OurEnum)

First we need to create the Enum, and this needs to be in its own file

public enum OurEnum { ENUM_A, ENUM_B, ENUM_C }

When we then later on (for instace have saved the toString value in a xml file) wish to

String ourEnumAsAString = "ENUM_A"; //replace with code to read the value
OurEnum ourEnum = OurEnum.valueOf(ourEnumAsAString);

Android resize a bitmap

Here is a snippet to scale down a bitmap – the original bitmap still needs to fit on memory though

InputStream bitmap = mContext.getAssets().open("image.file");
Bitmap scaledBitmap= Bitmap.createScaledBitmap(BitmapFactory.decodeStream(bitmap), 120, 120, false);
bitmap.

Android set background color of ActionBar

Here is a snipet that will set the background color of a ActionBar to a hexcolor.

final ActionBar bar = getActionBar();
 
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000")));

Color.parseColor will take any hexstring of a color and make it into a int.

git diff “old mode 100755 new mode 100644”

What git is trying to say here is that the mode of the files have changed, the content of the file is still the same.

If this is common and the filemodes are not important for this project we can simply tell git to ignore this.

Either by telling git to do this as default or only for this project.

Default

git config core.filemode false

Only for this project edit .git/config

[Core]
filemode = false

And in case we do need to check in single filemode changes the following works

git update-index --chmod=(+|-)x path/to/file

Android: Suppressing the warning about using px in layout xml

When working with for instance the Sony SmartWatch there is a need to specify layout for the Watch in pixels (not device pixels) – as the SmartWatch is a fixed size and not dependent of the device it is connected to. When this is done the lint verification will warn about this being a bad idea, but it is possible to suppress this warning by adding the following to the xml file:

  1. add the namespace xmlns:tools=”http://schemas.android.com/tools”
  2. and then then you can disable the px-warnings by including this in your view: tools:ignore=”PxUsage”.

Example:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/background"
>
 
    <View
        tools:ignore="PxUsage"
        android:id="@+id/smartWatchView"
        android:layout_width="128px"
        android:layout_height="128px"
        android:visibility="visible"
        android:layout_centerInParent="true" 
    />
 
</RelativeLayout>

SQLServer Adding a column to a table

Adding a column to existing table is not that hard. One thing to keep in mind, if the column is not nullable then a default value is mandatory.

ALTER TABLE {TABLENAME} 
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} 
CONSTRAINT {CONSTRAINT_NAME} 
DEFAULT {DEFAULT_VALUE}

Git SSL certificate problem – how to turn off SSL validation for a repo

This will start out with an error such as the following:

$ git pull origin master
error: SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed while accessing https://github.com/mopidy/mopidy.git/info/refs

If it is acceptable to turn off the SSL validation instead of actually solving the issue this will turn off validation for the current repo

git config --local http.sslVerify false

If you would rather have this as a default behaviour for git then the following will do it for all repos

git global --local http.sslVerify false

and for those that would rather add to the .git/config file directly the entry looks like

[http]
    sslVerify = false

Samsung Omnia II Master reset

In order to do a factory reset simply open the phone and dial “*2767*3855#”

The phone will restart and then do a master reset (make sure the battery is not too low now, that will be bad).

Another way should be “Settings > Basic Settings > Memory Settings > Clear memory > Enter + 1234 and confirm”

Another way is to hold all of the following buttons at once “green dial + power + volume up+ keylock” that will give you a question if you are sure (green dial confirms).