Friday, February 27, 2015

Android: UI: horizontal divider

In this recipe we will add a thin gray line divider to our UI



       <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginTop="6dp"
            android:alpha="0.5"
            android:background="@color/bar_grey_light"/>



Android: xmlns res-auto

You can use "res-auto" URI instead of defining custom packages:

so...

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:my_tag="http://schemas.android.com/apk/res-auto"

instead of...

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:my_tag="http://schemas.android.com/apk/res/com.my_company.xyz"



and then use your tag...

       <com.my_company.xyz.control.MyPanelView
                android:id="@+id/jdhfkjsd"
                style="@style/kjdlksjdhjhk"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                my_tag:header="@string/kjsdlfkdslkfj"

Java: Random


In this example we create a random speed jump by a certain constant.

private Random random = new Random();

private static final double SPEED_M_PER_S = 2.2352;  /* 5 MPH */
private static final double MAX_RANDOM_DELTA = 0.13;

double speed = SPEED_M_PER_S 
+ Math.min(MAX_RANDOM_DELTA, random.nextDouble()) 
* (random.nextBoolean() ? -1 : 1);

Thursday, February 26, 2015

Java: Unsupported major.minor version 52.0

This error basically means that you compiled your Java code with the version higher than that on the target machine you are trying to run it on.

 $ java -versionjava version "1.8.0_20-ea"


Java 1.8 == 8 == 52 (go figure why this confusion)

J2SE 8 = 52,
J2SE 7 = 51, 
J2SE 6.0 = 50, 
J2SE 5.0 = 49, 
JDK 1.4 = 48, 
JDK 1.3 = 47, 
JDK 1.2 = 46, 
JDK 1.1 = 45

Solution 1: update Java version on target machine (often impossible)
Solution 2: compile with lower Java version (skip on fancy language features e.g. lambdas) 

e.g. using Maven

   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
               <!-- target has java version "1.7.0_60", to be safe we use 1.6  -->
               <source>1.6</source>
               <target>1.6</target>
            </configuration>
         </plugin>


e.g. using Java Compiler

javac -target 1.6 HelloWorld.java

Tuesday, February 24, 2015

Java Locale p4

Create FXML layout



Java: Locale p3

in this tutorial part we will EXTEND ResourceBundle.Control to make sure Java properly reads our language properties files.


Step:
create public class UTF8Control extends ResourceBundle.Control

Step:
find package java.util public abstract class ResourceBundle


Step:
find public static class Control

Step:
find public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)


Step:

copy this method to your new class

Step:


CHANGE only what you see in line 76





Java: Locale p2

In this part we will create a simple utility class that will choose the right Locale

Note:

  • using static method
  • we are passing in character country code
  • we are returning Locale
  • this could grow to 180+ cases





Java: Locale p1

In this tutorial we will create JavaFX app that uses selected Locale to pull i18n (Internationalization) text.


Note:

  • round buttons
  • selected county name
  • text in different character set
  • money notation depending on selected country





Step : create a new IntelliJ Module

  • Module name DisplayLocale
  • create packages as shown in the picture below
  • find country flags icons about 64x64x
  • get some translation texts

Step: Create your translation .profile files

Example Australian English file

morning_greeting=G'day Mate!evening_greeting=G'evening Mate!


Step: Create class DisplayLocaleMain extends Application

Note:
  • import java.util.*
  • import javafx.*
  • extends Application -- JavaFX Application
  • implements Initializable -- automatic injection of Locale 
  • main method
  • @FXML - annotation
  • @override start method
  • css/buttons.css





Note: 

  • @Overrite initialize method
  • Currency.getInstance(bundle.getLocale());
  • NumberFormat.getCurrencyInstance(bundle.getLocale());

Note:
  • FXMLLoader fxmlLoader
  • ResourceBundle.getBundle("bundles.Language", locale, new UTF8Control());
  • fxmlLoader.setResources(languageBundle);
  • getResource("views/TranslatedPhrasesPane.fxml");
  • Pane pane = (BorderPane) fxmlLoader.load(url.openStream()); stage.getScene().getRoot()).getChildren().get(1); LanguageButtonEvent implements EventHandler
  • @Override handle method
  • LocaleBuilder.byLanguageCode(button.getId());







see next part

Thursday, February 19, 2015

Android: mipmaps instead of drawables

I started to use MIP (latin "multum in parvo", or "much in little") maps in Android 4.3 code, but I still have to come up with a good explanation of this optimization technique.


Google says:

It’s best practice to place your app icons in mipmap- folders (not the drawable- folders) because they are used at resolutions different from the device’s current density. For example, an xxxhdpi app icon can be used on the launcher for an xxhdpi device.


in AndroidManifest.xml


<application android:label="@string/app_name" android:icon="@mipmap/ic_launcher">
instead of
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">


Friday, February 13, 2015

Go: Hello World

Time to learn another language!

https://golang.org/dl/



IntelliJ IDEA had a plugin that is auto-detected when you open a file hello.go

package main
import "fmt"
func main() { fmt.Printf("hello, world\n")}

Wednesday, February 11, 2015

Java: manually holding execution

This snippet comes in handy when you want to hang execution of the command line app until user presses ENTER on the keyboard:


BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Waiting for you to press ENTER:");
reader.readLine(); // waiting for user indefinitely until ENTER is pressed

Tuesday, February 10, 2015

Java: Chicago Public Salary Analyzer



Visit page and take a look at the data:

https://data.cityofchicago.org/Administration-Finance/Current-Employee-Names-Salaries-and-Position-Title/xzkq-xp2w

Download data as CSV (Commas Separated Values) which is used commonly to import-export spreadsheet type of data.

Create a new IntelliJ IDEA Module:

  • Maven project
  • GroupID: edu.clcillinois.cit137
  • ArtifactId: chi_pub_salary_analyzer
  • Module name chi_pub_salary_analyzer
If you see Notification: "Maven projects need to be imported"
  • Enable Auto-Import

Create Main class







Create package data.reader

Reader



TextFileReader




Create new Package "data.parser"

Create new Interface Parser in which we will outline methods we want to GET our data:


CVSParser




Writer




CSVWriter



Friday, February 6, 2015

Raspberry Pi: Java 1.8


Check existing version

pi@raspberrypi ~ $ java -version
java version "1.7.0_40"





DOWNLOAD:
Linux ARM v6/v7 Hard Float ABI 76.67 MB
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-arm-downloads-2187472.html

COPY OVER TO PI

$ scp ~/Downloads/jdk-8u33-linux-arm-vfp-hflt.gz pi@192.168.1.xxx:

LOG IN TO PI

$ ssh pi@192.168.1.xxx

CHECK THE TRANSFER

pi@raspberrypi ~ $ ls -alt
total 178460
-rw-r----- 1 pi pi 80392759 Feb 6 06:44 jdk-8u33-linux-arm-vfp-hflt.gz




UNPACKAGE

pi@raspberrypi ~ $ sudo tar zxvf jdk-8u33-linux-arm-vfp-hflt.gz -C /opt
jdk1.8.0_33/COPYRIGHT
jdk1.8.0_33/LICENSE
jdk1.8.0_33/README.html
jdk1.8.0_33/THIRDPARTYLICENSEREADME.txt


jdk1.8.0_33/bin/
....






CHECK VERSION

pi@raspberrypi ~ $ /opt/jdk1.8.0_33/bin/java -version
java version "1.8.0_33"
Java(TM) SE Runtime Environment (build 1.8.0_33-b05)
Java HotSpot(TM) Client VM (build 25.33-b05, mixed mode)

Raspberry Pi: WiFi configuration

pi@raspberrypi ~ $ sudo cat /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="XX_wireless_network_name_007"
psk="xx_my_password_xx"
proto=RSN
key_mgmt=WPA-PSK
pairwise=TKIP
auth_alg=OPEN

}

Thursday, February 5, 2015

Beagle Board on Mac






Get newest HoRNDIS: USB tethering driver for Mac OS X
HoRNDIS (pronounce: “horrendous”) is a driver for Mac OS X that allows you to use your Android phone's nativeUSB tethering mode to get Internet access.

http://joshuawise.com/horndis


Serial Driver:
http://beagleboard.org/static/Drivers/MacOSX/FTDI/FTDI_Ser.dmg


FTDIUSBSerialDriver is an implementation of a serial driver for FTDI USB devices on Mac OS X.  It supports FT8U232AM, FT8U245AM, FT232BM, FT245BM, FT2232, FT232R, FT245R, FT2232H, FT4232H, FT232H and FT X Series devices.

http://beagleboard.org/static/Drivers/MacOSX/FTDI/

Cleaning space on Linux board (SD memory)

Check available space - none left

# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       3.5G  3.3G     0 100% /



List files in a suspicious directory by SIZE:

# ls -alS

Remove biggest offenders


# rm -rf /var/cores/*

# rm -rf /var/log/*


Check space available - 89% utilization

# df -hFilesystem     

Size  Used Avail Use% Mounted

on
/dev/root       3.5G  3.0G  370M  89% /

Wednesday, February 4, 2015

Cygwin: git

On Windows in Cygwin shell git pull was hanging up.

Apparently on Cygwin needs to show a pop-up to ASK for password for fetch / pull / push commands, to enable that you need to execute command:


git config --global core.askpass "git-gui--askpass"

Linux: restarting remotely

Immediate restart of the Linux system.


shutdown -rF now

Android: ping server

When trying to connect from Android device to a given server sometimes you need to test if such connection exists:

1: Try from your computer is server is listening on IP_ADDRESS (192.168.x.xx) and given PORT (5xxx)


$ telnet 192.168.x.xx 5xxx

Trying 192.168.x.xx...

Connected to imx6qsabrelite.




2: From command line see if Android can ping the server:


$ adb shell ping 192.168.x.xx

PING 192.168.x.xx (192.168.x.xx) 56(84) bytes of data.

64 bytes from 192.168.x.xx: icmp_seq=1 ttl=64 time=2.65 ms

Tuesday, February 3, 2015

Class Repo

There is a REPO created for each class:


  1. create a free account with xp-dev.com, remember the password
  2. send the create user_name to the instructor ulucas@clcillinois.edu
  3. the instructor will manually add each student per above request
  4. mkdir ~/CLC
  5. cd ~/CLC
  6. git clone https://xp-dev.com/git/CLC_CIT_137_students_2015_spring cit137
  7. cd cit137
  8. mkdir MY_FIRST_LAST_NAME
  9. mv my_old_project_directory/ ~/CLC/cit137/MY_FIRST_LAST_NAME/
  10. cd ~CLC/cit137/MY_FIRST_LAST_NAME/
  11. pwd
  12. ls -al
  13. git add --all
  14. git commit -m "my first commit with files from class #1, #2 and #3"
  15. git push

Homework: print current working directory and list files