Friday, January 9, 2015

View

This class represents the basic building block for user interface components. A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). 

ViewGroup subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties.

http://developer.android.com/reference/android/view/View.html

Activity

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View).

http://developer.android.com/reference/android/app/Activity.html

Context vs Activity

On Android, a Context is used for many operations but mostly to load and access resources. This is why all the widgets receive a Context parameter in their constructor. In a regular Android application, you usually have two kinds of Context, Activity and Application.

Typically each Activity will have it's own Context and the Application itself will have a Context.

A context allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

http://stackoverflow.com/questions/5454332/is-one-activity-one-context

Application Class

Begin by creating a class that extends Android’s android.app.Application.  Android creates an instance of this class when the application is started – that is when a DVM process is started to run your apk.

As an example of how the Application works, let’s assume that we are building some type of game.  Our game application will have several screens – each screen in this case being an Activity.  Players of the game generate points on each screen and their score needs to be tracked across the many screens of our hypothetical game.  In order to track the user’s game score across the many Activities that make up the game, we need a place to store the game score for the duration of the game – that is for as long as the application process is running. With a custom Application class created, we can use it to track the game score across the many Activity screens in our game application.
http://www.intertech.com/Blog/androids-application-class/

Instead of this you can use Singletons (Global Variables), same functionality in a more modular way.

Networking calls

Android volley is a networking library was introduced to make networking calls much easier, faster without writing tons of code. By default all the volley network calls works asynchronously, so we don’t have to worry about using asynctask anymore.
Volley comes with lot of features. Some of them are
1. Request queuing and prioritization
2. Effective request cache and memory management
3. Extensibility and customization of the library to our needs
4. Cancelling the requests

ListView

ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.

Extends Activity or ListActivity?

ListActivity it's very trivial and doesn't offer any advantage, only that saves time and making the code a little more cleaner, because it offers shortcut methods. For example, you get the onListItemClick() method which is called whenever you click a item which saves you from creating a separate listener.
However it's also said that: "ListActivity has some bugs which manifest themselves in certain use case. So ListActivity is conditonally usable on very very basic use case. And in that case is simple enough to use Activity and ListView."
http://stackoverflow.com/questions/2032417/android-activity-vs-listactivity-which-one-should-my-activity-class-extend

1. Building the UI
First you can create xml for the style of the list. Put this files inside the drawable folder (It is enough if you only paste it on the drawable-hdpi folder for example). Por lists, it's recommended to have 3 xml:
     - Row background. This provides a gradient color:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="@color/list_row_start_color"
      android:endColor="@color/list_row_end_color"
      android:angle="270" />

</shape>

     - Color for when the row is focused

     - Color for when the row is selected

     - A final list selector that unites the 3 files:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/list_row_bg" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/list_row_bg_hover" android:state_pressed="true"/>
    <item android:drawable="@drawable/list_row_bg_hover" android:state_pressed="false" android:state_selected="true"/>

</selector>

    - The list view layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_row_selector" />


</RelativeLayout>

tools:context=".MainActivity" Records which activity the layout is associated with (at designtime, since obviously a layout can be used by more than one layout). This will for example be used by the layout editor to guess a default theme, since themes are defined in the Manifest and are associated with activities, not layouts. This will help with things such as rendering the action bar (which is associated with the activity), a place to add onClick handlers, etc.

    - For the row, it's necessary to take into account this: