View groups containers


Published on

Published in: Technology, News & Politics
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

View groups containers

  1. 1. Topics <ul><li>ViewGroups </li></ul><ul><ul><li>What is viewgroup/layouts/containers? </li></ul></ul><ul><ul><li>XML & Custom based layouts. </li></ul></ul><ul><ul><li>Different types of layouts, LinearLayouts,FrameLayout,RelativeLayout, TableLayout. </li></ul></ul><ul><ul><li>Attributes of layout </li></ul></ul><ul><ul><li>Code snippets demonstrating each of layouts. </li></ul></ul><ul><ul><li>Tools to analyse the layouts (Hierarchy viewer,layoutopt ) </li></ul></ul><ul><li>HomeScreen Widgets </li></ul><ul><ul><li>What is an AppWidget ? </li></ul></ul><ul><ul><li>AppWidget framework </li></ul></ul><ul><ul><li>Steps for creating an AppWidget </li></ul></ul><ul><ul><li>Example demonstrating widget showing google static map of location as home screen widget. </li></ul></ul>
  2. 2. View <ul><ul><li>In an Android application, the user interface is built using  View  and  ViewGroup  objects. There are many types of views and view groups, each of which is a descendant of the  View  class. </li></ul></ul><ul><ul><li>View objects are the basic units of user interface expression on the Android platform.  </li></ul></ul><ul><ul><li>The View class serves as the base for subclasses called &quot;widgets,&quot; which offer fully implemented UI objects, like text fields and buttons </li></ul></ul>
  3. 3. ViewGroups / Containers / Layouts <ul><ul><li>  The ViewGroup class serves as the base for subclasses called &quot;layouts,&quot; which offer different kinds of layout architecture, like linear, table and relative. </li></ul></ul><ul><ul><li>An Android layout is a class that handles arranging the way its children appear on the screen.  Anything that is a View (or inherits from View) can be a child of a layout. All of the layouts inherit from ViewGroup (which inherits from View) so you can add views to it. </li></ul></ul><ul><ul><li>Containers/LayoutManagers are another terminologies to layouts. </li></ul></ul>
  4. 4. View <ul><ul><li>  CheckBox, TimePicker, DatePicker , GridView, ListView,  </li></ul></ul><ul><ul><li>  RadioButton, DigitalClock, EditText, TextView </li></ul></ul><ul><ul><li>  Gallery, ImageView, ProgressBar, VideoView </li></ul></ul><ul><li>ViewGroup </li></ul><ul><ul><li>LinearLayout </li></ul></ul><ul><ul><li>FrameLayout </li></ul></ul><ul><ul><li>TableLayout </li></ul></ul><ul><ul><li>RelativeLayout </li></ul></ul><ul><ul><li>AbsoluteLayout - Deprecated </li></ul></ul><ul><ul><li>ScrollView </li></ul></ul>
  5. 5. Two ways of defining layouts <ul><li>The Android framework gives you the flexibility to use two methods for declaring and managing your application's UI. </li></ul><ul><ul><li>Declare UI elements in XML . Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. </li></ul></ul><ul><ul><li>Instantiate layout elements at runtime . Your application can create View and ViewGroup objects (and manipulate their properties) programmatically. </li></ul></ul>
  6. 6. XML definitions of layouts <ul><ul><li>The most common way to define your layout and express the view hierarchy is with an XML layout file. XML offers a human-readable structure for the layout, much like HTML. Each element in XML is either a View or ViewGroup object (or a descendant ) </li></ul></ul><ul><ul><li><? xml version = &quot;1.0&quot; encoding = &quot;utf-8&quot; ?> <LinearLayout xmlns:android = &quot;;               android:layout_width = &quot;fill_parent&quot;               android:layout_height = &quot;fill_parent&quot;               android:orientation = &quot;vertical&quot; >     <TextView android:id = &quot;@+id/text&quot;               android:layout_width = &quot;wrap_content&quot;               android:layout_height = &quot;wrap_content&quot;               android:text = &quot;Hello, I am a TextView&quot; />     <Button android:id = &quot;@+id/button&quot;             android:layout_width = &quot;wrap_content&quot;             android:layout_height = &quot;wrap_content&quot;             android:text = &quot;Hello, I am a Button&quot; /> </LinearLayout> </li></ul></ul>
  7. 7. XML definitions of layouts <ul><ul><li>You could declare your application's layouts in XML, including the screen elements that will appear in them and their properties.. </li></ul></ul><ul><ul><li>The advantage to declaring your UI in XML is that it enables you to better separate the presentation of your application from the code that controls its behavior. </li></ul></ul>
  8. 8. Accessing UI elements from XML <ul><ul><li>You could then add code in your application that would modify the state of the screen objects, including those declared in XML, at run time. </li></ul></ul><ul><ul><li>public void onCreate(Bundle savedInstanceState) { </li></ul></ul><ul><ul><ul><li>super.onCreate(savedInstanceState); </li></ul></ul></ul><ul><ul><ul><li>setContentView(R.layout.main); </li></ul></ul></ul><ul><ul><ul><li>mTextView = (TextView) findViewById(; </li></ul></ul></ul><ul><ul><ul><li>mButton = (Button) findViewById(; </li></ul></ul></ul><ul><ul><ul><li>mTextView.setText(&quot;Accesing elements from XML..&quot;); </li></ul></ul></ul><ul><ul><ul><li>mButton.setOnClickListener(new View.OnClickListener() { </li></ul></ul></ul><ul><ul><ul><ul><li>@Override </li></ul></ul></ul></ul><ul><ul><ul><ul><li>public void onClick(View v) { </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>mTextView.setText(&quot;Text changed on Button Click&quot;); </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul><ul><ul><ul><li>}); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>- Generation of from resources by aapt tool during compilation. </li></ul></ul>
  9. 9. Instantiate layout elements at runtime . <ul><li>    public void onCreate(Bundle savedInstanceState) { </li></ul><ul><li>        super.onCreate(savedInstanceState); </li></ul><ul><li>        //setContentView(R.layout.main); </li></ul><ul><li>        LinearLayout mainLayout = new LinearLayout(this); </li></ul><ul><li>        mainLayout.setOrientation(LinearLayout.VERTICAL); </li></ul><ul><li>         </li></ul><ul><li>        //Create the child element </li></ul><ul><li>        mTextView = new TextView(this); </li></ul><ul><li>        mTextView.setText(&quot;Instantiating layout elements runtime&quot;); </li></ul><ul><li>        mTextView.setTextSize(30); </li></ul><ul><li>         </li></ul><ul><li>        //Add to main layout. </li></ul><ul><li>        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((LayoutParams.WRAP_CONTENT, </li></ul><ul><ul><li>LayoutParams.WRAP_CONTENT); </li></ul></ul><ul><li>        mainLayout.addView(mTextView, params); </li></ul><ul><li>         </li></ul><ul><li>        //Create child element </li></ul><ul><li>        mButton = new Button(this); </li></ul><ul><li>        mButton.setText(&quot;Click here...&quot;); </li></ul><ul><li>         </li></ul><ul><li>        // Add to main layout </li></ul><ul><li>        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); </li></ul><ul><li>        mainLayout.addView(mButton, params); </li></ul><ul><li>         </li></ul><ul><li>        mButton.setOnClickListener(new View.OnClickListener() { </li></ul><ul><ul><li>@Override </li></ul></ul><ul><ul><li>public void onClick(View v) { </li></ul></ul><ul><ul><ul><li>mTextView.setText(&quot;Text changed on Button Click&quot;); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>}); </li></ul></ul><ul><li>        setContentView(mainLayout); </li></ul><ul><li>    } </li></ul>
  10. 10. Attributes <ul><ul><li>Every View and ViewGroup object supports their own variety of XML attributes. Some attributes are specific to a View object (for example, TextView supports the  textSize  attribute),  </li></ul></ul><ul><ul><li>android:textSize=&quot;30dp&quot; </li></ul></ul><ul><ul><li>  Some are common to all View objects, because they are inherited from the root View class (like the  id  attribute).  </li></ul></ul><ul><ul><li>android : id = &quot;@+id/my_button&quot; </li></ul></ul><ul><li>Layout attributes </li></ul><ul><ul><li>Some attributes are considered &quot;layout parameters,&quot; which are attributes that describe certain layout orientations of the View object, as defined by that object's parent ViewGroup object. </li></ul></ul>
  11. 11. Layout attributes <ul><ul><li>XML layout attributes named  layout_ something  define layout parameters for the View that are appropriate for the ViewGroup in which it resides. </li></ul></ul><ul><ul><li>Every ViewGroup class implements a nested class that extends  ViewGroup.LayoutParams . This subclass contains property types that define the size and position for each child view, as appropriate for the view group. As you can see in figure, the parent view group defines layout parameters for each child view (including the child view group). </li></ul></ul>
  12. 12. Layout attributes <ul><li>LinearLayout, FrameLayout </li></ul><ul><ul><li>( layout_width  and  layout_height ), and each view is required to define them </li></ul></ul><ul><ul><li>layout_weight </li></ul></ul><ul><ul><li>layout_marginLeft </li></ul></ul><ul><ul><li>layout_gravity </li></ul></ul><ul><li> </li></ul><ul><li>RelativeLayout </li></ul><ul><li> </li></ul><ul><ul><li>layout_weight, layout_gravity not defined for RelativeLayout params. </li></ul></ul><ul><ul><li>layout_toLeftOf </li></ul></ul><ul><ul><li>layout_above </li></ul></ul><ul><ul><li>layout_below </li></ul></ul><ul><ul><li>layout_aligParentLeft </li></ul></ul>
  13. 13. LinearLayout <ul><ul><li>LinearLayout organizes elements along a single line. You specify whether that line is verticle or horizontal using android:orientation </li></ul></ul><ul><ul><li>vertical list will only have one child per row, no matter how wide they are. </li></ul></ul><ul><ul><li>a horizontal list will only be one row high  </li></ul></ul>
  14. 14. LinearLayout - Attributes <ul><ul><li>  A linearlayout respects  margin s between children and the  gravity  (right, center, or left alignment) of each child </li></ul></ul><ul><ul><li>  &quot; layout_weight &quot; - This attribute assigns an &quot;importance&quot; value to a view, and allows it to expand to fill any remaining space in the parent view. Child views can specify an integer weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.  </li></ul></ul><ul><ul><li>If we are dividing the parent in to equal parts, we just set the children’s layout_weights all to 1. But if we want to divide it unequally, we can do that in a number of ways. We can either use decimal fractional values which total 1, or we can use integer values. </li></ul></ul>
  15. 15. layout_weight <ul><li>  </li></ul>
  16. 16. layout_weight -- Proportinate fill <ul><ul><li>To create a proportionate size layout on the screen, create a container viewgroup object with the  layout_width  and  layout_height   attributes set to  fill_parent ; assign the children  height  or width to  0  (zero); then assign relative  weight  values to each child, depending on what proportion of the screen each should have. </li></ul></ul>
  17. 17. layout_gravity, gravity <ul><li>The difference between android:gravity and android:layout_gravity is that </li></ul><ul><li>  </li></ul><ul><li>* android:gravity  positions the contents of that view (i.e. what’s inside the view), [code : setGravity() ] whereas   </li></ul><ul><ul><li>* android:layout_gravity  positions the view with respect to its parent (i.e. what the view is contained in).  [  params .gravity = Gravity .BOTTOM;] </li></ul></ul><ul><li> </li></ul>
  18. 18. layout_margins, padding <ul><ul><li>Padding is giving space within the view. </li></ul></ul><ul><ul><li>Margin is giving spaces outside the view where the view is placed in.(between the two elements) </li></ul></ul><ul><li>android:padding  </li></ul><ul><li>android:paddingLeft,android:paddingRight </li></ul><ul><li>android:paddingTop,android:paddingBottom </li></ul><ul><li>android:margin </li></ul><ul><li>android:marginLeft,android:marginRight </li></ul><ul><li>android:marginTop,android:marginBottom </li></ul>
  19. 19. RelativeLayout <ul><ul><li>RelativeLayout lays out elements based on their relationships with one another, and with the parent container. </li></ul></ul><ul><li>Relative To Container(parent): </li></ul><ul><li>These properties will layout elements relative to the parent container. </li></ul><ul><ul><li>android:layout_alignParentBottom – Places the bottom of the element on the bottom of the container </li></ul></ul><ul><ul><li>android:layout_alignParentLeft – Places the left of the element on the left side of the container </li></ul></ul><ul><ul><li>android:layout_alignParentRight – Places the right of the element on the right side of the container </li></ul></ul><ul><ul><li>android:layout_alignParentTop – Places the element at the top of the container </li></ul></ul><ul><ul><li>android:layout_centerHorizontal – Centers the element horizontally within its parent container </li></ul></ul><ul><ul><li>android:layout_centerInParent – Centers the element both horizontally and vertically within its container </li></ul></ul><ul><ul><li>android:layout_centerVertical – Centers the element vertically within its parent container </li></ul></ul>
  20. 20. RelativeLayout <ul><li>Relative To Other Elements </li></ul><ul><ul><li>These properties allow you to layout elements relative to other elements on screen. One thing to remember is that referencing an element before it has been declared will produce an error. </li></ul></ul><ul><ul><li>android:layout_above – Places the element above the specified element </li></ul></ul><ul><ul><li>android:layout_below – Places the element below the specified element </li></ul></ul><ul><ul><li>android:layout_toLeftOf – Places the element to the left of the specified element </li></ul></ul><ul><ul><li>android:layout_toRightOf – Places the element to the right of the specified element </li></ul></ul><ul><li>Alignment With Other Elements </li></ul><ul><ul><li>These properties allow you to specify how elements are aligned in relation to other elements.  </li></ul></ul><ul><ul><li>android:layout_alignBaseline – Aligns baseline of the new element with the baseline of the specified element </li></ul></ul><ul><ul><li>android:layout_alignBottom – Aligns the bottom of new element in with the bottom of the specified element </li></ul></ul><ul><ul><li>android:layout_alignLeft – Aligns left edge of the new element with the left edge of the specified element </li></ul></ul><ul><ul><li>android:layout_alignRight – Aligns right edge of the new element with the right edge of the specified element </li></ul></ul><ul><ul><li>android:layout_alignTop – Places top of the new element in alignment with the top of the specified element </li></ul></ul>
  21. 21. RelativeLayout <ul><li><Button </li></ul><ul><li>android:layout_width=&quot;125px&quot; </li></ul><ul><li>android:layout_height=&quot;wrap_content&quot; </li></ul><ul><li>android:text=&quot;Send&quot; </li></ul><ul><li>android:layout_below=&quot;@+id/txtComments&quot; </li></ul><ul><li>android:layout_alignLeft=&quot;@+id/txtComments&quot; </li></ul><ul><li>/> </li></ul><ul><li><Button </li></ul><ul><li>android:layout_width=&quot;125px&quot; </li></ul><ul><li>android:layout_height=&quot;wrap_content&quot; </li></ul><ul><li>android:text=&quot;Cancel&quot; </li></ul><ul><li>android:layout_below=&quot;@+id/txtComments&quot; </li></ul><ul><li>android:layout_alignRight=&quot;@+id/txtComments&quot; /> </li></ul><ul><li>alignifParentMissing=”true” </li></ul>
  22. 22. FrameLayout <ul><ul><li>  Adds view on top of another in the order the views are defined in XML. </li></ul></ul><ul><ul><li>framelayout positions all child elements   on the top left of the screen. (if gravity is not specified) </li></ul></ul><ul><li>- ImageView </li></ul><ul><li>- TextView </li></ul><ul><li>- Button </li></ul>
  23. 23. Important attributes <ul><li>layout_weight </li></ul><ul><li>gravity, layout_gravity </li></ul><ul><li>padding, layout_margin </li></ul><ul><li>layout_centerHorizontal=&quot;true&quot; --> RelativeLayout </li></ul>
  24. 24. LinearLayout Vs RelativeLayout <ul><ul><li>  All difficult layouts can be visualised in relativelayout. </li></ul></ul><ul><ul><li>  To avoid more nested layouts it is recommended to use relativelayout. </li></ul></ul><ul><ul><li>  Dont use absolutelayout as it is difficult to handle different resolutions. Involved tedious work. </li></ul></ul><ul><li>One scenario is when views are returned to listview it should be as simple as possible. so relativelayouts help to avoid more nested layouts. </li></ul><ul><li> </li></ul>
  25. 25. Layouts / Containers Heirarchy viewer: android-sdk/tools# ./hierarchyviewer
  26. 26. layoutopt tool <ul><ul><li>layoutopt  is a command-line tool that helps you optimize the layouts and layout hierarchies of your applications. </li></ul></ul><ul><li>samples / too_many . xml     7 : 413 The root - level < FrameLayout /> can be replaced with < merge />     - 1 :- 1 This layout has too many views : 81 views , it should have <= 80 ! samples / useless . xml     7 : 19 The root - level < FrameLayout /> can be replaced with < merge />     11 : 17 This LinearLayout layout or its FrameLayout parent is useless </li></ul>
  27. 27. Reference links <ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>
  28. 28. Home screen widgets <ul><ul><li>What is an AppWidget ? </li></ul></ul><ul><ul><li>AppWidget framework </li></ul></ul><ul><ul><li>Steps for creating an AppWidget </li></ul></ul><ul><ul><li>Example demonstrating widget showing google static map of location as home screen widget. </li></ul></ul>
  29. 29. What is an App Widget?? <ul><li>App Widgets are miniature application views that can be embedded in other applications (such as the Home screen) and receive periodic updates. </li></ul>
  30. 30. Appwidget usages <ul><ul><li>People can drop widgets onto their home screen and interact with them </li></ul></ul><ul><ul><li>Widgets can provide a quick glimpse into full-featured apps, such as showing upcoming calendar events, or viewing details about a song playing in the background. </li></ul></ul><ul><ul><li>Users can also interact with your app through the widget, for example pausing or switching music tracks. </li></ul></ul>
  31. 31. AppWidget Framework <ul><ul><li>App Widget Provider Metadata XML file </li></ul></ul><ul><ul><li>AppWidgetProvider class </li></ul></ul><ul><ul><li>View layout </li></ul></ul><ul><ul><li>App Widget configuration Activity (Optional) </li></ul></ul>
  32. 32. AppWidget Framework <ul><ul><li>  App Widget Provider Metadata XML file </li></ul></ul><ul><ul><ul><li>Describes the metadata for an App Widget, such as minimum width, minimum height, update frequency, the AppWidgetProvider class. </li></ul></ul></ul><ul><ul><ul><li>It also references App widget's layout file </li></ul></ul></ul><ul><ul><ul><li>This should be defined in res/xml folder. </li></ul></ul></ul><ul><ul><li>  AppWidgetProvider class </li></ul></ul><ul><ul><ul><li>Defines the basic methods that allow you to programmatically interface with the App Widget, based on broadcast events. (AppWidgetProvider class is a child class of BroadcastReceiver class.) </li></ul></ul></ul><ul><ul><ul><li>Through it, you will receive broadcasts when the ApWidget is updated, enabled, disabled and deleted </li></ul></ul></ul>
  33. 33. Steps for Building an App Widget <ul><ul><li>Declare an AppWidgetProvider in the Manifestfile </li></ul></ul><ul><ul><li>Create the App Widget Provider Info Metadata XML file </li></ul></ul><ul><ul><li>Create the App Widget Layout XML file </li></ul></ul><ul><ul><li>Write the AppWidgetProvider Class </li></ul></ul>
  34. 34. 1. Declare AppWidgetProvider in Manifest <ul><li><receiver android:name=&quot;ExampleAppWidgetProvider&quot; > </li></ul><ul><li><intent-filter> </li></ul><ul><li><action android:name=&quot; android.appwidget.action.APPWIDGET_UPDATE &quot; /> </li></ul><ul><li></intent-filter> </li></ul><ul><li>< meta-data android:name=&quot;android.appwidget.provider&quot; </li></ul><ul><li>android:resource=&quot;@xml/ example_appwidget_info &quot; /> </li></ul><ul><li></receiver> </li></ul>
  35. 35. 2. Create App Widget Provider Info Metadata XML file <ul><li><appwidget-provider </li></ul><ul><li>xmlns:android=&quot;; </li></ul><ul><li>android:minWidth=&quot;294dp&quot; </li></ul><ul><li>android:minHeight=&quot;72dp&quot; </li></ul><ul><li>android:updatePeriodMillis=&quot; 86400000 &quot;  --> Every one day </li></ul><ul><li>android:initialLayout=&quot;@layout/example_appwidget&quot; </li></ul><ul><li>android:configure=&quot;; > </li></ul><ul><li></appwidget-provider> </li></ul><ul><li>previewImage --> Added in android 3.0 </li></ul><ul><li>android:previewImage = &quot;@drawable/preview&quot; > </li></ul>
  36. 36. 3. Create App Widget Layout <ul><ul><li>App Widget layouts are based on RemoteViews,which do not support every kind of layout or view widget. </li></ul></ul><ul><ul><li>A RemoteViews object (and, consequently, an App Widget) can support the following layouts and Widget classes </li></ul></ul><ul><ul><ul><li>FrameLayout, LinearLayout, RelativeLayoyt </li></ul></ul></ul><ul><ul><ul><li>AnalogClock, Button, Chronometer, ImageButton, </li></ul></ul></ul><ul><ul><ul><li>ImageView, ProgressBar, TextView </li></ul></ul></ul><ul><ul><li>Android 3.0 supports additional widgets </li></ul></ul><ul><ul><ul><li>ListView </li></ul></ul></ul><ul><ul><ul><li>GridView </li></ul></ul></ul><ul><ul><ul><li>StackView </li></ul></ul></ul><ul><ul><ul><li>AdapterViewFlipper </li></ul></ul></ul>
  37. 37. 4. Write AppWidgetProvider Class <ul><ul><li>The  AppWidgetProvider  class extends BroadcastReceiver to handle the App Widget broadcasts. The AppWidgetProvider receives only the event broadcasts that are relevant to this App Widget, such as when the App Widget is updated, deleted, enabled, and disabled.  </li></ul></ul><ul><li> Methods to override </li></ul><ul><ul><li>onUpdate(Context, AppWidgetManager, int[]) - called </li></ul></ul><ul><li>when each App Widget is added to a host (unless you use a configuration Activity), Typically the onlymethod that needs to be present </li></ul><ul><ul><li>onDeleted(Context, int[]) </li></ul></ul><ul><ul><li>onEnabled(Context) </li></ul></ul><ul><ul><li>onDisabled(Context) </li></ul></ul><ul><ul><li>onReceive(Context, Intent) </li></ul></ul>
  38. 38. Example programs
  39. 39. AppWidget provider - onUpdate <ul><ul><li>Only one  updatePeriodMillis  schedule will be managed for all instances of the App Widget. </li></ul></ul><ul><ul><li>Because  AppWidgetProvider  is an extension of  BroadcastReceiver , your process is not guaranteed to keep running after the callback methods return i.e onUpdate,onEnabled. </li></ul></ul><ul><ul><li>Consider starting a  Service  in the  onUpdate()  method.  </li></ul></ul><ul><ul><li>To update an appWidget all you need to know is AppWidgetId and have AppWidgetManager instance </li></ul></ul>
  40. 40. Use AlarmManager to update <ul><ul><li>The disadvantage with updateperiodMillis is that minimum value 30 mins . Cannot update the widget before that. </li></ul></ul><ul><ul><li>AlarmManager helps in updating the widget  </li></ul></ul><ul><ul><li>final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  </li></ul></ul><ul><ul><li>final Intent intent = new Intent(context, MyService.class);   </li></ul></ul><ul><ul><li>if (service == null)   </li></ul></ul><ul><ul><li>{   </li></ul></ul><ul><ul><li>service = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); </li></ul></ul><ul><ul><li>}   </li></ul></ul><ul><ul><li>m.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 1000 * 60, service);  </li></ul></ul><ul><ul><li>In service - onStartCommand() - updateWidget(). </li></ul></ul>
  41. 41. Service <ul><ul><li>A  Service  is an application component that can perform long-running operations in the background and does not provide a user interface. </li></ul></ul><ul><li>onStartCommand() </li></ul><ul><ul><li>The system calls this method when another component, such as an activity, requests that the service be started, by calling startService(). </li></ul></ul><ul><li>A service runs in the main thread of its hosting process - Spawn thread to do perform CPU intesive operations to avoid ANRs. </li></ul>
  42. 42. Remoteviews <ul><ul><li>RemoteViews  is the key mechanism behind the AppWidget feature of the Android platform. </li></ul></ul><ul><ul><li>  RemoteViews is a parcelable data structure that can be sent through Android IPC (Inter Process Communication). </li></ul></ul><ul><ul><li>AppWidgets are broadcast event receivers, they process broadcasts from Launcher and respond with RemoteViews. When the RemoteViews data structure is received and deserialized, it can be applied to any ViewGroup of the UI and can therefore appear on the UI of another application (Launcher).  </li></ul></ul>
  43. 43. Overview of AppWidget mechanism
  44. 44. Multiple sized widgets <ul><li>Declare AppWidgetProviderInfo metafiles 4x1,4x2,2x2 </li></ul><ul><li>2x2 : </li></ul><ul><li>android:minWidth=&quot;147dp&quot; </li></ul><ul><li>android:minHeight=&quot;142dp&quot; </li></ul><ul><li>4x2 : </li></ul><ul><li>android:minWidth=&quot;294dp&quot; </li></ul><ul><li>android:minHeight=&quot;142dp&quot; </li></ul><ul><li>Define corresponding Appwidget classes. </li></ul><ul><li>Declare corresponding AppWidget Broadcast receivers in AndroidManifest.xml </li></ul>
  45. 45. Things to keep in mind  <ul><ul><li>Frequency of update should not be high. ex. every 5 mins.It will reduce the battery life of the phone. </li></ul></ul><ul><ul><ul><li>Handled badly your widget could end up making the phone completely unresponsive. Pushing updates to onscreen widgets is a somewhat costly process. </li></ul></ul></ul><ul><ul><li>Avoid nesting of layouts within a layout. </li></ul></ul><ul><ul><li>Give proper padding. As it might collide with adjacent widget. No boundary seperation would be seen. </li></ul></ul><ul><ul><li>Offload any webrequest through a service to avoid ANRs. </li></ul></ul>
  46. 46. Location based map  <ul><li>onEnabled() -> Start a service </li></ul><ul><li>onUpdate() -> Get the appWidgetId and appWidgetType and pass to the service. </li></ul><ul><li>onDeleted() -> Get the appWidgetId and pass to the service. </li></ul><ul><li>Service -> Listens for movement change using PhoneStateListener and fetches current location using GPS or Wifi and downloads the static map as Bitmap  and updates the widgets 2x2 and 4x2. </li></ul>
  47. 47. Implementation overview OnUpdate / onDeleted Pass AppWidgetId startService(intent) updateWidget(AppWidgetId, remoteview) Service LocationIdentifier – GPS/Wifi Fetch the map updateWidget(AppWidgetId, remoteview) updateWidget(AppWidgetId, remoteview) OnUpdate / onDeleted Pass AppWidgetId startService(intent) OnUpdate / onDeleted Pass AppWidgetId startService(intent) Gogle static map Latitude,longitude Spwan a thread AppWidget AppWidget AppWidget Maintains list of AppWidgetIds, AppwidgetType 3 5 6 6 1 4 9 2
  48. 48. Demo snapshot 2x2 - 147dp width, 144dp height 4x2 - 294dp width, 144dp height
  49. 49. To try out in demo <ul><li>-   Add an Button on widgets to let user decide location movement detection ON/OFF and decide updating the widget based on that. </li></ul><ul><li>  CLUE : Add a pendingIntent for the button click to call the same service with an SETTING_ID and check for the intent value in onStartCommand to update the widget. </li></ul><ul><li>Add Configure activity and get the inputs from user.  </li></ul><ul><ul><li>Basically collect some settings values like movement detection On/Off </li></ul></ul><ul><ul><li>Store it in sharedpereferences </li></ul></ul><ul><ul><li>Read the values from sharedpereferences in service and take appropriate action in code. </li></ul></ul>
  50. 50. Reference links <ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>
  51. 51. Thank you Mani.S Email: