Thursday, January 10, 2013

4. Basics of Files,Folders and how to Run the Project in Android

Now your screen will be looking like the Image below.



 In the Package Explorer there is a Main Folder named "sample", ie. your project name. Click on the folder to expand it. Now you can see the following folders and some other folders which are not relevant to us.
"src" folder is the source folder (that is why the name),
"gen" folder is the Generated folder (Thats why the name)
"res" folder is the resources folder (That is why the name).


Now click on the following folders step by step to expand it. (I have given > means click on the folder)
src > com.example.sample > MainActivity.Java

"src" contains the source files of your project. The "MainActivity.Java" is the Main java file of your project. You may double click to open it. You should know Java to program it. For this project we are not doing anything in it so close it.

"gen" folder contains an automatically generated  Java files. Don't modify any files here because your application will not work.

Now click on the following file to expand it

res >values > strings.xml & styles.xml
The values of strings,colors used etc are given in the strings.xml file.

Now click on the following file to expand it
res> layout >activity_main.xml

 It contains the Main Layout files/XML files for your project. Now double click on the activity_main.xml  now you can see activity_main.xml file as in the image. There are two tabs in the bottom, Graphical Layout and activity_main.xml. You can modify the file in both the modes. You may drag and drop the form widgets in the Graphical Layout to create Button,Textview etc. When you do so the corresponding XML code will be automatically generated in the activity_main.xml. If you drag and drop you can give the parameters in the left side box.It is the easiest method for this project,however knowing xml is essential, but if you want to do it through Graphical method go to the Heading "Using Graphical Method" below)
 
   For our project we are modifying the xml file. Click on the activity_main.xml tab. Now you can see the xml codes. (see the image below)



There is a <textview given and below that there is a android:text="@string/hello_world"/>   In Android the text are displayed in "textviews" you have to create a text view in order to display a text. Since Android have already created a "textview" for us you can just modify to display your own text. "android:text=" is the syntax for displaying text in xml. It means you are specifying a text here to display. Now the "@string/hello_world" means the text is stored in a "string" called "hello_world" if you see the if you open res >values > strings.xml  you can see the string as well as the text as follows 
<string name="hello_world">Hello world!</string> Here "hellow_world" is the string used to store the text "Hello world!" (Dont confuse both you can even store the text in a string named "abc".)  if you change the text you can display your own text.

     Now we are going for an easiest method to display your own text. (Good practice is to use the string to store the text) Replace the  @string/hello_world   and just type your name (starting with I am Mr..)within the double quotes.
(see the image below)



Using Graphical Method
This is the easiest method for this project. First select the Graphical Layout Tab, and click on the form widget on the left side (see the image below), now drag and drop it to the blank page, you can see a blue rectangular outline, click on it and you can specify the text,text size,style etc on the properties window right side.




Now you have successfully created a project to display your own name while running the application. Before running you have to make an Android Virtual Device Manager (known as AVD) to see the result same as in the Mobile phone. It is very easy. Just click the Virtual Device Manager icon from the tool bar (see the image below)



Now you will get a child window named Android Vertual Device Manager. click "New" to create a new one. Another child window named create new Android Virtual Device (AVD) is open, Now give the following in the respective fields, (see the image below)
AVD Name: "vertualDevice" (don't give space) 
Device: 4.7" WXGA (1280x720:xhdpi) (you can select any virtual device to imitate the phone or tablet)
Keyboard :  "Click to tick" Hardware keyboard present. (otherwise you cannot enter text from computer keyboard)
Skin: "Click to tick" display a skin with hardware control (so that it will give the option of menus and back button)
Internal Storage: 200 MiB
SD Card Size : 200 MiB 
Now click Ok to create the AVD. Now your AVD is created and you can run your project.




To Run your project click on the RUN button (see the image below). No child window is opened select "Android Application" and press "OK



Now your AVD is automatically launched ( see the image below)



After some time your application will be launched (It will take some 3-4 Minutes to launch) and  your name will be shown in the screen. (As shown below)

 
That's it. You have created your first application. Do experiment with the Graphical Layout, drag and drop many textviews in the layout and try to display different texts and give different formatting to the text. You may read the android tutorials given under the Resources  in the right side. Plenty of Android forums will give you answer to any of your questions. Just search in the search box (customized for Android Development forums and sites ) given above. Thank You,

(TIP) For better performance and avoid crashing, always launch AVD from AVD Manager and click the option "Scale display to real size"



                                                                 Best of Luck 

We will discus how to export your Application as APK (Android Package File) 
 in the NEXT CHAPTER 

2 comments:

Please give your valuable comments so as to know whether this blog is useful to you.You can also write helpful Android Development Syntax or Shortcuts or Tips.