TechEndu

Give me six hours to chop down a tree and I will spend the first four sharpening the axe - Abraham Lincoln.

This blog work in same way of this quote where your basic understanding of Technologies like Java and Android (how to chop down a tree ) will be cleared in a easy way and motivational articles will play role of sharping your axe!!!

Android Basics Part II





ANDROID BASICS PART I                                                      ANDROID BASICS PART III



ANDROID BASICS PART II



Mohan Lalwani





1.       Explain about Folders , File and Description of Android App :

1. src

2. gen

3. bin

4. res

5.assets


Ø  src 

·         contains .java source files for your project

·         you write code for your application in this file

·         file is available under the package name of your project


Ø  gen      
  
·          contains R.java file

·         It is a compiler generated file

·         It references all the resources found in your project

·         You should not modify this file Android 4.** library

·         This folder contains android.jar file

·         it contains all the class libraries needed for an android application


Ø  bin
              
·      Bin folder will contain binary files which are generated after compiling and linking like 

     class files and .apk file

·         contains android package files .apk

·         It is built by ADT during build process

·         It contains everything that needs to run an android application assets

·        This folder contains all the information about HTML file, text files, databases, etc.


Ø  res

·       This folder contains all resource files that is used by android application

·       This folder again contains many sub folders like menu folder to create menus, layout folder 
     
to create layouts for your activities, drawable folder to store images used in your project.

·        values folder will contain styles and strings related xml files

Ø  assets

·         This folder contains all the information about HTML file, text files ,databases, etc


Extended Question: -


F1.What does R indicates?

View all your answers on this page - http://techendu.blogspot.in/p/android-basics-ex.html?m=1



2.       What is resource

A user defined JSON, XML, bitmap, or other file, injected into the application build process, which can later be loaded from code


res/drawable-hdpi :

It contains drawable objects that are designed for high density screens e.g images,icons


res/layout:

It contains files(.xml) that define your apps user interface


res/values:

It has various xml files that contains resources like 

             styles.xml,

             colors.xml,

             dimens.xml,

             strings.xml,etc



3.       What is hdpi ldpi mdpi xhdpi xxhdpi

A set of six generalized densities:
ü  ldpi (low) ~120dpi
ü  mdpi (medium) ~160dpi
ü  hdpi (high) ~240dpi
ü  xhdpi (extra-high) ~320dpi
ü  xxhdpi (extra-extra-high) ~480dpi
ü  xxxhdpi (extra-extra-extra-high) ~640dpi


4.       I] Where are layout details placed? Why?

II] What is the importance of XML-based layouts?


·  Layout details are placed in XML files while other items are placed in source files

·  XML based layouts provide a consistent and standard means of setting GUI definition format

· The use of XML-based layouts provides a consistent and somewhat standard means of

   setting GUI definition format.

·  In common practice, layout details are placed in XML files while other items are placed in source files.


5.       What do you mean by Orientations


           · Orientation decides if the LinearLayout should be presented in row wise or column wise fashion.

   · The values are set using setOrientation()

           · The values can be HORIZANTAL or VERTICAL.


6.       AndroidManifest.xml - What is Android manifest file? Why do you need this?


       ·         Manifest file which describes fundamental characteristics of the app

·       for e.g what will be app name,app icon,app version,etc

·   This file is essential in every application. It is declared in the root directory and contains information about the application that the Android system must know before the codes can be executed.





<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   
    package="com.example.hhhhhh"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock" >
        <activity
           
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
       
         </activity>
    </application>
</manifest>



EXPLANATION




<?xml version="1.0" encoding="utf-8"?>                                                                  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hhhhhh"                          // package name of an application
    android:versionCode="1"                                      // version number of an application
    android:versionName="1.0" >                               // version name of an application

    <uses-sdk
        android:minSdkVersion="19"
// minimum version of the OS on which application will run
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

//icon of your application...ic_launcher.png is default image that located in res/drawable folders

        android:label="@string/app_name"
//name of your application...available in res/values/strings.xml file

        android:theme="@style/Theme.Sherlock" >       //theme of your application 
 
 <activity

            android:name=".MainActivity"                              //information about activity
            android:label="@string/app_name" >
            <intent-filter>                                                                                                            
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>
</manifest>




The AndroidManifest.xml file contains the following information about the application

It contains the package name of the application.

The version code of the application is 1.This value is used to identify the version number of your application.

The version name of the application is 1.0

The android:minSdkVersion attribute of the element defines the minimum version of the OS on which the application will run ic_launcher.png is the default image that located in the drawable folders app_name defines the name of application and available in the strings.xml file

It also contains the information about the activity. Its name is same as the application name


7.       Which elements can occur only once and must present

       Manifest and Application element can occur only once


8.       What is the importance of setting permissions in app development?

·         Using permission, certain restrictions can be set to protect data and code

·         Codes could be compromised in absence of these permissions

·         It may cause defects in functionality.


9.       An Android application needs to access device data e.g SMS messages,camera,etc. 

      At what stage user needs to grant the permissions?

         Application permission must be granted by the user at install time


10.   What is a service?


  • A service doesn’t have a visual user interface, but rather runs in the background for an indefinite period of time.

  • For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it.

  • Each service extends the Service base class.


11.   Explain about Activities :

·         It is used to provide interactive screen to the users

·         A single screen in an application, with supporting Java code.

·         An activity presents a visual user interface for one focused endeavor the user can undertake.

·         For example, an activity might present a list of menu items users can choose from or it might display photographs along with their captions.

·         When you create an android application in eclipse though wizard ,it asks you the name of the activity

·         Default name is Main Activity (You can provide any name as per your choice)

·         Android application can have one or more activities.

·         Basically, it is a class that is inherited automatically from Activity Class

·         Activity goes through number of stages ,known as activity's life cycle




Example:

packagecom.example.careerride;            //Application name careerride
importandroid.os.Bundle;                       // Default packages
importandroid.app.Activity;                   // Default packages
importandroid.view.Menu;

public class MainActivity extends Activity
{
        @Override
        protected void onCreate(Bundle savedInstanceState)
       {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_main);
       }

@Override
publicbooleanonCreateOptionsMenu(Menu menu)
      {
                                // inflate the menu; this adds items to the action bar if it is present.
                                getMenuInflater().inflate(R.menu.main, menu);
                                return true;
      }
}




When you run the application onCreate method is called automatically.

Sincere Thanks to Source:

12.   Explain activity's life cycle :


According to me,it can be best explained here :


13.   When does onResume() method called?

·     onResume() method is an activity lifecycle method.

·     This is called when the activity comes to the foreground().

·     You can override this method in your activity to execute code when activity is started,restarted
   
     or comes to the foreground.


14.   Where will you declare your activity so the system can access it ?

Activity is to be declared in the manifest file


15.   Explain activityCreator

·         initial step for creation of new Android project

·         It consists of a shell script that is used to create a new file system structure required for writing codes in Android IDE.


16.   What is IDE?

An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmersfor software development. 

An IDE normally consists of a source code editor, build automation tools and a debugger. 

Most modern IDEs haveintelligent code completion. 

Some IDEs contain a compiler, interpreter, or both, such as NetBeans and Eclipse; others do not, such asSharpDevelop and Lazarus.

An Integrated Development Environment (IDE) is an all-in-one solution that allows an application (app) developer (a.k.a. programmer) to perform the software development cycle repeatedly and quickly. 

That cycle is to design, write (or code), compile, test, debug and package the app software. For Android app development Google currently supports two IDEs (but read on for a list of alternative IDEs and languages):

1.       Android Developer Tools (ADT) – http://developer.android.com/sdk/index.html

2.       Android Studio – http://developer.android.com/sdk/installing/studio.html (in beta)


Both of these IDEs require the use of the Java computer language to write Apps. 

The first option uses the well-established Eclipse IDE. The second option is based upon the IntelliJ IDE.

The Google IDEs and the Java language are not the only options for Android App development. 

Some developers might not need the power of Java or just don’t get on with C style languages. 

Some developers would like a single code base to support other platforms: Apple (iOS), Windows, Blackberry and the Web (HTML5). This is known as cross-platform development.

Sincere Thanks to Sources:



17.   What is Android IDE?


AIDE is an integrated development environment (IDE) for developing real Android apps directly on your Android device.

It comes with interactive coding lessons and step-by-step tutorials for app development and Java programming skills. 

You can visually design apps, write code with the editor which can do code completion, real-time error checking, refactoring, smart code navigation and more.


AIDE supports Java/Xml and the Android SDK, apps with C/C++ and the Android NDK as well as pure Java console applications. 

AIDE is fully compatible Dropbox and allows easy download of your code from your Dropbox and sync back your changes. 

AIDE can also open Android Studio projects which follow the default folder structure. AIDE also supports Git for professional development.


Sincere Thanks to Source:



18.   How to start Another Activity?


Intent i= new Intent(getApplicationContext(),Activity2.class);

startActivity(i);



//Note: You need to paste this code on onClick method



19.   Differentiate Activities from Services.

Activities can be closed, or terminated anytime the user wishes. 

On the other hand, services are designed to run behind the scenes, and can act independently.

Most services run continuously,regardless of whether there are certain or no activities being executed.




20.   What is a Fragment?


Fragments is a new concept introduced in 3.0 version.

The basic purpose of fragments is:

1. Fragments are designed to use the device UI space efficiently. When you are writing an application in android, then some people can download it into phone, some into tablets. If you see the space in tablets it will be little bigger than phones. You should be able to use that space efficiently. But you can't keep writing different applications one targeting for phone, and other targeting for tablets. In order to do it efficiently, i.e. writing only application that can fit well with all screen sizes, we use fragments concept.

2. Fragments are designed as a reusable UI components between more than one activity. Once you design a fragment, you can view it as a detachable independent unit, so that you can plug it into any activity where ever there is a space. That means you can reuse the code designed for a fragment.

Fragment you can think of it like a sub activity, which sits with in an activity and which contributes its own UI to the activity screen. Fragments are always part of an activity. Without an activity, a fragment will not exist. So your fragment life cycle will always be affected by activity life cycle. An activity can contain more than one fragment. Similarly a fragment can be re used in multiple activities.

A fragment is a part or portion of an activity. It is modular in a sense that you can move around or combine with other fragments in a single activity.

Fragments are also reusable.

We can use Activity without Fragments But We can't use fragment without activity.
Sincere Thanks to Source:


21.   Intent


·    It's an intention to do an action

·    Most common use of intents is to start new activities

·    If you want to navigate from one activity to another, then android provides you intent class

·    This class is available in android.content.Intent package

·   Using Intentes, notification messages can be displayed to the user from an android enable devices

·   The users can respond to intents Depending on the intent, apps or OS might be listening for it and will react accordingly

·    A class (Intent) which describes what a caller desires to do.

·  The caller will send this intent to Android’s intent resolver, which finds the most suitable activity for the intent. E.g. opening a PDF document is an intent, and the Adobe Reader apps will be the perfect activity for that intent (class)

·    Intents displays notification messages to the user from within the Android enabled device. It can be used to alert the user of a particular state that occurred. Users can be made to respond to intents


There are two types of Intents.

1) Explicit Intents

2) Implicit Intents



1. Explicit Intents are used to call a specific component. When you know which component you want to launch and you do not want to give the user free control over which component to use.

2. For example, you have an application that has 2 activities. Activity A and activity B. You want to launch activity B from activity A. In this case you define an explicit intent targeting activity B and then use it to directly call it.

3. Implicit Intents are used when you have an idea of what you want to do, but you do not know which component should be launched. Or if you want to give the user an option to choose between a list of components to use.

4. If these Intents are send to the Android system it searches for all components which are registered for the specific action and the data type. If only one component is found, Android starts the component directly.

5. For example, you have an application that uses the camera to take photos. One of the features of your application is that you give the user the possibility to send the photos he has taken.

6. You do not know what kind of application the user has that can send photos, and you also want to give the user an option to choose which external application to use if he has more than one. In this case you would not use an explicit intent. Instead you should use an implicit intent that has its action set to ACTION_SEND and its data extra set to the URI of the photo.

7. An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters . 

Implicit Intents do not directly specify the Android components which should be called, it only specifies action to be performed. 

An Uri can be used with the implicit intent to specify data type.


For example

      Intent intent = new Intent(ACTION_VIEW,Uri.parse("http://www.google.com");


This will cause web browser to open a webpage .

Android system searches for all components which are registered for the specific action and the data type. 

If many components are found then the user can select which component to use..

Explicit intents are used in the application itself wherein one activity can switch to other activity.



Example 

      Intent intent = new Intent(this,Target.class);


This causes switching of activity from current context to the target activity. 


     Explicit Intents can also be used to pass data to other activity using putExtra method and retrieved by target activity by getIntent().getExtras() methods.

Intents works in pairs: action and data. The action defines what you want to do, such as editing an item, viewing the content of an item etc. The data specifies what is affected, such as a person in the Contacts database. The data is specified as an Uri object.


Explicitly starting an Activity


Intent intent = new Intent (this, SecondActivity.class);

startActivity(intent);



Here SecondActivity is the name of the target activity that you want to start.



Implicitly starting an Activity

If you want to view a web page with the specified URL then you can use this procedure.

Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse(“http://www.amazon.com”));
startActivity(i);



If you want to dial a telephone number then you can use this method by passing the telephone number in the data portion


Intent i=new Intent (android.content.Intent.ACTION_DIAL,Uri.parse(“tel:+9923.....”));

      startActivity(i);

In the above method the user must press the dial button to dial the number. If you want to directly call the number without user intervention, change the action as follows:


Intent i = new Intent (android.content.Intent.ACTION_CALL,Uri.parse(“tel:+9923.....”));

startActivity(i);



If you want to dial tel no or use internet then write these line in AndroidManifest.xml
<uses-permissionandroid:name=”android.permission.CALL_PHONE”/>
<uses-permissionandroid:name=”android.permission.INTERNET”/>



22.   What is the function of an intent filter?


Because every component needs to indicate which intents they can respond to, intent filters are used to filter out intents that these components are willing to receive. One or more intent filters are possible, depending on the services and activities that is going to make use of it.

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive.

When you create an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object.


AndroidManifest.xml

<activity android:name=”.HelloWorld”
    android:label=”@string/app_name”>
    <intent-filter>
        <action android:name=”android.intent.action.VIEW”/>
        <category android:name=”android.intent.category.DEFAULT”/>
        <category android:name=”android.intent.category.BROWSABLE”/>
        <data android:scheme=”http” android:host=”androidium.org”/>
    </intent-filter>
</activity>
Launch HelloWorld

Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse(“http://androidium.org”));
startActivity(intent);



Sincere Thanks to Source:

23.   What is the difference between Activity and Intent

In very simple language. Activity is your user interface and whatever you can do with a user interface. When you move from one user interface you need to launch that new user interface with an intent. 

The intent is your event that is passed along with data from the first user interface to another.

Intents can be used between user interfaces and background services too. Also an intent is passed when you want to broadcast data to all activities and background services.

Intent lives as an object, activity lives with a face and interactions.

In the simplest case, you start a new activity like this:

Intent intent = new Intent(this, SomeOtherActivity.class);
startActivity(intent);


It sure looks like you are starting an activity, and the activity that you are starting is "intent".

But what you are really doing is calling the method startActivity() and you are passing it a container called intent. That container tells startActivity() what to do.

You can see it more clearly when you are passing data to a new activity


Intent intent = new Intent(this, SomeOtherActivity.class);

startActivity(intent);

intent.putExtra("ANIMAL_TYPE", "unicorn");

intent.putExtra("ANIMAL_COLOR", "ruby");

startActivity(intent);


Now when you call startActivity(), it looks at intent and knows that it needs start the SomeOtherActivity class. 

Also, in the SomeOtherActivity class, you can access those passed key/value pairs from the intent like this:


Bundle extras = getIntent().getExtras();
if(extras !=null) 
           {
                String animal = extras.getString("ANIMAL_TYPE");
                String animalColor =        extras.getString("ANIMAL_COLOR");
           }


Sincere Thanks to Source:
24. What is an Action?


  • In Android development, an action is what the intent sender wants to do or expected to get as a response.

  • Most application functionality is based on the intended action

  •  It is a string value that is assigned to intent. Action string can be defined by the Android itself or by you as third party application developer .


25.    How will you pass data to sub-activities?

We can use Bundles to pass data to sub-activities. There are like HashMaps that and take trivial data types. These Bundles transport information from one Activity to another.


Example :

        Bundle b = new Bundle();

        b.putString("EMAIL", "abc@xyz.com");

        i.putExtras(b); // where i is the intent



26.   How will you call a sub activity? Write code.


        Intent intent = new Intent(this, SubActivity.class);

        addintent.putExtra(name, value);

        startActivityForResult(intent, int);




Sincere Thanks to Sources:




Find More Here:


ANDROID BASICS PART I                                             ANDROID BASICS PART III

No comments:

Post a Comment