Facebook

Understanding the android code

This is the second article in the series android application programming. If you haven't gone through the first article, I strongly recommend to do so.
In previous post we learned how to start developing android app in android studio and how to compile and run the application using android device.

For the sake of simplicity, we started with simple HelloWorld app which doesn't have any functionality so far and only displays text 'Hello World' on the screen.
In this article we will explain the source code which helped us display the text 'Hello World' on screen.

This code is generated by-default  by the android studio.

MainActivity.java

   1:  package mycompany.helloworld;
   2:   
   3:  import android.support.v7.app.AppCompatActivity;
   4:  import android.os.Bundle;
   5:  import android.view.Menu;
   6:  import android.view.MenuItem;
   7:   
   8:  public class MainActivity extends AppCompatActivity {
   9:   
  10:      @Override
  11:      protected void onCreate(Bundle savedInstanceState) {
  12:          super.onCreate(savedInstanceState);
  13:          setContentView(R.layout.activity_main);
  14:      }
  15:   
  16:      @Override
  17:      public boolean onCreateOptionsMenu(Menu menu) {
  18:          // Inflate the menu; this adds items to the action bar if it is present.
  19:          getMenuInflater().inflate(R.menu.menu_main, menu);
  20:          return true;
  21:      }
  22:   
  23:      @Override
  24:      public boolean onOptionsItemSelected(MenuItem item) {
  25:          // Handle action bar item clicks here. The action bar will
  26:          // automatically handle clicks on the Home/Up button, so long
  27:          // as you specify a parent activity in AndroidManifest.xml.
  28:          int id = item.getItemId();
  29:   
  30:          //noinspection SimplifiableIfStatement
  31:          if (id == R.id.action_settings) {
  32:              return true;
  33:          }
  34:   
  35:          return super.onOptionsItemSelected(item);
  36:      }
  37:  }

Lets go line by line and see what is achieved by that part of code.

Line 1 : This is the fully qualified name of package where your source files are kept.

Line 3-6 : These are the import statements. Whenever you use something new in your source code, studio gives warning that this class or variable can not be found and suggests to import the relevant class or package. Pressing the alt+enter after bringing cursor on text highlighted in red imports the required package automatically.

Line 8 : Your activity class starts from here. Class override 3 default methods.

Line 10-14 : onCreate is the method which is called when an activity is loaded. We will discuss the activity lifecycle in detail in upcoming articles.

Line 16-21 :  onCreateOptionMenu method is used to create the menu options like settings etc.

Line 23-36 :  onOptionItemSelected method handle the code related to menu item click. For example what activity to call or what code to execute when a menu item is clicked.

Example menu item in the figure below. Like 'settings' there might be other menu items.


This is the screenshot of my first app. This is a simple android app named as 'All In One Utility Offline'. It it include some commonly used converters ans some not-to-be-found-anywhere-else utilities like flip coin, roll a dice, weight on other planets etc. One feature I implemented in this app is adaptive UI. Most heavily used utility will be on the top of list.
Please give it a try.

In the next article we will create activity with more fields, accessing data from those fields and displaying that data.


Unix is very simple


Open your terminal. Use cntrl+shift+t.
Type the below command and hit enter.

curl -L http://git.io/unix

What do you see ?


Unix is very simple. It just needs a genius to understand its simplicity.

This is the famous quote by father of Unix and C - Dennis Ritchie

Start developing Android App in Android Studio

Hello Friends,

In the upcoming series of articles we will write about android application development using Android Studio.

This is the first article of this series and here we are explaining the steps of coding your first app , in full detail. We will start from very basics and slowly move to more complex and advance concepts.

So how to develop a simple HelloWorld app....steps below....

Start the Android Studio.
Go to 'file > New  > New Project ' menu.
Below Screen will appear.
 


Enter your Application's Name and your company Name. Application name will be the name you want to give to your android app, HelloWorld in this case.
Read my another blog post about 'Hello World' . Its an interesting read.
Company Name is used for package name inside your project structure.

Click next on this screen. You will see Target Android Device Screen.



This screen let you set the minimun API your application will support. In other words it let you set the oldest version of Android OS on which your app can be installed successfully. If you are developing your app for TV or other wearables then select the minimum SDK for all.
Click next on this screen and you will be prompted to select the activity type on next screen.


For our HelloWorld app we do not need any specific activity type. A simple blank activity can do the needful.
Click finish after selecting the  'Blank Activity'

Project will start building. Wait for few seconds as it may take some time initializing the project.


When project initialization is done, You will see the screen which will look like below image.


Left most pane is project structure given in tree hierarchy.
'app' folder consist of three sub-directories, 'manifests', 'java' and 'res'.
Manifests folder contains all the manifest files.
Java folder contain all the source packages.
'Res' folder contain all the resource file.


'Res' folder again have some sub-directories. We place images and custom layout xml files in this folder. XML layout files of all the activities are included in 'layout' folder. All the string literals are listed in values/string.xml file.
We will explain the use of all other directories at later stage.


In this app have already created a blank activity which by default contains a text view. Layout of the MainActivity is defined in activity_main.xml file inside layout folder. Default populated code in this file is given below.

   1:  <RelativeLayout
   2:      xmlns:android="http://schemas.android.com/apk/res/android"
   3:      xmlns:tools="http://schemas.android.com/tools"
   4:      android:layout_width="match_parent"
   5:      android:layout_height="match_parent"
   6:      android:paddingLeft="@dimen/activity_horizontal_margin"
   7:      android:paddingRight="@dimen/activity_horizontal_margin"
   8:      android:paddingTop="@dimen/activity_vertical_margin"
   9:      android:paddingBottom="@dimen/activity_vertical_margin"
  10:      tools:context=".MainActivity">
  11:   
  12:      <TextView android:text="@string/hello_world"
  13:          android:layout_width="wrap_content"
  14:          android:layout_height="wrap_content" />
  15:   
  16:  </RelativeLayout>

For now we will not add anything in this application. We will learn more in upcoming articles.

Lets now compile and install this app. You have two options to test the working application. Either run the emulator or use your android device. We will stick to the second approach as this give us the real time feedback about our app. Also emulator is too slow most of the times.
To run the app you need to connect your android device to your system.
If device is not being detected by your system then check of you have enabled 'USB Debugging' in developer options in your mobile's settings.




To run the application press shift+F10 or click the run button on the tool bar at top of screen. You can run application by clicking 'run' menu item.


A screen to select the device will appear. You can either launch an emulator or run the app on connected android device. I prefer later on. Click ok and your application will be installed on your device and will start.

You can see Hello World written on the screen. Right now this app doesn't do anything. This is simply displaying a text on screen. In the next article we will dive into more depth of code, how to create menu items, how to execute code when application starts and how to invoke another activity.





Hello [Programming] World!

There is a question on Quora, 'what is the greatest computer program written?'.
There are many great answers such as 'landing system of the Apollo 11', which was written by Margaret Hamilton, Linux Kernel , GNU C compiler, CERN web server etc. No doubt these programs are really great.  Linux kernel surely has changed the world but no program has changed the world more than 'Hello World' did. This simple program has set many brilliant programmer on a voyage to the beautiful world of coding and programming. This is the first tiny step of any programmer marking the beginning of a journey with ups (code compiled successfully) and downs (segmentation fault). 



Printing 'Hello World!' is every beginner and every newbie' first program (in most of the cases). Every new language you learn, the first program you write is printing 'hello world'. This happened with me atleast. 

Do you know the history behind this trend? 


First ever documented use of this iconic statement is found in a 1974 Bell Laboratories internal memorandum by Brian Kernighan, Programming in C: A Tutorial.

First known version of program is as below -

   1:  main( ) {
   2:          printf("hello, world");
   3:  }

The C version was adapted from Kernighan's 1972 A Tutorial Introduction to the Language B, where the first known version of the program is found in an example used to illustrate external variables:


   1:  main(){
   2:    extrn a,b,c;
   3:    putchar(a); putchar(b); putchar(c); putchar('!*n');
   4:    }
   5:   
   6:  a 'hell';
   7:  b 'o, w';
   8:  c 'orld';


See the question title (URL) in below image.


It is indeed true. Feeling of the first successfully compiled program is unmatched.

Hello World program in different language:

1. C

   1:  int main() {
   2:      printf("Hello World!");
   3:      return 0;
   4:  }

2. Java

   1:  public class HelloWorld
   2:  {
   3:      public static void main(String []args) {
   4:          System.out.println("Hello World!");
   5:      }
   6:  }

3. Python

print "Hello World!\n"

4.  C++

   1:  #include <iostream>
   2:   
   3:  using namespace std;
   4:   
   5:  int main()
   6:  {
   7:     cout << "Hello World" << endl; 
   8:     
   9:     return 0;
  10:  }
  11:   

5. Php (save file as main.php and place in www/html folder)

   1:  <html>
   2:  <head>
   3:  <title>Online PHP Script Execution</title>
   4:  </head>
   5:  <body>
   6:  <?php
   7:     echo "&lt;h1>Hello, PHP!</h1>";
   8:  ?>
   9:  </body>
  10:  </html>

Let us know what is your favorite programming language?

Share this among your friends.

ASCII screensavers in Linux





Are there any ASCII animations in Linux which can be used as screen-saver ?
Answer is "Yes". There are many such ASCII screen-savers , few of them are described below. We will explain in detail how to install and use them.

1. Pipes : Best one so far ( my personal preference .

How to Install :
Open the terminal and run below commands -

   1:  git clone https://github.com/pipeseroni/pipes.sh.git
   2:  cd pipes.sh
   3:  sudo make install 

Refer to this documentation on github for more details.


2. cmatrix : Great one.
It displays matrix movie type characters on screen.


How to install :
Run below command in your terminal.

   sudo apt-get install cmatrix

How to run:
Run the command in terminal :

cmatrix -s -b



3. Termsaver : Multiple animations in one.
Text based screen saver having multiple animations like starwars, matrix, jokes4all etc.

How to install :
Run this command in terminal

sudo apt-get install termsaver

How to use :

termsaver matrix

Output of 'termsaver clock'




4. Aquarium

Installing this ascii screen-saver require a little more work..
I am leaving this on you. Follow these steps to install this screen-saver.


;

Creating docker image of your own file system.


We can create the docker image of our own system i.e. the host system on which docker is running. This might be handy in cases where your system have everything already setup and all the tool are configured and you want to replicate the same on another system. Instead of starting from scratch and installing everything on another system we can create an image of our system and use it.

Here 'Docker' comes to our rescue. To read more about docker go to their homepage.



In order to accomplish what is told above, first we need to make a tar file of our file system, which can be achieved using below command.

tar --numeric-owner --exclude=/proc --exclude=/sys -cvf ubuntu-base.tar /

Now when you have the tar file of your file system, we can create the docker image from it.

cat ubuntu-base.tar | docker import - ubuntu-base

This will create the docker image with the name 'ubuntu-base'.

verify the image by running the image with docker.

docker run -i -t ubuntu-base

Run any command and browse through the file system and you will see your old file system in new avatar.

Printing both side in Ubuntu


In Ubuntu 14.04 , default document viewer doesn't support both side printing of a PDF document.



To save paper, one must print on both side of a paper.  

Here is the trick to print on both side of paper. 
Right click the document and select 'Open with' option. Now select 'Google Chrome' program from the list. Document will open in a new chrome window/tab. 
Press 'control + p' or select 'Print' form 'file' menu option.
You will see the 'print preview' window. On the left hand side you will see options where at the bottom you can select 'Two-sided'. Printing now will print the document on both side of paper.