Thursday, February 15, 2018

Layouts In Android

As discussed in the previous chapter that layouts are used to create User Interface for our Android Applications and inflated in the Activity class using setContentView() method
The basic building blocks of Android user interface is View. Each views  has attributes/properties. Attributes defines the look of the views. Some of the important and  frequently used views in Android are Layout, TextView, EditText, Button,CheckBoxes, Radio Buttons, Progress bar etc. How to use these views and how to handle the click and other events on these views will be discussed in detail in next chapter.
In this chapter we will discuss about Layout and their types.
Before going through the layouts we must understand some of the basic and important attributes of layout and views.

Attribute
Description
android:id
This is the ID which uniquely identifies the view.
android:layout_width
This is the width of the view.
android:layout_height
This is the height of the view
android:layout_marginTop
This is the extra space on the top side of the view.
android:layout_marginBottom
This is the extra space on the bottom side of the view.
android:layout_marginLeft
This is the extra space on the left side of the view.
android:layout_marginRight
This is the extra space on the right side of the view.
android:layout_gravity
This specifies how child Views are positioned.
android:layout_weight
This specifies how much of the extra space in the layout should be allocated to the View.


LinearLayout
A Linear  Layout  arranges all  its children in  vertical or horizontal direction, this direction is is given by      android:orientation  attribute.   There are two possible values for this attrinute , vertical and horizontal.
Lets discuss each of them separately.
Vertical Linear Layout
In vertical layouts children are arranged in vertical direction.
Example:
Create a new layout inside layout folder with name main.xml

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
       android:layout_marginTop="30dp">
   

              <TextView
                     android:id="@+id/textView1"
              android:layout_gravity="center_horizontal"
              android:layout_marginTop="30dp"
              android:layout_marginBottom="25dp"
              android:textSize="25dp"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="This Is Vertical Orientation" />


              <Button
              android:id="@+id/button1"
                     android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Button 1" />


              <Button
              android:id="@+id/button2"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Button 2" />

              <Button
              android:id="@+id/button3"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Button 3" />

              <Button
              android:id="@+id/button4"
                  android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Button 4" />

</LinearLayout>



Now inflate this layout main.xml in your  Activity class

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          
        // Inflate the layout
        setContentView(R.layout.main);
    }

}





                                                   




You can see all child views(1 textview and 4 buttons) are placed vertically in the scrren..










Android Application Components

Before you start developing your own Android applications, it is important to understand how
Android applications are constructed and to have an understanding of the Android application life cycle.
In this chapter you will learn about Android application components that make up an Android application. You will also learn about Android application life cycle and managing resources.
Android Application Components
Android application components are building blocks of an Android application. Each component is a different point through which the system can enter your app and they also interact each other.
There are four major types of application components. Each type of component has  a distinct purpose and has a distinct lifecycle that defines how the component is created and destroyed.
Activity
 An activity represents a single screen with a user interface with which user can interact. For example when you open the phone/dialer application you see dialer screen, this is an activity. Another example when you open messaging application you see a list of SMS this is an activity. When you click on compose button a new screen for composing SMS gets open this is another Activity.
An activity is implemented as a subclass of Activity class, you need to extend Activity class and override some of the methods as per your requirement. Activity and it’s life cycle will be described in detail later in this chapter.
Services
 A service is a component that runs in the background to perform long-running operations. A service does not provide a user interface. For example, a service can vibrate device in the background, a service can download an application in the  background or a service can transfer file over Bluetooth in background.
A service is implemented as a subclass of Service class, you need to extend Service class and override some methods as per your requirement. Service and its life cycle will be described in detail later in this chapter.
Broadcast receivers
A broadcast receiver is a component that responds to system generated broadcast announcements. Android system  sends announcements  when some events occur.  All registered receivers for an event are notified by the Android runtime once this event happens. For example android system broadcast announcements when a SMS is received, when there is an incoming call, when boot process completes, when battery is low etc.
To receive these broadcasts we need to register our application for respective broadcast for example if we want to receive broadcast when a SMS received we need to register the application for SMS_RECEIVED event.
Broadcast receivers are implemented using BroadcastReceiver class, you need to extend BroadcastReceiver class and override onReceive() method. Broadcast receivers will be described in detail later in this chapter.
Content provider
A content provider allows applications to access data. Content provider component supplies data from one application to others on request. A SQLite database is private to the application which creates it. If you want to share your application data with other applications you need to use a content provider.
We also use content provider to access other application’s data. For example you can access the phonebook contacts using content providers.
Note: All the Android components must be added/declared in manifest file with respective under application tag. How to add these components in manifest file is described below in respective sections.
Let us understand all these Android components in detail.
Activities
An Activity is an application component that provides a screen containing user interface with which users can interact in order to do something. An application can have one or  multiple activities that are loosely bound to each other. Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application is launched for the first time. Each activity can then start another activity in order to perform different actions.
Declaring the activity in manifest file
An activity is declared in manifest by activity tag.
<activity android:label="labelName"
      android:name=".MyActivity">
</activity>
You can also add action and category attributes.
The <action> element specifies that this is the "main" entry point to the application. The <category> element specifies that this activity should be listed in the system's application launcher (to allow users to launch this activity).
To make an activity launcher activity that will be execute when application will be launch, you need to mention it in manifest file like below
<activity android:name=".ExampleActivity">
   
<intent-filter>
       
<action android:name="android.intent.action.MAIN" />
       
<category android:name="android.intent.category.LAUNCHER" />
   
</intent-filter>
</activity>

Note: All your application activities must be declared with <Activity> tag in manifest, if you do not declare them in manifest you will get exception “ActivityNotFoundException”.

Creating an Activity
To create an activity, you need to extend  Activity class. In your subclass, you need to implement callback methods which will be call by android system when the state of the activity gets changed such as when the activity is being created, stopped, resumed, or destroyed. The two most important callback methods are:
You must implement this method. This method is called by system when creating your activity. In this method you need to set your user interface with which user will interact. Most importantly, this is the method where you must call setContentView() to define the layout for the activity's user interface.
The system calls this method when another activity gets into the foreground and current activity goes in background. When an activity starts another activity, the current activity goes in pause state.

  There are several other lifecycle callback methods that you can override in order to provide a effective user experience between activities.

Adding Emulator and Real Devices in Android

The Android SDK comes with a virtual mobile device emulator that runs on your computer. The emulator facilitates  you develop and test Android applications without using  real physical device.
But there are some limitations of  emulator. You can not test all your application on emulator for example an Android Application related to voice call or video call can not be tested because emulators can not have a SIM card. If you are developing a complex application you are advised to test on real Android device. 
In this chapter both topics are discussed, creating and deploying Android Emulator(also called Android Virtual Device or “AVD”),  deploying/running Android project on real device.

Creating Emulator
To create an Emulator, follow below steps

1: Select  Window( in menu bar) and choose  “Android Virtual Device Manger” or you can also directly choose  Android Virtual Device Manger icon(marked with  red in below fig.).


2:  AVD Manager will get open, you can there is no available device, Click on “New” to create an AVD



3: A new window with title       gets opened, you need to give a name to AVD and specify some values. Click on “OK”.

4: You will see following window with your newly created AVD, click on “Start“ to start your AVD.


6: A window asking screen size will appear, give the screen size or leave it as it is and click on “Launch”


7: A new AVD will get launch. AVD will get time to get ready depending on your system’s speed. Generally it takes 2-3 minutes.
You see the AVD/Emulator


Click on OK button of AVD (bottom right corner).
Now your AVD is ready, you can deploy/run your Android application.
Ø  To deploy your Android application just right click on your Android project and select “Run as Android Application”

Adding real Android devices
The first thing you need to do is to install the Device Driver of your Android Device. You can download the device driver from respective device  manufactures website. For Samsung devices it can be downloaded from following URL.
Once the device driver is installed connect your device to your System via USB cable. You will see your real phone is listed under the device list.



Android Development Tools Part1

You must have written JAVA programs , what you typically do is to use an editor like notepad to write  the code, compile it with javac command and run it with java command using command prompt. An integrated development environment (IDE) is a programming environment which facilitates a code editor, a built in compiler, a debugger, and a graphical user interface (GUI) builder and many other tools. Some other features of IDE includes auto correction od code, auto completion code, giving suggestion, drag and drop etc.
For Android Development we can use IDEs like Eclipse, Android Studio etc.
In this book all the sample examples have been developed in Eclipse
Eclipse
Eclipse is an integrated development environment (IDE). It contains a base workspace and an plug-in system for customizing the environment. Eclipse is packed with many powerful features which makes it an idea IDE, besides these features Eclipse also has a plug in system with which we can customize it for  creating and developing vast variety of application including Android applications, Web Applications etc.

Downloading  and configuring the  Eclipse
1: You can download Eclipse from  http://www.eclipse.org/downloads/ . Select Eclipse IDE for Java Developers  and download the zip file.
2: Extract the zip file into your preferred folder.

3:To start Eclipse double click on the Eclipse icon, you can also create a shortcut icon for Eclipse on your desktop.



Setting up the Workspace

When Eclipse starts for the first time, it shows a dialog asking to set up the workspace, as shown in Figure. All your projects will be saved in this workspace. You can give any name to your workspace and check the checkbox at bottom left, other it will ask to choose workspace every time you run Eclipse.

Note: You can have multiple workspaces and switch between one workspace to other. If you have multiple projects keep them in different workspaces because managing all them in a single workspace becomes difficult. Also keeping projects in their own workspaces makes it easier to find the project when you have to go back to open it and  to fix bugs.




Configuring Eclipse for Android Development

Now you have installed Eclipse, the next step is to configure it  to setup the environment for Android Development. You need to install the ADT(Android Development tools)plugin

Note:  Android Development Tools (ADT) is custom plugin for the Eclipse IDE. This plugin is designed to give you a powerful, integrated environment  to design, develop and debug Android apps, we have discussed it briefly in previous chapter. ADT plugin extends the capabilities of Eclipse to let you quickly set up and create new Android projects, design and build  user interfaces , debug your application, manage resources of your application and other various significant features.

Installing the ADT plugin

To Install ADT plugin follow  below steps :

1: Open/Run the Eclipse (If it is not already running)
2. Go to Help > Install New Software.
Install window opens, you always use this window to install new pluging or software for Eclipse
3. Click Add, in the top-right corner
   A dialog window  will appear with title “add repository”
4. In the Add Repository dialog that appears, enter "ADT Plugin" for the Name field and the following URL for the Location field:

https://dl-ssl.google.com/android/eclipse/
the Location is the  name of the web address on Internet where software is hosted on the. Using this location you can  location  update the software/plugin when a new version is released.

Note: If you have problem in  acquiring the ADT plugin, try using "http" in the Location URL, instead of "https" (https is used only for security reasons).

Android Development Tools Part 2

Android Development Tools(ADT)
ADT (Android Developer Tools) is a plugin for Eclipse that provides a suite of tools that are integrated with the Eclipse IDE .ADT provides various tools  which facilitates the design of speedy quality applications. These tools comprise   of development, design, analysis and debug tools.
Getting a good knowledge of all these tools will help you in developing and debugging your Android applications in better and efficient manner. Let’s go through all these tools briefly, I will be discussing these tools in detail in next chapter after you install and get familiar with Eclipse IDE
The most important SDK tools include the Android SDK Manager (Androdid SDK), the AVD Manager (Android AVD) the emulator (emulator), and the Dalvik Debug Monitor Server (DDMS). A summary of some of the  commonly -used SDK tools is below.
Android SDK Manager: Provides the API for Android Application Development.
Dalvik Debug Monitor Server (DDMS): Helps in debugging your Android Application and integrated into Eclipse .It provides a number of various useful features  for interacting with Emulators and other Android Devices. You use DDMS to view and manage processes and threads running on the device, view heap data, attach to processes to debug, and a variety of other tasks.

Android Emulator (emulator): Also calles Android Virtual Device(AVD), Since you are developing Android application, you need to test your application in an Android Device.Emulator is a virtual Android device that looks, acts, and behave like a physical Android device. You can use Emulator to run and debug and test your Android Application. A comprehensive detail of how to create , manage emulators will be discussed in next chapter.

Hierarchy Viewer (hierarchyviewer) Lets you debug and optimize an Android application's user interface. It shows your

Draw 9-patch Allows you to easily create a NinePatch graphic using a WYSIWYG editor. It also previews stretched versions of the image, and highlights the area in which content is allowed.

Note : These tools are present in tools folder  in Android SDK folder. See following fig. for reference.
Creating and configuring emulator has been  discussed in next chapter.


Wednesday, February 14, 2018

Romantic Sweet Qoutes Mo

$*#@%$ ca-app-pub-2341236189713257/5485394116





Romantic qoutes for Romantic people




The first day you came into my life, i realized that you will stay here until the end.


I am of the believe that dreams can surely come true, because mine did when we both met. I will never leave you, my love!


Absolutely true for any person who is in real love with their partner. In this era of leechers, it is tough to find such people. If you happen to find one, treat him/her as a rare gem. You deserved i


I can not say how much i love you, and how special you are. All I can say is that my world in full of happiness and smile whenever i am with you.


This is a saying that this type of love is only once in a life.
 And i say for me that one is you. My today and tomorrow is all for you. Love you from the depth of my heart and soul.



 I didn’t cho0se this life. I chose HIM. The life was just part of the deal



 If you have a love in your life, who turns any moment of sadness into a marvelous one. Then this quote is for them.
 Share this love quote with your lover and see the smile on their face. It will be priceless.



 I know that my feelings for you are real. As i spend more time thinking of you than myself.



All i know of love is just because of you. Please never leave me. My life is just an empty flame without you.



I loved you once my sweet prince. And i still do. And will keep loving you till my last breath.



You make me complete. I love you so much, I didn’t know what love meant until I met you.



A true love is rarer than the Pink Star diamond. And your love is not ordinary.
Do not miss any chance to let your love know how much you love him/her.
Tell them this love quote, and they will undoubtedly be the happiest person alive.



I wish that you know how much i feel happy when we are together. You add colors to my life. Thank you my love for being there for me all the time.



I have a crush on your mind, I fell for your personality, and your looks are just a big bonus




I know that God has opted some special person for me, i have no need to search him because i have found him. I love you and will continuously do.



I am in love with you and this reality is far better than my dreams.