Step 03 - Donate Button

Place a button directly on to the activity - attached to the bottom of the screen as shown:

Step 03 - 图1

Rename the button in the Outline view:

Step 03 - 图2

Fix the lint error - and give the button the text ‘Donate!’. If all goes as expected, your xml files should be like this:

activity_donate.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".Donate" >
  10. <TextView
  11. android:id="@+id/donateTitle"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_alignParentLeft="true"
  15. android:layout_alignParentRight="true"
  16. android:layout_alignParentTop="true"
  17. android:text="@string/donateTitle"
  18. android:textAppearance="?android:attr/textAppearanceLarge" />
  19. <TextView
  20. android:id="@+id/donateSubtitle"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_alignParentLeft="true"
  24. android:layout_alignParentRight="true"
  25. android:layout_below="@+id/donateTitle"
  26. android:text="@string/donateSubtitle"
  27. android:textAppearance="?android:attr/textAppearanceMedium" />
  28. <Button
  29. android:id="@+id/donateButton"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:layout_alignParentBottom="true"
  33. android:layout_centerHorizontal="true"
  34. android:text="@string/donateButton" />
  35. </RelativeLayout>

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">Donation</string>
  4. <string name="action_settings">Settings</string>
  5. <string name="donateTitle">Welcome Homer</string>
  6. <string name="donateSubtitle">Please give generously</string>
  7. <string name="donateButton">Donate</string>
  8. </resources>

If there is a deviation from the above - retrace your steps (delete the button) until you can match the above.

We can now switch our attention to the Java Activity class Donate:

  1. package com.example.donation;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. public class Donate extends Activity
  6. {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState)
  9. {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_donate);
  12. }
  13. @Override
  14. public boolean onCreateOptionsMenu(Menu menu)
  15. {
  16. // Inflate the menu; this adds items to the action bar if it is present.
  17. getMenuInflater().inflate(R.menu.donate, menu);
  18. return true;
  19. }
  20. }

For any ‘controls’ a user can interact with we usually find it useful to associate a class member with that object. Currently we only have one - a Button. The text fields we dont consider ‘interactive’ as such, so we will not include those.

Insert the following new field into the class:

  1. private Button donateButton;

The class will have to be imported. The class name will always match the name in the Pallette:

Step 03 - 图3

We are free to call the variable anything we like. However, in order to keep confusion to a minimum, always call the variable by the same name you used in the Outline view:

Step 03 - 图4

In onCreate - we need to initialise this variable:

  1. donateButton = (Button) findViewById(R.id.donateButton);

We might also add a logging message so we can have some feedback as the app launches:

  1. Log.v("Donate", "got the donate button");

This is the complete activity class:

  1. package com.example.donation;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.util.Log;
  5. import android.view.Menu;
  6. import android.widget.Button;
  7. public class Donate extends Activity
  8. {
  9. private Button donateButton;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState)
  12. {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_donate);
  15. donateButton = (Button) findViewById(R.id.donateButton);
  16. Log.v("Donate", "got the donate button");
  17. }
  18. @Override
  19. public boolean onCreateOptionsMenu(Menu menu)
  20. {
  21. getMenuInflater().inflate(R.menu.donate, menu);
  22. return true;
  23. }
  24. }

Finding the log message can be very difficult, unless you set a filter. In the ‘LogCat’ view in eclipse, create a filter like this:

Step 03 - 图5

If you then select the filter, we should see our message:

Step 03 - 图6

We should check the donate button actually exists before logging our success:

  1. if (donateButton != null)
  2. {
  3. Log.v("Donate", "Really got the donate button");
  4. }

Run the app again, and verify the above message appears.