Saturday, April 30, 2011

Android Tabbed Layout with single activity.

In Android, the content for each tab in a Tab Layout can be implemented in one of two ways:
1. Use tabs to switch views within the same activity.
2. Use tabs to switch between different activities.
The tutorial in the Android documentation shows how to do it the 2nd way i.e use tabs to switch between activities but I couldnt find a reasonable example of the first case so I have created one and the contents for layout .xml file and the source .java files are given below.







Layout file
 <?xml version="1.0" encoding="utf-8"?>  
 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
      android:id="@android:id/tabhost"   
      android:layout_width="fill_parent"  
      android:layout_height="fill_parent">  
      <LinearLayout android:orientation="vertical"  
           android:layout_width="fill_parent"   
           android:layout_height="fill_parent"  
           android:padding="5dp">  
           <TabWidget android:id="@android:id/tabs"  
                android:layout_width="fill_parent"   
                android:layout_height="wrap_content"   
                />  
           <FrameLayout android:id="@android:id/tabcontent"  
                android:layout_width="fill_parent" android:layout_height="fill_parent"  
                android:padding="5dp">  
                <LinearLayout android:id="@+id/layoutTab1"  
                     android:orientation="vertical"   
                     android:layout_width="fill_parent"  
                     android:layout_height="fill_parent">  
                     <TextView android:layout_width="fill_parent"  
                          android:layout_height="wrap_content"   
                          android:text="this is tab1. you can add whatever controls you want inside this Linear layout." />  
                </LinearLayout>  
                <LinearLayout android:id="@+id/layoutTab2"  
                     android:orientation="vertical"   
                     android:layout_width="fill_parent"  
                     android:layout_height="fill_parent">  
                     <TextView android:layout_width="fill_parent"  
                          android:layout_height="wrap_content"   
                          android:text="this is tab2. you can add whatever controls you want inside this Linear layout." />  
                </LinearLayout>  
           </FrameLayout>  
      </LinearLayout>  
 </TabHost>  

Java Source File
 package com.example;  
 import android.app.TabActivity;  
 import android.content.res.Resources;  
 import android.os.Bundle;  
 import android.widget.TabHost;  
 public class ExampleTabLayout extends TabActivity {  
      @Override  
      public void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.main);  
           res = getResources();  
           TabHost tabHost = getTabHost();  
           TabHost.TabSpec spec;  
           spec = tabHost.newTabSpec("status").setIndicator("Status").setContent(R.id.layoutTab1);  
           tabHost.addTab(spec);  
           spec = tabHost.newTabSpec("actionitems").setIndicator("Action Items").setContent(R.id.layoutTab2);  
           tabHost.addTab(spec);  
           tabHost.setCurrentTab(0);  
      }  
      protected Resources res;  
 }  

Tuesday, March 29, 2011

Install Firefox 4 in Ubuntu

Firefox 4 is available for Ubuntu 10.04 and 10.10. To install it use the following commands

sudo add-apt-repository ppa:mozillateam/firefox-stable
sudo apt-get update
sudo apt-get upgrade

This will update your Firefox to version 4. Note that this will not install Firefox 4 side by side with your previous version as used to be the case with beta versions. As this installs the stable release version of Firefox so your previous version will be removed and after installation you will only have Firefox 4.0.

Tuesday, March 1, 2011

git bare repos

A normal git repository is a directory that contains:
  1. Our files: Files and directories that we have created and which contain our data. These files and directories are collectively known as working tree.
  2. .git directory: This folder contains all of the Git's control files.
A bare git repository does not have working tree and it also does not have .git directory either. All of the Git's control and management files are present in the directory directly. You can not checkout from a bare repository or commit to it.

So what are they useful for when you cant checkout or commit to them?

Bare Git repos are good for pushing and pulling only and they are designed this way so that they can be used as common shared repo by a group of people. So any one want to make changes makes a clone of that bare repo, makes changes in that cloned repo, commit them there and then push the changes to bare repo. Now any one wanting those changes can pull them from the bare repo.

To create and initialize a new bare git repo you can use the command:
git --bare init
and to clone an existing repo as a bare repo use:
git clone --bare

Thursday, February 24, 2011

5 useful Grep options that you must know

Grep is a very handy utility for searching for a pattern in files. While it has many options, every grep user should at-least know the following five options


-R, -r, --recursive

This option makes grep search all the files in the specified directory and its sub-directories, recursively. This specifically comes in handy for programmers when they are looking for the declaration or all uses of some variable/function/anything in the source tree.
So, for example if you want to search the current directory for all files containing the word "foo" then you can use:
grep -r foo .


-n, --line-number

This option makes grep to prefix the ouput with the line number of each matched line withing its input file. This makes it easier to make changes as you know the line number, so just open the file, go to the specific line and make the required changes.



-I

This option makes grep ignore binary files i.e binary files are not searched. This option is again very helpful for programmers who only want to search in the source files.



-A NUM, --after-context=NUM

This option prints NUM lines following the matched line from the input file. For example if you want the output to contain the matched line and 2 lines after it then you can use:
grep -A2 <PATTERN> <FILES>


-B NUM, --before-context=NUM

This option prints NUM lines before the matched line from the input file. So if you want the output to contain the matched line and 2 lines before it then you use
grep -B2 <PATTERN> <FILES>


Thursday, February 10, 2011

OpenSUSE: turn firewall off

To disable the firewall on OpenSUSE use the following command as root user:
/sbin/SuSEfirewall2 off
To turn it back on use:
/sbin/SuSEfirewall2 on

Sunday, January 30, 2011

Remove envelope icon from Indicator Applet

The envelope icon in Indicator Applet really annoys me. It unnecessarily increases the number of clicks needed to open an application like Empathy or Pidgin. To remove that icon uninstall the indicator-messages package using following command:
sudo apt-get remove indicator-messages
This makes that envelope icon disappear and applications start appearing in Notification Area.

Wednesday, January 19, 2011

Limit Internet Bandwidth Usage (internet speed) in Ubuntu

There are many reasons one would want to limit internet bandwidth/speed on a system e.g you dont want your siblings to choke your internet or, if you are a network admin, you dont want one user, downloading a big movie over torrent, to hurt every one else's surfing.

To limit internet bandwidth on a Ubuntu machine install wondershaper. For this open a terminal and type following command:
sudo apt-get install wondershaper
Now use ifconfig command to find out the name of interface connected to internet. The name will be something like eth0, eth1, wlan0 or something similar. In my case it is eth0


Now issue a command similar to follwoing (relpacing eth0 with your interface name):
sudo wondershaper eth0 1024 256
this will limit the download speed to 1024 Kbps and upload to 256 Kbps. Remember that this limit is for all internet traffic i.e torrent, ftp, browsing and any thing else.

If you want to remove these limits use
sudo wondershaper clear eth0
again replacing eth0 with your interface name