Inspect the view hierarchy (layout) of any Android app

Ever wondering how a particular app structure their hierarchy of views? With the latest Android SDK(I’m using 21.01 currently) and Eclipse ADT plugin, you can finally do that!

1. Connect your phone, Open the app you are curious about. Switch to the DDMS perspective, See the little button over there? click on it.insepct_view_hierarchy_1

2. The following screen will appear, you can now browse the view hierarchy just like a DOM tree which Web developers are familiar with.

insepct_view_hierarchy_2

What is does is basically dump a snapshot of the view in the current screen to the IDE. There’s quite a lot of information you can see in the “Node Detail” section, but you probably cannot reconstruct a layout XML in your own code by these info because It doesn’t show you some attributes like layout_width, layout_height, layout_weight etc. But it definitely tells you what types of views you need to build a particular layout.

Android: Can’t establish a reliable data connection to the server

Few days ago I saw this strange error message when I try to login my Google account on the HTC Desire (my previous phone):

Can’t establish a reliable data connection to the server

Since I cannot login to google, I cannot use the Play Store, Gmail, Youtube etc. My phone was connected to the home WiFi and the internet is definitely working.

I was thinking maybe this phone is already old and maybe Google changed something in their Authentication API. When I was about to give up after trying for about 10 times, I realize something Continue reading

Install MongoDB PHP driver in CentOS 6.x

To use MongoDB in PHP, you are required to install a driver extension to the PHP environment. In CentOS, it’s in fact very straight forward.


Update 2015-March:

This tutorial works for CentOS 6.x, you can also do this by the following command,

You might first need the Remi Repo if you have not, tutorial on how to install that here.

If you use CentOS 7, you can also install by the above command without installing additional repo.


Original Post (Still works, and you will guarantee using the latest version of the mongodb driver):

Prepare the tools to build PHP extensions

This step is extremely easy with yum, but if you want to use the latest version of PHP, this article does not cover that.

Get the latest MongoDB PHP driver source

The source code is hosted in GitHub, you can find it here .

 Build the driver

The following steps are also mentioned in the readme of mongodb php driver

If all went well, the mongo.so should be installed in /usr/lib64/php/modules/
now go to /etc/php.d

Restart Apache to make the change effective

 

Why Learning Android could improve your programming skills

Android is probably the most popular mobile operating system, a lot of programmers join the Android club to be an Android developer and turn their ideas into great apps. While some of you may not be interested in making a mobile app, but I would say you should give it a try. In this article I’ll try to convince you that you can improve your programming skills by studying the Android framework.

It’s simple

By simple, I don’t mean programming in Android is easy. The Google Android team has made it very easy for beginners to get started, just fire up the Eclipse IDE and load the examples and you have your first Android app. Through Android you’ll learn how to organize an app that is so simple to understand, write and deploy. You could apply this in organizing your web application. There’s a lot of good engineering practice in the Android framework. Of course, programming is never easy, if you feel that what you’re doing is easy, you maybe not making something useful!

It’s well documented

Learning Android is a joy. Continue reading

Mirroring Github repos on your own server

We have already discuss why Git is so useful in managing your source code. Now you may also want to use git to check out some interesting projects from other gurus on open source code sharing platforms, e.g. GitHub. Sometimes you may want to mirror the repo on your own server so you and your team can access it from there instead.

A local copy of the foreign repo that is always up-to-date

For example somehow you and your team need to pull the Facebook Android SDK into their workspace, instead of everyone pulling it from Github, you can clone the repo into a local server, and everyone just pull from the local server. This technique can reduce redundant network traffic and is particularly useful if your team don’t have a fast and reliable internet connection, Continue reading

Patterns for running Asynchronous code in Android (Part 1)

An important technique in Android Programming is to make potentially long task run asynchronously in your app, avoid blocking the UI thread, so your app feels more responsive. For example, downloading a file from the Internet, loading a media file from the external storage or applying a filter on an image bitmap etc. I will show you how to implement with AsyncTask, simple java threads and java Executor in Part 1.

Let’s say I want to load a bitmap from the internet, and set it to an ImageView , this might be what you’d write at first:

The Java way: simple Threads

You can implement it just like any other Java programs, define a Thread that does the job, and run it. Every Java programmer knows how to do that. Remember that if you want to modify the UI, you need to use runOnUiThread()

Continue reading

[Android] Using LruCache to cache Bitmaps and limit memory usage

Bitmaps are very bulk objects, loading them from the Internet or local storage could be expensive and “choke” the UI. You want to  keep them in memory so that they could be loaded instantly, however you also want to discard them as soon as they are not useful. Here comes the LruCache.

What is LruCache?

LruCache is a util introduced in API 12, which is an implementaion of the Least Recently Used cache. In short, that is a caching strategy which the “oldest” entries are discarded when overflow occur. It is very useful to cache Bitmaps. The default implementation allows you to limits the number of items in the cache. Here’s how to slightly modify the LruCache to limit the memory usage of the cache.

How to use it?

Well, actually it act just like a HashMap. Continue reading

What is Git and how to use Git in your own project?

What is Git?

In short, Git is a free and open-source distributed version control system. If you have used CVS/SVN, they are centralized repositories, in contrast, Git is the “distributed” version. It was created by Linus Torvalds himself as a tool to manage the Kernel. He has a very nice Google Talk about Git, which I recommend you to check it out first. If you have had some experience using SVN, you can easily understand. I’m going to include some summary of the video in this article.

Why Git?

  1. It is distributed, and it is the only right way to do version control (as Linus say so). Well, actually it means every one owns a full copy of the repository and no single repo is more important than the others. Collaboration happens by pulling and pushing code between repos and branches.
  2. Branching is inherent property of development. Continue reading

Protect your server from SSH attack

Today I have found that there is an IP in Turkey keep trying to login to my server, and I’ve found that my current settings has no protection against this brute-force login, so I did a Google search on this and would like to share with you guys.

To check if your server is currently being attacked via ssh, use this command

> tcpdump port ssh

sshd config

In CentOS, the sshd config is located at /etc/ssh/sshd_config, I have uncommented the following lines:

you can also limit root access, allow only certain IP to access the ssh etc, however I’m using dynamic IP ISP and I’m used to root ssh access, so I only use these settings.

IPTables protection

Relying only on sshd_config is not enough, the attack host still keep sending login requests and could possibly paralyse the network traffic of linux box. I need to find a way to Continue reading