Using Android preferences for storage

Android provides a few ways to persist information between sessions, with the simplest option being the preferences system. The preferences system allows you to save key value pairs with primitive data types as values.

To use preferences you need to import SharedPeferences and then use getSharedPreferences() within your activity to get the preferences object:

1
2
3
4
import android.content.SharedPreferences;

// Somewhere in your code
SharedPreferences settings = getSharedPreferences("SomeKey", MODE_PRIVATE);
Read More

Different settings for different language in vim

Recently I have been mostly working in JavaScript and per my project standards, all my tabs are replaced by 2 spaces. The problem with this is that for other projects in other programming languages the standard tab width is 4 spaces, so it becomes annoying to have to hit tab twice to indent a line correctly. To fix this you can declare settings specific for a language if you place them on ~/.vim/ftplugin/LANGUAGE.vim.

Since currently I am working on an Android app and I want a tab width of 4, I created the file ~/.vim/ftplugin/java.vim and added this content:

1
2
3
" Make tabs 4 spaces wide "
set tabstop=4
set shiftwidth=4
Read More

Git rebasing vs merging

When I switched to git for the first time I had a very hard time understanding what rebasing did to a point where I totally avoided it. I had been doing all my work on private and public repositories by going to master and merging to my feature branch or to a team member’s branch. I was happy with this way of working until I started on my new job a few months ago and they had a rebase based workflow. It goes something like this:

1
2
3
4
5
6
cd repo
git pull --rebase
// Do some work and add stage it
git commit -m 'Some message'
git pull --rebase
git push origin master

In this little example I am working in my local master branch, but if I wanted I could create a feature branch and rebase it to master as needed. The most obvious benefit about this workflow is a very clean history. Lets look at a simple example of a merge based history:

Read More

Changing your default name and e-mail on git

If you just installed git in your computer you probably got a message telling you that your name and e-mail haven’t been configured and suggesting you to change them the first time you commit:

1
2
3
git config --global user.name "First Last"
git config --global user.email [email protected]
git commit --amend --reset-author

This will set “First Last” and [email protected] as your default name and email for any git repository you work on in that computer. This may be something you don’t want because probably you want to have different identities in different computers. One thing you can do is amend your commits with an alternate identity you want to use:

1
git commit --amend --author="First Last <[email protected]>"

If you will constantly be working on this repository this will become annoying really fast, so instead you can set your identity for a specific repository by adding this to your .git/config file:

1
2
3
[user]
  name = First Last
  email = [email protected]
Read More

Some things I learned about drawing on Android

I have been playing with 2D graphics in Android and it was a little hard to understand how to do some things that I needed. The API documentation for some functions doesn’t really do much of explaining you exactly what each argument does and how you can use them. After a little research I was able to draw everything I needed so I am going to try to summarize what I learned.

Paint

Most of the drawing functions on Android will take a Paint object as an argument. To use it you just need to instantiate the Paint class:

1
Paint p = new Paint();

You can specify one of three drawing styles: FILL, FILL_AND_STROKE, STROKE. In a very simple scenario you might want to fill a shape using a blue color, and you could do it like this:

1
2
3
Paint p = new Paint();
p.setStyle(Paint.Style.FILL);
p.setColor(Color.BLUE);
Read More

Drawing on an android view

I am building a simple app that will need to graph some data, so I found myself in the need of using Android’s drawing library. There are a few good tutorials out there of how to do this using a canvas, but I read somewhere that it was possible to draw directly into a view and I wanted to try that. Drawing directly into the view is useful when you have data that you don’t need to redraw very often. When you use a canvas you have to manually call the onDraw method every time you want to show something, which makes sense for animations. For more static data you can draw directly into the view and the onDraw method will be called automatically every time the view it is shown to the user.

Read More

Debugging node apps using built in debugger

When it comes to debugging node applications you have a few options. Most people I know use their IDE’s debugger which does the work really well for them. The next option for people who don’t use IDE’s is usually node inspector, which does a pretty good job too. Not long ago I found out that Node comes with a command line built in debugger which I tried and I think is easy enough that may become my debugger of choice.

To start debugging an application just use the debug command before running it:

1
node debug app.js

When you do this your script will be loaded but the execution will stop at the first line. You will also get a debug> prompt where you can issue debugger commands.

Read More

Debugging Android applications

Recently I have been exploring Android development and it was just a matter of time to find myself in the need to debug some of the code I write. Android documentation talks a little about debugging using Eclipse, but I try to stay away from bloated IDEs, so I have to pay by not getting the sugar they offer. The alternatives are not as easy and pretty as using an IDE with a compatible debugger, but hopefully they will do the job.

Logging

I come from PHP and JavaScript, so logging or dumping stuff is something that I am used to doing. When I started using JavaScript and I wanted to know what my code was doing or the value of a variable in a specific point in time I used console.log to print it to the browser terminal. Using this approach is really easy and very helpful in a lot of scenarios.

Read More

Twitter Bootstrap

Twitter bootstrap is a front end framework that helps rapidly develop responsive web apps. Everybody says it is awesome, so I thought it was time to explore what it does.

I started by downloading it from Twitter Bootstrap Website. Twitter Bootstrap depends on jQuery and requires an HTML5 doctype so make sure your page has both.

Read More

Android Menus

There are three different types of menus that can be added to an Android app:

Action bar – For older devices this is the menu that you get when you click on the menu button on your device. Since most new devices don’t have this button anymore the recommended way to create this type of global menus is by using the action bar which is visible by default and located on the top of the screen.

Contextual action mode – This menu allows you to perform actions on selected elements. The contextual action bar can be found in the same place where the action bar was, but it only appears when elements are selected and actions can be performed against them.

Popup menu – This is a modal popup menu that should be used to perform actions related to the current view.

Read More