Tuesday, 2 July 2013

How to Setup Android Developer Tool and "Hello World" of the Android


Hi Friends,
            Welcome you all in the Android Tutorial. 

Today we will learn "How to Setup the Android Developer Tool (ADT)" and "How to create first android Application".

           

First of all we will setup our development environment for Android.
For that you need to download the ANDROID DEVELOPER TOOL (ADT).

You can use the following link to download the ADT :-
http://developer.android.com/sdk/installing/bundle.html

            This single bundle gives everything you need for developing apps.
1)    Eclipse + ADT plugin
2)    Android SDK Tools
3)    Android Platform-tools
4)    Simulator

Setting up the Developer Tool:-
1)    Unzip the zip file named adt-bunlde-<os-platform>.zip and save it to some location where you want to save.
2)    Move to adt-bundle-<os-platform>/eclipse/ directory and run the eclipse exe.


Create our first Android Project:-

The steps for creating a new Android Application are as follows:-

1)    Go to File à New  and click on Android Application Project.






2)    Write your application name in the Application Name.

a.     Project Name: is the name of your project directory.

b.     Package Name: is the name package namespace.

c.      Minimum Required SDK: is the lowest version of Android that your app support.
d.     Target SDK: is the highest version of Android what your app support.
e.     Compile With: is the platform version against which your app will compile.
f.       Theme specifies: the Android UI style apply for your app.
3)    Click on the next button.
4)    Leave the next screen default and click on the next button.
5)    Next screen is the launcher icon screen. You can customize also. Leave this screen as default , click on the next button.
6)    Next screen belongs to the activity template. For this project select BlankActivity and click next button.
7)    Leave the next screen default and do Finish.
                      
   Now your first android application is ready with some default files.





How to Run on the Emulator:-

            First of all before running our application on the Emulator, we need to create an Android Virtual Device (AVD).
            
1)    Click on the Android Virtual Device Manager.
    The following image shows how AVD manager look like.


2)    Click on New Button. Type AVD name. I have given Galaxy Nexus. Select Device, choose Galaxy Nexus device. We will try to run our application on Galaxy Nexus emulator.








3)    Click on the OK button.

4)    Select your AVD device and click on the start button.

5)    Click on the Launch Button.


6)    You might get some error like the following image.


7)    To fix this error, select your AVD device and click on the edit button, change the ram size to 512.
8)    Start your AVD once again. This will take some long time.
9)    Then unlock your emulator screen.
10)           Click Run Button from the toolbar. 
         This will run your first application on the Emulator.

Your application on the Emulator will look like this.






Porting of the application on an Android Device:-
1)    Plug in your device to the development machine with a USB cable.
2)    Enable USB DEBUGGING on your device.
a.     On device Android 3.2 or older,  you can find this option under Settings à Applications à Development.
b.     On Android 4.0 and newer, it’s in Settings à Developer options.
On Android 4.2 and newer versions, Developer options is hidden by default. To make it available go to Setting à About phone and tap Build number seven times.
Return to previous screen to find the Developer option.

If you are developing on Windows then you might need to install a proper USB drive for you device.
If you are connected with net windows 7 will automatically install the proper driver.
The following link helps to install driver related things.


3)    Open your first application project and click on the RUN menu and select Run Configuration.
4)    Go to Target Tab.


5)    Select Always prompt to pick device and click on the run button
6)    Then choose a running Android device.
7)    Click ok.
Then you will see some messages on console window like Uploading FirstApp.apk on device.
Installing FirstApp.apk
Success!

Check it out your android device; you will be seeing the application running.

Enjoy your First Android Application buddies. 


Do comments, ask questions, and don't forgot to share it with your buddies.






                                

Android Tutorial Index

How to develop a Graphical User Interface ( GUI ) application for an Android



Hello Friends,
Welcome you all in the Android Tutorial.
Today we will learn how we can develop a GUI application for an Android.


            To develop a GUI app for an android is very easy.
            There are two main things we need to think before creating any GUI application.
            1)        User Interface
            2)        Layout
           
            1) User Interface:-
                        What are the interfaces you are providing to the user as a part of your GUI application?
                        These UI (user interface) interacts with user directly.
                        User Interfaces are like buttons, text fields, list, radio-button etc.
                       
            2) Layout:-
                        The second important thing is how we are positioning the UI in our GUI app.
                        Like you want to put all the UIs in a linear format, vertical format or grid format etc.
                       
            In our Fist GUI application, we are going to use two UIs. They are text field and button.
            And layout we are going to use is Linear Layout.
           
                       
            To build a GUI application for an android we use View and ViewGroup Objects.
            Views are like UI widgets. Example button, textbox, radio button etc.
            ViewGroup are invisible containers. This defines how the views are laid out. Example in grid format or in a linear format, or like vertical format.
           
            Let's start with creating a new Project:-
           
            Step 1: Create a New Project
           
                        File --> New --> Android Application Project
                       Give "FirstGUI" as your application name.
                        and Press Finish.
           
Your "FistGUI" application has been created with default “BlankActivity".
                       
            Step 2: Create a Layout
           
                        Open the activity_main.xml file from the layout directory.
                        Replace the Relativelayout with the LinearLayout.
                        Add the "vertical" orientation.
                        Delete the TextView from the activity_main.xml file. We do not need    this in our current project.
                        So the modified activity_main.xml file will be looking like as follows:-


      

           Step 3:  Add User Interface widgets
           
                        Add a TextField. We will use it for “FirstName”.
                       
                        To create a user editable text field we use will use <EditText> element.
                        Add the following code into your activity_main.xml inside the LinearLayout.

 
  
                      Add another TextField. We will use it for “LastName”.

 
                        

            Let’s have a brief info of some contents:-
           
            a)        android:id: This provides a unique identifier for the view, which we can use to reference the object from the code. Example to read and manipulate the object.
           
                        @ sign is required when we are referring to any resource object from XML.
                        + sign before the resource type is needed only when we are defining a resource ID for a first time.
                       
                        When we compile the app, the SDK use the ID name to create a new resource ID in your project's gen/R.java.
                       
            b)        android:layout_width and android:layout_height:
                        We are not specifying any size for the width and height. We are using the "wrap_content".
                        So the textfield size only as big as needed to fit the contents.
                       
            c)         android:hint
                        We can put the default string to display when the text field is empty.
                        We can hardcode the default value or we can use “@string/first_name" value refers to a string resource defined in a separate file.
                       
       
        Step 4: Add the default string
       
            Open the strings.xml file from the /res/values directory
           Add the following string name, so that we can access it from the other files:-           

First Name
Last Name
Send
                       
            Step 5: Add a Button
           
                      Copy the following code int the activity_main.xml. The following code will add a button into your application.

                       
           
           

 Your final activity_main.xml /res/layout will look like as follows:-


    
    
    
    
    
    




           
Your final strings.xml /res/values will look like as follows:-


    FirstGUI
    Hello world!
    Settings
    First Name
     Last Name
    Send




           
Your "FirstGUI" application is ready, run it and enjoy it ...
Your application will look like as follows :---


              
Do not forgot to share !!!

Friday, 21 June 2013

Contact Us



 "Do you have any questions to ask ??" , if yes this is the place where you can ask if you have any queries regarding to any post.


        This is the place where you can ask programming related question of C, C++ and Java.


        I will be very happy if i can help you to resolve your doubts.

    

       You can also suggest the topic on which you want some details. I will try to make a post in a simple and explained manner.

        Like someone has suggested me to write some important things of C++ 11.



Name


Email


Message






Thursday, 6 June 2013

C++ 11 ( some important features ) static_assert and type_traits

Hello friends,
            Welcome you all in the C++ 11 Part5. In the Part4 we have discussed about lambda function.

In this part we will learn about static_assert and type_traits.

static_assert:
                       
            static_assert performs compile time assertion checking. If the assertion is false, the complier shows the specified error message.

syntax: static_assert(constant_expression, error_message)

constant_expression must be a constant expression that can be converted to bool.
Error_message must be a string literal. You can give the error message what you want to display.


Let’s see how we can use static_assert in our c++ code.
//author: shobhit upadhyaya

template < class T, size_t length >
class String
{
   static_assert(length < 100, "length is too big");
   T str_data[length];
};

int main()
{
   String< int, 101 > a1;
   String< int, 90 > a2;
   return 0;
}

The above code gives you the following error at compile time, because in the static_assert we have a condition string length should be less than 100. But we are trying to give a string length greater than 100.

error C2338: length is too big
  : see reference to class template instantiation 'String<int,101>' being compiled


Static_assert becomes more powerful when used together with the type_traits.
<type_traits> header files provide various classes that give information about types at compile time.

<type_traits> header is categorized into three classes:
            Helper classes: for creating compile time constants.
            Type traits: to get type information at compile time.
            Type transformation: for getting new types by applying specific transformations to existing types.


Let’s see the example how type_traits can help us:-

//author: shobhit upadhyaya

#include 
#include 
using namespace std;

template < class T1, class T2 >
auto mul(T1 a, T2 b) -> decltype(a * b) 
{
  static_assert(is_integral::value,"T1, please input integral value");
  static_assert(is_integral::value,"T2, please input integral value");

   return a * b;
};

int main()
{
   mul(1,3.14);
   mul(1,5);    
   return 0;
}
//


The above code gives the following error at compile time. Because in the static_assert we are using a type_trait is_integral. is_integral checks the type and return false if it is not an integral type. But in our code we are trying to pass a floating point value.

: error C2338: T2, please input integral value
 : see reference to function template instantiation 'T2 mul<int,double>(T1,T2)' being compiled
        with
        [
            T2=double
,            T1=int
        ]


Following are some example of type_traits:-
1.     is_array :-
This check type is an array or not. If not returns false.
2.     is_class:-
This check type is a class or not. If not returns false.
3.     is_enum:-
This check type is an enum or not. If not returns false.
4.     is_floating_point:-
This checks type is a floating point or not. If not returns false.
5.     is_function:-
This checks type is a function or not. If not returns false.
6.     is_integral:-
This checks type is an integral or not. If not returns false.
7.     is_lvalue_reference:-
This checks type is lvalue_reference or not. If not returns false.
8.     is_member_function_pointer:-
This checks type is a pointer to member function or not. If not returns false.
9.     is_member_object_pointer:-
This checks type is a pointer to member object or not. If not returns false.
10.  is_pointer:-
This checks type is a pointer or not. If not returns false.
11.  is_rvalue_reference:-
This checks type is an rvalue reference or not. If not returns false.
12.  is_union:-
This checks type is a union or not. If not returns false.
13.  is_void:-
This checks type is a void or not. If not returns false.

            There are so many others type_traits that I have not covered here. With the help of all there type_traits we can improve the static_assert.






Feel free to give your valuable comments and feedback.
Do not forgot to share J

Tuesday, 4 June 2013

C++ 11 ( some important features ) Part4

Hello friends,
            Welcome you all in the C++ 11 Part4. In the Part3 we have discussed about range-based for statement and override virtual methods.

In this part we will learn about lambda functions.


Lambda function:-
            Anonymous functions are known as lambda function.     
Syntax:
            [capture] (parameters) -> return-type {body}

          body:                     This represent the function body.
return-type:             This can be omitted. If not present it's implied by the function return statements ( or void if it doesn't return any value) 
          parameters:             List of parameters as like named functions.
           

          Let’s have a look of the following code that demonstrates lambda function.
//author: shobhit upadhyaya
#include 
using namespace std;
int main()
{

int ret= [] () -> int {return 5; } ();
printf("ret = %d\n",ret);

// we can omit the return-type -> int, it is implied by the function return statement
int ret1= [] () {return 5;} ();
printf("ret1 = %d\n",ret1);

//parameter demonstration. 
int ret2= [] (int input) -> int { return (input * input * 5 ); } (5);
printf("ret2 = %d\n",ret2);

   return 0;
}
//

A lambda function can refer to identifiers declared outside the lambda function. The set of these variables is commonly called as closure. Closures are defined between square brackets [ and ] in the declaration of lambda expression. The mechanism allows these variables to be captured by value or by reference.

         capture: 
              Let's understand how to use the external variables inside the lambda function.

[]:        // no variables defined. We cannot use any external variables is the lambda function.
[ a, &b]:                     a is captured by value, b is captured by reference.
[&]:     any external variable is implicitly captured by reference if used.
[=]:     any external variable is implicitly captured by value if used.
[&, a]:            a is explicitly captured by value. Other variables will be captured by reference
[=, &b]:         b is explicitly captured by reference. Other variables will be captured by value

Following code gives the demonstration about how to use captures :-


//author: shobhit upadhyaya
#include 
#include 
using namespace std;

int main ()
{

 vector my_list (1,5);                       // five ints with value 1
 int sum = 0;
 
 // demonstration of captures
 /*
    lambda function:
      [&sum] (int x) { sum += x; }
      
      here we captured variable "sum" by reference ,so we can change the value of variable also.
 */
 printf("before, sum = %d\n",sum);
 
 for_each(begin(my_list), end(my_list), [&sum](int x) {
  sum += x;
});
 
  printf("after, sum = %d\n",sum);
  return 0;
}
//



Following code gives the demonstration of how we use STL without and with lambda function:-
//author: shobhit upadhyaya
#include 
#include 
#include 

using namespace std;

bool compare(int i,int j){return (i > j);}

using namespace std;
int main()
{
	int n = 10;

	vector v(n);
	//initialize the vector with values from 0 to 9
	for(int i=0; i < n; i++)
        {
          v[i] = i;
        }
	
	for(int i = 0; i < n; i++)
           printf("v[%d] == %d\n",i,v[i]);

	printf("\n");

	//sort the vector, with the help of compare function
	sort(v.begin(), v.end(), compare); // C++98 style
	
        for(int i = 0; i < n; i++)
	    printf("v[%d] == %d\n",i,v[i]);

	printf("\n");

        //sort the vector, with the help of lambda function
        sort(v.begin(),v.end(),[](int i, int j) -> bool{ return (i < j);}); //C++11 style
   
  
	for(int i = 0; i < n; i++)
           printf("v[%d] == %d\n",i,v[i]);

        printf("\n");

	return 0;
}


//





Give your comments and feedback.

Do not forgot to share J