Thursday, June 30, 2011

Android Application File Structure

android file structure, what is androidmenfest.xml, androidmenfest is for, jobs of androidmenfest, tags use in androidmenfest, defile resources in androidmenfest
Android File Structure Tutorial 01 
Hi All


Let's look at the file structure and the usage of each file of the Android project. This knowledge is vital when we discus further developments. Open your project file structure and lets see about each file.


 AndroidManifest.xml
This file have vital information about android system, those are important to adjust the application before run codes. Mainly AndroidManifest do following things. 



  • It names the Java package for the application (witch we discuss before post 'Hello World with Android')
  • Describe the component of the application. (the activities, services, broadcast receivers, and content providers that the application is composed of. It names the classes that implement each of the components and publishes their capabilities)
  • It determines which processes will host application components and declarer the permission within the application and also when interacting with other applications, application components.
  • In your file there is list of Instrumentation classes, those are base class for implementing application instrumentation code. These declarations are present in the manifest only while the application is being developed and tested; they're removed before the application is published.
  • Also have defined the minimum level of Android API and Libraries that require.
If you cant understand the things in before list just let it there a bit, because with moving Android you may grab what those are. 
I know this has long text lines unlike before posts, but should get understand about around things also before moving hand on works. 

Now open your AndroidManifest.xml file 
(here I'm using the AndroidManifest.xml file from last developed application in post - Change the Properties of the Interface Element , if you advance Androd programmer this may simple for you.)

Before image is simple example of manifest file, there are lot more tags when you add new services and libraries to the project.

If you want to learn it further, below list contain the complete set of tags use in the androidmanifest use those link provided to explore further.These are the only legal elements; you cannot add your own elements or attributes.


   <uses-sdk> 

This post may complicate beginners, but don't confuse with these tags. you will learn those with next few activities, be with the next blog posts. Android File structure tutorial posts will not add as continues posts. Because it may boring to followers.   


Thank You All


Next post :

Introduction to Buttons 

                                                                                                                                                                                     By MIthila Karunarathna

Saturday, June 11, 2011

More About Android Resources and Manipulation

Android Layout Tutorial 04

First of all I like to know about past layout editing posts are worthy or not, any way I found a easy way to generate layout xml even with complex positionings of object. Check below link just create your layout easily.

                                                                  http://www.droiddraw.org/

OR even you can download the binary also -  (Mac OS X, Windows, Linux)

This post mainly focus about how add resources to the project and how those use, manipulate in layouts. Just step to pool :)

OK, here I want to focus your attention for "res" folder, you can see there are set of files call "drawable-hdpi" , "drawable-ldpi","drawable-mdpi" those files basically for add image resources for your application. BUT WHY 3 FOLDERS?
These are for images of various resolutions, just open folders, there are one sample file in both three folder. Check the sizes of each file, even those three had same name ("icon.png") their sizes are different. So there is meaning with those folder names.

  • drawable-hdpi -  higher dot per inch(hdpi)
  • drawable-ldpi - Lover dot per inch(ldpi)
  • drawable-mdpi - Mid dot per inch(mdpi )

The hdpi qualifier indicates that the resources in that directory are for devices with a high-density screen. While the images in each drawable directory are sized for a specific screen density, the filenames are the same. This way, the resource ID that you use to reference the icon.png image is always the same, but Android selects the version of that drawable that best matches the current device configuration.

EASY WAY TO CREATE IMAGES FOR YOUR APPLICATION 
When you create a image, icon you should provide both three resolution images for better application flexibility to work on different resolution environment. Now by "Android Asset Studio"  provide facility to get generate icons and images with both three resolutions . 
Click Here > Android Asset Studio
It provide functionalities to create what you want in easy manner, otherwise you may have to deal with photoshop to get done those. 

Even in this service provide selected and unselected views also. that will help you a lot when going to develop professional level forms :)

Lets add some images from drawable files. 

open the Layout file and by using "Graphical layout" view add " imageView " to the interface and open the xml view. Got to the " imageView " tags and change the source as this image.


Now run you application, it shows added image in the screen. you can add sound files  for button click, actions etc. but we will discuss those in later tutorials, because those require bit knowledge of java programming and specially about threads. Let me to make prepare you easy to that step. 


Thank You All


Next post :
About Android Application File Structure

                                                                                                                                                                                     By MIthila Karunarathna

Thursday, June 9, 2011

Change the Properties of the Interface Element

Android Layout Tutorial 04

With this post we are going to discuss how change properties of interface element manually and by code too. This require java coding a bit, but don't worry, you will be able to through out. lets move to the tutorial. 

Change properties from property menu in Eclipse 

This is very simple thing and even this may not suitable to make a post also. Actually this is not about Android :), this is just about function require in the Eclipse IDE for Android SDK. With this option you can change the properties of the element (buttons , textareas, etc.) by without editing the layout xml file.

1. Open layout xml file and select the "Graphical layout " as usual way.
2. Now add new button to the interface or if available one on the screen already, Click on that button.
3. Check left side of the IDE for Property window. like below image. IF NOT DON'T WORRY, just follow next bullet.

4. If it not appear as before, just add the window to the eclipse window. follow this steps.

There is a small button in bottom left corner by saying "Show view as fast view" Click on it > Others > General > Properties
Then Property window will appear on the window and you can position as you want by dragging to some positions.






















5. OK, Can you see the property window on the main Eclipse window?  then change the properties as you want. there are huge list of properties have listed down. change colours, text , gravity, etc. simply from properties. DON"T FORGET TO GO AND SEE THE LAYOUT XML AFTER CHANGING ANY PROPERTY VALUE, IT GIVES YOU A KNOWLEDGE ABOUT HOW THESE CHANGES  HAPPEN IN XML.

Change properties when running the application. 

I you are a programmer you feel why we need some value of component when running application, as an example when you going to fill a form in web some of them says your email is wrong by changing that text box colour in to red, just after entering your email with missing important part of an address. In suck event dynamically we have to change the propertied. In android also it is important as before.

Still we didn't learn about Eventhandling in Android, so with this section I'll illustrate how the property change when  load the application. (WITHIN onCreate() METHOD)
Then you can use that on any where you want after leaning other around functions on Android.

1. If you have a button on the layout just open the Activity file within the "src" folder, Or add a button and open the Activity file. ( In my case Activity file is "MyMain.java", if you follow my past post list)

2. Now go to the inside of theonCreate() method.

     public class MyMain extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    // here you are in the method, type your code here :)
    }

Now we should learn a bit about layout objects,
Go to the layout xml and you check out the xml section related to button. It include record mentioning something about ID. All the Objects in the XML have unique ID and it allows us to point out that object from wherever. 
 <Button 
    android:id="@+id/button1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Button2" 
    android:gravity="center_vertical|center_horizontal">
    </Button>


This button's ID is button1.
You can change the object ID from properties menu, but make sure to each ID unique from others.


3. Now we should create button object in the onCreate method.
    this seems bit different and I'll explain a bit about the code and how it works.
Button button1 = (Button)findViewById(R.id.button1);

In normally we start to create button object and then we should cast the object return by findViewById(int) method. OK, this may be some what difficult to understand people who are not really familiar with java. Anyway in simple word this is doing create an object referring the element that we added to the XML. 


What is findViewById(int) ?


Now look at the findViewById(int) method. This method declared in the Android.App package as a public method.  This method coming from API level 1. findViewById(int) return a View, that's why we should cast in to the type of object .

public View findViewById (int id)

Since: API Level 1
Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
Returns
  • The view if found or null otherwise.


Create Button 
     Add below code in to onCreate() methode, make sure ID of your button is "button1" if not it gives you an error.

 Button button1 = (Button)findViewById(R.id.button1);
button1.setText("new button");

IF GOT AN ERROR CHECK :    (R.id.button1) (your button name may be different)


     public class MyMain extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    // here you are in the method, type your code here :)
 Button button1 = (Button)findViewById(R.id.button1);
button1.setText("new button");
    }


Easy way to add the button name, 


After successfully creating the Buotton object, we can edit its properties from Action. 


Place  "OBJECTNAME . "  then you will see list of properties of the object.




Change what you want and run the application, When running the application it shows what you change in the onCreate() method. see below image, button text are change when application running on the phone.




OK, GOT IT? If there is any problem place a comment :). I want to discuss about the int parameter passed to findViewById method. 

findViewById (int id)

 this post Is bit longer than others and it exhausted you and I'll discuss about that in next post, with few around things :) 


Thank You All


Next post :
More About Android Resources and Manipulation

                                                                                                                                                                                     By MIthila Karunarathna

Tuesday, June 7, 2011

Android Layout Editing and Resource Handling 3

Android Layout Tutorial 03
Hi all ....... 

Did you enjoy with layout editing? No we are going to move to see the java codes how can we use the objects in the layout and how add multiple layouts to an application. Obviously there are many different interfaces comes up when we work with application, some interfaces are containing Forms, Information, graphical decorated interface and many more. So it should be able to add many interfaces on multiple layout files and also should have facility to change the visible layout too. With this post I'm going to discuss about those operations.

[Important]


Let's add new layout file to our Android  application.

1. Open the layout folder (where your main.xml located) 
2. Right click on the "Layout' folder and go to      New > Other > Android > Android xml file

3. Now fill the "New Android XML File " form and finish. It basically ask about the project name, name of the file, etc. Select "Layout" when it ask "What type of resource would you like to create?"
Don't do any with resource configuration for now, and select linearLayout (because I think you are familiar with it). Click finish :)

4.OK, now you can see new layout xml have added to the Layout folder, just select it and do changes what you want. see it work same as before.






5. now run your application and check weather it shows in your virtual phone. CAN YOU SEE ?


:-(  Ya!  still it shows old main.xml layout. Now we should instruct to the application to show our new layout instead of main.xml. (in my case new file named as second.xml, it can be any as you preferred)


Now it is time to open our java code :-o , but don't worry still I just want to change some text only no any programming yet.   :) 


Change the main layout in Android application


1. open src folder and package
2. You can see the application activity file with you given name when you create the application. If you cant remember just go to previous Android Hello World Post, and see the given for activity and package name. Here I continually used same application, you can compare the names with my src content also. 




3. open your Activity file (in my case it is "MyMain.java"), and change the it as shown in the image.
setContentView(R.layout.main); ----change to ----->      setContentView(R.layout.second);

because my newly added layout is "second.xml" it may different in your application, add the name you created before. 
4. NOW RUN THE APPLICATION AND SEE THE DIFFERENCE :)

Actually in this post I just what you to show we can add many layouts to one application and how those gonna view on the application. still I didn't do any clarification about how this setContentView () function working and about the Activity file. there are lot to learn about those things. 

This may be a short post comparing with other posts, because I'm bit busy with my work these days and I hope to cover element property handling part too, in this post but I'll do it in my next post. 
And there are in depth discussions about layout and styling about Interfaces should discuss within next posts.

Thank you all for reading :)

Next Post :
 Change Properties of the Interface Element Manually and with Activity File.
;) need a bit about programming :)



By MIthila Karunarathna