The Christmas-Calendar - A TableLayout with SubActivity

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

The Christmas-Calendar - A TableLayout with SubActivity

Postby plusminus » Wed Dec 05, 2007 12:17 am

[align=center]The Christmas-Calendar - A TableLayout with SubActivity[/align]

What you learn: You will learn how to create a Christmas Calendar using a TableLayout and how to start SubActivities (+ passing extra Data to them).

:?: Problems/Questions: Write them right below...

Difficulty: 24 of 31 :lol:

What it will look like:
[align=center]Running on different Resolutions,
reveals the power of XML-Based GUI-Definition:
Image Image
Image[/align]

Description:
The whole setup of this application is pretty simple, as we have just a lot of the same thing to do (like 24 times :roll: ).


0.) The first thing as always we will do is to think about the layout of what we want to display. :!:
As we want to display a calendar, a TableLayout would fit absolutely perfect :!:

Lets take a look at the calendar_layout.xml. It will consist of 4 Rows, with 6 columns each. Each field in our table is 'filled' with a Button, that has a ID, a Text (the day) and as we do not want normal buttons, but fancy look-through ones, we apply a Style to them(which we will also create in the next step).
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <!-- Demonstrates using a TableLayout to create a christmas calendar -->
  4.  
  5. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  6.  
  7.                                 android:background="@drawable/christmasbackground"
  8.  
  9.                android:layout_width="fill_parent"
  10.  
  11.                android:layout_height="fill_parent"
  12.  
  13.                android:stretchColumns="0,1,2,3,4,5"
  14.  
  15.                >
  16.  
  17. <TableRow
  18.  
  19.  android:layout_width="fill_parent"
  20.  
  21.  android:layout_height="wrap_content">
  22.  
  23.         <Button id="@+id/cmd_door_2" style="@style/DoorButton" android:text="2"/>
  24.  
  25.         <Button id="@+id/cmd_door_21" style="@style/DoorButton" android:text="21"/>
  26.  
  27.         <Button id="@+id/cmd_door_11" style="@style/DoorButton" android:text="11"/>
  28.  
  29.         <Button id="@+id/cmd_door_10" style="@style/DoorButton" android:text="10"/>
  30.  
  31.         <Button id="@+id/cmd_door_5" style="@style/DoorButton" android:text="5"/>
  32.  
  33.         <Button id="@+id/cmd_door_16" style="@style/DoorButton" android:text="16"/>
  34.  
  35. </TableRow>
  36.  
  37. <TableRow
  38.  
  39.  android:layout_width="fill_parent"
  40.  
  41.  android:layout_height="wrap_content">
  42.  
  43.         <Button id="@+id/cmd_door_7" style="@style/DoorButton" android:text="7"/>
  44.  
  45.         ....
  46.  
  47. </TableRow>
  48.  
  49. <TableRow ... >
  50.  
  51.         ....
  52.  
  53. </TableRow>
  54.  
  55. <TableRow ... >
  56.  
  57.         ....
  58.  
  59. </TableRow>
  60.  
  61. </TableLayout>
Parsed in 0.005 seconds, using GeSHi 1.0.8.4


As we want to fill the whole screen(at least in width) we applied above the following code to the TableLayout:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.                 android:layout_width="fill_parent"
  2.  
  3.                 android:layout_height="fill_parent"
  4.  
  5.                 android:stretchColumns="0,1,2,3,4,5"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


1.) As see in the screenshots above we are not handling "standard"-Buttons :!:
Every Button is adapted to the same customized style:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.         <Button id="@+id/cmd_door_7" style="@style/DoorButton" android:text="7"/>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

We remember that the "@"-sign refers to a resource of this project, here "@style" refers to a style-resource. Styles like the @style/DoorButton are normally defined within the styles.xml.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <resources>
  4.  
  5.     <style name="DoorButton">
  6.  
  7.         <!-- Replaces the complex BackGround to a simple color -->
  8.  
  9.         <item name="android:background">@color/door_background</item>
  10.  
  11.  
  12.  
  13.         <!-- Text-Style -->
  14.  
  15.         <item name="android:textSize">26sp</item>
  16.  
  17.         <item name="android:textStyle">bold</item>
  18.  
  19.        
  20.  
  21.         <!-- Text-Color -->
  22.  
  23.         <item name="android:textColor">@color/door_caption_color_selected</item>
  24.  
  25.         <item name="android:normalTextColor">@color/door_caption_color</item>
  26.  
  27.        
  28.  
  29.         <!-- Spacing between the Buttons -->
  30.  
  31.         <item name="android:padding">4px</item>
  32.  
  33.         <item name="android:paddingTop">6px</item>
  34.  
  35.     </style>
  36.  
  37. </resources>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


So we are simply overriding the default-style-parameters for the Button. We once again refer to xml-Color-Definitions we place in a file like colors.xml.
[align=center]:arrow: Detailed explanation of defining colors in xml[/align]
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <resources>
  4.  
  5.    <color name="door_background">#4200</color>
  6.  
  7.    <color name="door_caption_color_selected">#F00</color>
  8.  
  9.    <color name="door_caption_color">#BBFF0000</color>
  10.  
  11. </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4

[align=center]Phew, that was enough XML for [s]this tutorial[/s] now =)[/align]

2.) Lets get to the ChristmasCalendar.xml:
We will need an OnClickListener to react on the clicks to all of the 24 Buttons. We choose one single OnClickListener for all Buttons as a click on them will lead to almost the same outcome just a different picture (/res/drwawable/door_X.gif) will be loaded).

So we read out the Caption of the Button, switch on it, to get the related Drawable-Resource (put that to a b]Bundle[/b](within an b]Intent[/b]), that will be passed to our b]ImageDisplay[/b]-SubActivity, which displays the Drawable.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public OnClickListener myOcl = new OnClickListener(){
  2.  
  3.                 // @Override
  4.  
  5.                 public void onClick(View aView) {
  6.  
  7.                         /* It definitely was a button that was
  8.  
  9.                          * clicked, as we assigne only Buttons
  10.  
  11.                          * to this OnClickListener */
  12.  
  13.                         Button clickedButton = (Button)aView;
  14.  
  15.                         String buttonCaption = clickedButton.getText().toString();
  16.  
  17.                         Integer buttonNumber = Integer.parseInt(buttonCaption);
  18.  
  19.  
  20.  
  21.                         int drawableResID = 0;
  22.  
  23.                         switch(buttonNumber){
  24.  
  25.                                 case 1: drawableResID = R.drawable.door_1; break;
  26.  
  27.                                 case 2: drawableResID = R.drawable.door_2; break;
  28.  
  29.                                 case 3: drawableResID = R.drawable.door_3; break;
  30.  
  31.                                 // ... 4 - 22 here too =)
  32.  
  33.                                 case 23: drawableResID = R.drawable.door_23; break;
  34.  
  35.                                 case 24: drawableResID = R.drawable.door_24; break;
  36.  
  37.                         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

We now determined the Drawable behind this calendar-door. Now we prepare to send an Intent to the ImageDisplayer-Activity (will be explained seperately) the will display that Drawable.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                         /* This Intent will start the Viewing
  2.  
  3.                          * of the image behind the door later */
  4.  
  5.                         Intent i = new Intent(ChristmasCalendar.this,
  6.  
  7.                                                                                 ImageDisplayer.class);
  8.  
  9.                        
  10.  
  11.                         /* We want to send two extra-datasets
  12.  
  13.                          * (the ResID of the Drawable and some text)
  14.  
  15.                          * to our ImageDisplayer */
  16.  
  17.                          // A Bundle is just like a pimped HashMap
  18.  
  19.                         Bundle myExtras = new Bundle(2);
  20.  
  21.                         // Add those to extra datasets
  22.  
  23.                         myExtras.putString(CAPTION_TO_SHOW_MARKER, "Behind door " + buttonCaption + " is...");
  24.  
  25.                         myExtras.putInteger(DRAWABLE_RESID_MARKER, drawableResID);
  26.  
  27.                        
  28.  
  29.                         /* Assign the Bundle to the Intent.
  30.  
  31.                          * The Intent will 'carry' it to the
  32.  
  33.                          * SubActivity, where we will read the values back */
  34.  
  35.                         i.putExtras(myExtras);
  36.  
  37.                        
  38.  
  39.                         // We use SUB_ACTIVITY_REQUEST_CODE as an 'identifier'
  40.  
  41.                         startSubActivity(i, IMAGESUBACTIVITY_ID);
  42.  
  43.                 }
  44.  
  45.     };
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

3.) Lets take a look at the onCreate(...)-method of our ChristmasCalendar-Activity. It also does not much except calling applyListenerToButtons(myOcl);, which applies the listener from the previous step to all the 24 Buttons from our XML-Layout. After that it simply displays a Notification that shows the current Date and Time (don't cheat on opening doors of tomorrow ;) ):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     /** Called when the activity is first created. */
  2.  
  3.     @Override
  4.  
  5.     public void onCreate(Bundle icicle) {
  6.  
  7.         super.onCreate(icicle);
  8.  
  9.         setContentView(R.layout.calendar_layout);
  10.  
  11.        
  12.  
  13.        
  14.  
  15.         // Apply the Button-Listener to all Buttons from the xml-file
  16.  
  17.         applyListenerToButtons(myOcl);
  18.  
  19.        
  20.  
  21.         // Retrieve a dateString using the DateUtils
  22.  
  23.         String dateString = DateUtils.dateString(
  24.  
  25.                         System.currentTimeMillis()).toString();
  26.  
  27.        
  28.  
  29.         // Get the notification manager serivce.
  30.  
  31.         NotificationManager nm = (NotificationManager)
  32.  
  33.                 getSystemService(NOTIFICATION_SERVICE);
  34.  
  35.  
  36.  
  37.         /* Show a message showing the current date */
  38.  
  39.         nm.notifyWithText(NOTIFICATION_ID,"Today is: " + dateString + "\n\t\tdon't cheat <img src="http://www.anddev.org/images/smilies/wink.png" alt=";)" title="Wink" />",
  40.  
  41.                 NotificationManager.LENGTH_LONG, null);
  42.  
  43.     }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4

A part of the 'monotonous' applyListenerToButtons()-method:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     private void applyListenerToButtons(OnClickListener aOcl){
  2.  
  3.         // 1 to 10
  4.  
  5.         findViewById(R.id.cmd_door_1).setOnClickListener(aOcl);
  6.  
  7.         findViewById(R.id.cmd_door_2).setOnClickListener(aOcl);
  8.  
  9.         findViewById(R.id.cmd_door_3).setOnClickListener(aOcl);
  10.  
  11.         // ...
  12.  
  13.         findViewById(R.id.cmd_door_10).setOnClickListener(aOcl);
  14.  
  15.         // 11 to 20
  16.  
  17.         findViewById(R.id.cmd_door_11).setOnClickListener(aOcl);
  18.  
  19.         // ...
  20.  
  21.         findViewById(R.id.cmd_door_20).setOnClickListener(aOcl);
  22.  
  23.         // 21 to 24
  24.  
  25.         findViewById(R.id.cmd_door_21).setOnClickListener(aOcl);
  26.  
  27.         // ...
  28.  
  29.         findViewById(R.id.cmd_door_24).setOnClickListener(aOcl);
  30.  
  31.     }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Together with some Constants we are now done with this Activity :)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         // Just random unique ints!
  2.  
  3.         public static final int NOTIFICATION_ID = 0x1337;
  4.  
  5.         public static final int IMAGESUBACTIVITY_ID = 0x7331;
  6.  
  7.        
  8.  
  9.         public static final String DRAWABLE_RESID_MARKER = "DRAWABLE_RESID_MARKER";
  10.  
  11.         public static final String CAPTION_TO_SHOW_MARKER = "CAPTION_TO_SHOW_MARKER";
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


4.) The ImageDisplayer.java-Activity is pretty simple. It loads a Drawable using the id that it got passed in the bundle we sent to it on a click to on of the calendar-doors of the Main-Part. Also provides an simple close-button.

[align=center]This is what the ImageDisplayer-Activity will look like:
Image[/align]
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.christmascalendar;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.view.View;
  10.  
  11. import android.view.View.OnClickListener;
  12.  
  13. import android.widget.Button;
  14.  
  15. import android.widget.ImageView;
  16.  
  17.  
  18.  
  19. public class ImageDisplayer extends Activity{
  20.  
  21.        
  22.  
  23.     /** Called when the activity is first created. */
  24.  
  25.     @Override
  26.  
  27.     public void onCreate(Bundle icicle) {
  28.  
  29.         super.onCreate(icicle);
  30.  
  31.         // Apply our subactivity.xml-Layout
  32.  
  33.         setContentView(R.layout.imagedisplayer_layout);
  34.  
  35.        
  36.  
  37.                 // Read out the passed title and apply it
  38.  
  39.                 String captionToShow = getIntent().getExtras()
  40.  
  41.                                 .getString(ChristmasCalendar.CAPTION_TO_SHOW_MARKER);
  42.  
  43.                 this.setTitle(captionToShow);
  44.  
  45.                
  46.  
  47.                 // Find the ImageView from our xml-layout
  48.  
  49.                 ImageView iv = (ImageView)findViewById(R.id.image);
  50.  
  51.         // Read out the drawableResID
  52.  
  53.                 int drawableResID = getIntent().getExtras()
  54.  
  55.                                 .getInteger(ChristmasCalendar.DRAWABLE_RESID_MARKER);
  56.  
  57.                 iv.setImageResource(drawableResID); // apply it
  58.  
  59.                 iv.invalidate(); // ensures at least one redraw
  60.  
  61.        
  62.  
  63.         // Find the button defined in the subactivity.xml
  64.  
  65.         Button cmd_return = (Button)findViewById(R.id.subactivity_cmd_return);
  66.  
  67.         /* Add an OnClickListener to it, that close this activity */
  68.  
  69.         cmd_return.setOnClickListener(new OnClickListener(){
  70.  
  71.                         // @Override
  72.  
  73.                         public void onClick(View arg0) {
  74.  
  75.                                 // Close this Activity
  76.  
  77.                                 ImageDisplayer.this.finish();
  78.  
  79.                         }
  80.  
  81.         });      
  82.  
  83.     }
  84.  
  85. }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


Finally the xml-Layout for the ImageDisplayer:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:orientation="vertical"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="fill_parent"
  10.  
  11.    >
  12.  
  13.     <ImageView id="@+id/image"
  14.  
  15.         android:layout_width="fill_parent"
  16.  
  17.         android:layout_height="wrap_content"
  18.  
  19.    />
  20.  
  21.         <Button id="@+id/subactivity_cmd_return"
  22.  
  23.             android:layout_width="fill_parent"
  24.  
  25.             android:layout_height="wrap_content"
  26.  
  27.             android:text="BACK"
  28.  
  29.    />
  30.  
  31. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


[align=center]:) Thats it :)[/align]
The full Source:

:idea: You find the whole "/res"-Folder attached to the end of this post :!:

'AndroidManifest.xml'
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    package="org.anddev.android.christmascalendar">
  6.  
  7.     <application android:icon="@drawable/icon">
  8.  
  9.         <activity class=".ChristmasCalendar" android:label="@string/app_name">
  10.  
  11.             <intent-filter>
  12.  
  13.                 <action android:value="android.intent.action.MAIN" />
  14.  
  15.                 <category android:value="android.intent.category.LAUNCHER" />
  16.  
  17.             </intent-filter>
  18.  
  19.         </activity>
  20.  
  21.         <activity class=".ImageDisplayer" android:label="Picture Viewer">
  22.  
  23.             <intent-filter>
  24.  
  25.                 <action android:value="android.intent.action.VIEW" />
  26.  
  27.                 <category android:value="android.intent.category.DEFAULT" />
  28.  
  29.             </intent-filter>
  30.  
  31.         </activity>
  32.  
  33.     </application>
  34.  
  35. </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


'src/your_package_structure/ChristmasCalendar.java'
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.christmascalendar;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.app.NotificationManager;
  8.  
  9. import android.content.Intent;
  10.  
  11. import android.os.Bundle;
  12.  
  13. import android.util.DateUtils;
  14.  
  15. import android.view.View;
  16.  
  17. import android.view.View.OnClickListener;
  18.  
  19. import android.widget.Button;
  20.  
  21.  
  22.  
  23. public class ChristmasCalendar extends Activity {
  24.  
  25.        
  26.  
  27.         // Just random unique ints!
  28.  
  29.         public static final int NOTIFICATION_ID = 0x1337;
  30.  
  31.         public static final int IMAGESUBACTIVITY_ID = 0x7331;
  32.  
  33.        
  34.  
  35.         public static final String DRAWABLE_RESID_MARKER = "DRAWABLE_RESID_MARKER";
  36.  
  37.         public static final String CAPTION_TO_SHOW_MARKER = "CAPTION_TO_SHOW_MARKER";
  38.  
  39.        
  40.  
  41.         public OnClickListener myOcl = new OnClickListener(){
  42.  
  43.                 // @Override
  44.  
  45.                 public void onClick(View aView) {
  46.  
  47.                         /* It definitely was a button that was
  48.  
  49.                          * clicked, as we assigne only Buttons
  50.  
  51.                          * to this OnClickListener */
  52.  
  53.                         Button clickedButton = (Button)aView;
  54.  
  55.                         String buttonCaption = clickedButton.getText().toString();
  56.  
  57.                         Integer buttonNumber = Integer.parseInt(buttonCaption);
  58.  
  59.                        
  60.  
  61.                         int drawableResID = 0;
  62.  
  63.                         switch(buttonNumber){
  64.  
  65.                                 case 1: drawableResID = R.drawable.door_1; break;
  66.  
  67.                                 case 2: drawableResID = R.drawable.door_2; break;
  68.  
  69.                                 case 3: drawableResID = R.drawable.door_3; break;
  70.  
  71.                                 case 4: drawableResID = R.drawable.door_4; break;
  72.  
  73.                                 case 5: drawableResID = R.drawable.door_5; break;
  74.  
  75.                                 case 6: drawableResID = R.drawable.door_6; break;
  76.  
  77.                                 case 7: drawableResID = R.drawable.door_7; break;
  78.  
  79.                                 case 8: drawableResID = R.drawable.door_8; break;
  80.  
  81.                                 case 9: drawableResID = R.drawable.door_9; break;
  82.  
  83.                                 case 10: drawableResID = R.drawable.door_10; break;
  84.  
  85.                                 case 11: drawableResID = R.drawable.door_11; break;
  86.  
  87.                                 case 12: drawableResID = R.drawable.door_12; break;
  88.  
  89.                                 case 13: drawableResID = R.drawable.door_13; break;
  90.  
  91.                                 case 14: drawableResID = R.drawable.door_14; break;
  92.  
  93.                                 case 15: drawableResID = R.drawable.door_15; break;
  94.  
  95.                                 case 16: drawableResID = R.drawable.door_16; break;
  96.  
  97.                                 case 17: drawableResID = R.drawable.door_17; break;
  98.  
  99.                                 case 18: drawableResID = R.drawable.door_18; break;
  100.  
  101.                                 case 19: drawableResID = R.drawable.door_19; break;
  102.  
  103.                                 case 20: drawableResID = R.drawable.door_20; break;
  104.  
  105.                                 case 21: drawableResID = R.drawable.door_21; break;
  106.  
  107.                                 case 22: drawableResID = R.drawable.door_22; break;
  108.  
  109.                                 case 23: drawableResID = R.drawable.door_23; break;
  110.  
  111.                                 case 24: drawableResID = R.drawable.door_24; break;
  112.  
  113.                         }
  114.  
  115.                        
  116.  
  117.                         /* This Intent will start the Viewing
  118.  
  119.                          * of the image behind the door later */
  120.  
  121.                         Intent i = new Intent(ChristmasCalendar.this,
  122.  
  123.                                                                                 ImageDisplayer.class);
  124.  
  125.                        
  126.  
  127.                         /* We want to send two extra-datasets
  128.  
  129.                          * (the ResID of the Drawable and some text)
  130.  
  131.                          * to our ImageDisplayer */
  132.  
  133.                          // A Bundle is just like a pimped HashMap
  134.  
  135.                         Bundle myExtras = new Bundle(2);
  136.  
  137.                         // Add those to extra datasets
  138.  
  139.                         myExtras.putString(CAPTION_TO_SHOW_MARKER, "Behind door " + buttonCaption + " is...");
  140.  
  141.                         myExtras.putInteger(DRAWABLE_RESID_MARKER, drawableResID);
  142.  
  143.                        
  144.  
  145.                         /* Assign the Bundle to the Intent.
  146.  
  147.                          * The Intent will 'carry' it to the
  148.  
  149.                          * SubActivity, where we will read the values back */
  150.  
  151.                         i.putExtras(myExtras);
  152.  
  153.                        
  154.  
  155.                         // We use SUB_ACTIVITY_REQUEST_CODE as an 'identifier'
  156.  
  157.                         startSubActivity(i, IMAGESUBACTIVITY_ID);
  158.  
  159.                 }
  160.  
  161.     };
  162.  
  163.    
  164.  
  165.    
  166.  
  167.     /** Called when the activity is first created. */
  168.  
  169.     @Override
  170.  
  171.     public void onCreate(Bundle icicle) {
  172.  
  173.         super.onCreate(icicle);
  174.  
  175.         setContentView(R.layout.calendar_layout);
  176.  
  177.        
  178.  
  179.        
  180.  
  181.         // Apply the Button-Listener to all Buttons from the xml-file
  182.  
  183.         applyListenerToButtons(myOcl);
  184.  
  185.        
  186.  
  187.         // Retrieve a dateString using the DateUtils
  188.  
  189.         String dateString = DateUtils.dateString(
  190.  
  191.                         System.currentTimeMillis()).toString();
  192.  
  193.        
  194.  
  195.         // Get the notification manager serivce.
  196.  
  197.         NotificationManager nm = (NotificationManager)
  198.  
  199.                 getSystemService(NOTIFICATION_SERVICE);
  200.  
  201.  
  202.  
  203.         /* Show a message showing the current date */
  204.  
  205.         nm.notifyWithText(NOTIFICATION_ID,"Today is: " + dateString + "\n\t\tdon't cheat <img src="http://www.anddev.org/images/smilies/wink.png" alt=";)" title="Wink" />",
  206.  
  207.                 NotificationManager.LENGTH_LONG, null);
  208.  
  209.     }
  210.  
  211.    
  212.  
  213.     private void applyListenerToButtons(OnClickListener aOcl){
  214.  
  215.         // 1 to 10
  216.  
  217.         findViewById(R.id.cmd_door_1).setOnClickListener(aOcl);
  218.  
  219.         findViewById(R.id.cmd_door_2).setOnClickListener(aOcl);
  220.  
  221.         findViewById(R.id.cmd_door_3).setOnClickListener(aOcl);
  222.  
  223.         findViewById(R.id.cmd_door_4).setOnClickListener(aOcl);
  224.  
  225.         findViewById(R.id.cmd_door_5).setOnClickListener(aOcl);
  226.  
  227.         findViewById(R.id.cmd_door_6).setOnClickListener(aOcl);
  228.  
  229.         findViewById(R.id.cmd_door_7).setOnClickListener(aOcl);
  230.  
  231.         findViewById(R.id.cmd_door_8).setOnClickListener(aOcl);
  232.  
  233.         findViewById(R.id.cmd_door_9).setOnClickListener(aOcl);
  234.  
  235.         findViewById(R.id.cmd_door_10).setOnClickListener(aOcl);
  236.  
  237.         // 11 to 20
  238.  
  239.         findViewById(R.id.cmd_door_11).setOnClickListener(aOcl);
  240.  
  241.         findViewById(R.id.cmd_door_12).setOnClickListener(aOcl);
  242.  
  243.         findViewById(R.id.cmd_door_13).setOnClickListener(aOcl);
  244.  
  245.         findViewById(R.id.cmd_door_14).setOnClickListener(aOcl);
  246.  
  247.         findViewById(R.id.cmd_door_15).setOnClickListener(aOcl);
  248.  
  249.         findViewById(R.id.cmd_door_16).setOnClickListener(aOcl);
  250.  
  251.         findViewById(R.id.cmd_door_17).setOnClickListener(aOcl);
  252.  
  253.         findViewById(R.id.cmd_door_18).setOnClickListener(aOcl);
  254.  
  255.         findViewById(R.id.cmd_door_19).setOnClickListener(aOcl);
  256.  
  257.         findViewById(R.id.cmd_door_20).setOnClickListener(aOcl);
  258.  
  259.         // 21 to 24
  260.  
  261.         findViewById(R.id.cmd_door_21).setOnClickListener(aOcl);
  262.  
  263.         findViewById(R.id.cmd_door_22).setOnClickListener(aOcl);
  264.  
  265.         findViewById(R.id.cmd_door_23).setOnClickListener(aOcl);
  266.  
  267.         findViewById(R.id.cmd_door_24).setOnClickListener(aOcl);
  268.  
  269.     }
  270.  
  271. }
Parsed in 0.059 seconds, using GeSHi 1.0.8.4


'src/your_package_structure/ImageDisplayer.java'
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.christmascalendar;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.view.View;
  10.  
  11. import android.view.View.OnClickListener;
  12.  
  13. import android.widget.Button;
  14.  
  15. import android.widget.ImageView;
  16.  
  17.  
  18.  
  19. public class ImageDisplayer extends Activity{
  20.  
  21.        
  22.  
  23.     /** Called when the activity is first created. */
  24.  
  25.     @Override
  26.  
  27.     public void onCreate(Bundle icicle) {
  28.  
  29.         super.onCreate(icicle);
  30.  
  31.         // Apply our subactivity.xml-Layout
  32.  
  33.         setContentView(R.layout.imagedisplayer_layout);
  34.  
  35.        
  36.  
  37.                 // Read out the passed title and apply it
  38.  
  39.                 String captionToShow = getIntent().getExtras()
  40.  
  41.                                 .getString(ChristmasCalendar.CAPTION_TO_SHOW_MARKER);
  42.  
  43.                 this.setTitle(captionToShow);
  44.  
  45.                
  46.  
  47.                 // Find the ImageView from our xml-layout
  48.  
  49.                 ImageView iv = (ImageView)findViewById(R.id.image);
  50.  
  51.         // Read out the drawableResID
  52.  
  53.                 int drawableResID = getIntent().getExtras()
  54.  
  55.                                 .getInteger(ChristmasCalendar.DRAWABLE_RESID_MARKER);
  56.  
  57.                 iv.setImageResource(drawableResID); // apply it
  58.  
  59.                 iv.invalidate(); // ensures at least one redraw
  60.  
  61.        
  62.  
  63.         // Find the button defined in the subactivity.xml
  64.  
  65.         Button cmd_return = (Button)findViewById(R.id.subactivity_cmd_return);
  66.  
  67.         /* Add an OnClickListener to it, that close this activity */
  68.  
  69.         cmd_return.setOnClickListener(new OnClickListener(){
  70.  
  71.                         // @Override
  72.  
  73.                         public void onClick(View arg0) {
  74.  
  75.                                 // Close this Activity
  76.  
  77.                                 ImageDisplayer.this.finish();
  78.  
  79.                         }
  80.  
  81.         });      
  82.  
  83.     }
  84.  
  85. }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Attachments
christmas_calendar_res.zip
Christmas Calendar Res-Folder
(251.93 KiB) Downloaded 871 times
Last edited by plusminus on Wed Dec 12, 2007 9:57 pm, edited 3 times in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby polarbear » Tue Dec 11, 2007 9:47 pm

Hi Plusminus,

The "calendar_layout.xml" is under the folder /res/layout, right?
polarbear
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Dec 03, 2007 4:47 am

Postby plusminus » Tue Dec 11, 2007 10:15 pm

Hello polarbear,

yep, all layout-files go to the "/res/layout/"-folder :)

Regards,
plusminus

btw: Post 300 :lol:
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby polarbear » Tue Dec 11, 2007 11:42 pm

Plusminus, congratulations in your 300 post!! Anyway, where did you place the colors.xml and styles.xml files?
polarbear
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Dec 03, 2007 4:47 am

Postby polarbear » Tue Dec 11, 2007 11:47 pm

Okay. I noticed that in your color tutorial you place the file in /res/values.
polarbear
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Dec 03, 2007 4:47 am

Postby plusminus » Tue Dec 11, 2007 11:55 pm

Hello polarbear,

:D 300.. in just 3 weeks is really much :lol:

I noticed, I should also attach the Full Source to this tutorial. This will be done tomorrow evening.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby Katharnavas » Wed Dec 12, 2007 6:10 am

Hi,
Congrats :!: Its really a hardwork .. Hope to see more ..

plusminus wrote:Hello polarbear,

:D 300.. in just 3 weeks is really much :lol:

I noticed, I should also attach the Full Source to this tutorial. This will be done tomorrow evening.

Regards,
plusminus
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby Katharnavas » Wed Dec 12, 2007 6:12 am

Hi,
We are waiting for the full source code with all the resources(images).
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby plusminus » Wed Dec 12, 2007 5:37 pm

Katharnavas wrote:Hi,
We are waiting for the full source code with all the resources(images).

Hello Katharnavas,

Fulfilled all your wishes ;)
Keep informing me as sth. is missing anywhere here :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby polarbear » Wed Dec 12, 2007 8:28 pm

Hi Plusminus,

Did you define anything in the manifest?

Thank you.
polarbear
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Dec 03, 2007 4:47 am

Postby plusminus » Wed Dec 12, 2007 9:58 pm

polarbear wrote:Hi Plusminus,

Did you define anything in the manifest?

Thank you.


Hello polarbear,

:oops: Thanks for informing me :!:
Added it in the first post.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby venkat » Thu Dec 13, 2007 8:06 am

plusminus congrats for crossing 300th post and i am really exited about your hard work :)

Can u attach full source code of this tutorial... :)
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby Katharnavas » Thu Dec 13, 2007 8:51 am

Hi,
Its already there and working nice for me. Nice work. Thanks for fullfilling my wish (but not all just missed the custom Button one.. )
venkat wrote:plusminus congrats for crossing 300th post and i am really exited about your hard work :)

Can u attach full source code of this tutorial... :)
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby ronenfe » Wed Sep 10, 2008 5:48 pm

hi, some errors i get when i try to run this project:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Description     Resource        Path    Location        Type
  2.  
  3. ERROR Error: No resource found that matches the given name attribute: 'android:normalTextColor' value: 'id'.    styles.xml      CristmasCalendar/res/values     line 13 aapt Problem
  4.  
  5. NotificationManager.LENGTH_LONG cannot be resolved      ChristmasCalendar.java  CristmasCalendar/src/org/anddev/android/christmascalendar       line 104        Java Problem
  6.  
  7. The method getInteger(String) is undefined for the type Bundle  ImageDisplayer.java     CristmasCalendar/src/org/anddev/android/christmascalendar       line 28 Java Problem
  8.  
  9. The method putInteger(String, int) is undefined for the type Bundle     ChristmasCalendar.java  CristmasCalendar/src/org/anddev/android/christmascalendar       line 71 Java Problem
  10.  
  11. The method startSubActivity(Intent, int) is undefined for the type new View.OnClickListener(){} ChristmasCalendar.java  CristmasCalendar/src/org/anddev/android/christmascalendar       line 79 Java Problem
  12.  
  13.  
  14.  
  15.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4

i had more errors which i found out how to fix them, like dateutil is not recognized ( it's located now inside android.pim), also i think that colors.xml should be color.xml ( that's how it's called from the styles.xml)
i will appreciate any help about those errors.
ronenfe
Freshman
Freshman
 
Posts: 4
Joined: Tue Sep 09, 2008 5:12 pm

Postby plusminus » Wed Sep 10, 2008 6:35 pm

That happens because the code is from an early SDK.
Some tips:

"[font=Courier New]id="+@.....[/font]" is now "[font=Courier New]android:id="+@.....[/font]"
[font=Courier New]myBundle.getInteger()/myBundle.putInteger()[/font] is now [font=Courier New]myBundle.getInt()/myBundle.putInt()[/font]
[font=Courier New]startSubActivity[/font] is now [font=Courier New]startActivityForResult[/font]
Notifications using [font=Courier New]NotificationManager[/font] are now from [font=Courier New]Toast[/font] (search for it)

Hope I could help you.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 12 guests