Step 05 - Research + New Control Layout

Recall the UI we are trying to implement:

Step 05 - 图1

We need radio buttons, some sort of selection/combo box + a progress bar. These can be found in various locations in the pallette:

Step 05 - 图2
Step 05 - 图3

RadioGroup, ProgressBar and NumberPicker seem likely candidates. The names of these controls are exactly as advertised, and we can expect them to be in the ‘widgets’ package. To verify this, try importing them at the top of the Donate activity class:

  1. import android.widget.RadioGroup;
  2. import android.widget.NumberPicker;
  3. import android.widget.ProgressBar;

… and we can bring in three fields into the class:

  1. private RadioGroup paymentMethod;
  2. private ProgressBar progressBar;
  3. private NumberPicker amountPicker;

We can also open up three pages of documentation - which we can reverse engineer from the package/class names:

Note this time we have gone to the Activity class before actually creating the controls. We should do this now - and remember to use the same names (for the IDs) as we create the controls.

Step 05 - 图4
Step 05 - 图5

Getting the layout +id names as shown above may take some practice. However, it is an essential skill to get on top of, even it it takes a lot of trial and error.

For reference purposes (try to do it yourself first!), these are the relevant generated xml files:

  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" /><Button
  28. android:id="@+id/donateButton"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:layout_alignParentBottom="true"
  32. android:layout_centerHorizontal="true"
  33. android:onClick="donateButtonPressed"
  34. android:text="@string/donateButton" />
  35. <RadioGroup
  36. android:id="@+id/paymentMethod"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:layout_above="@+id/progressBar"
  40. android:layout_alignLeft="@+id/donateSubtitle"
  41. android:layout_below="@+id/donateSubtitle"
  42. android:layout_marginLeft="14dp"
  43. android:layout_marginTop="26dp"
  44. android:layout_toLeftOf="@+id/amountPicker" >
  45. <RadioButton
  46. android:id="@+id/PayPal"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:checked="true"
  50. android:text="@string/PayPal" />
  51. <RadioButton
  52. android:id="@+id/Direct"
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. android:text="@string/Direct" />
  56. </RadioGroup>
  57. <ProgressBar
  58. android:id="@+id/progressBar"
  59. style="?android:attr/progressBarStyleHorizontal"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:layout_above="@+id/donateButton"
  63. android:layout_alignParentLeft="true"
  64. android:layout_alignParentRight="true"
  65. android:layout_marginBottom="67dp" />
  66. <NumberPicker
  67. android:id="@+id/amountPicker"
  68. android:layout_width="wrap_content"
  69. android:layout_height="wrap_content"
  70. android:layout_alignRight="@+id/donateSubtitle"
  71. android:layout_alignTop="@+id/paymentMethod" />
  72. </RelativeLayout>
  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">Button</string>
  8. <string name="PayPal">PayPal</string>
  9. <string name="Direct">Direct</string>
  10. </resources>

If we have our naming conventions right - then we can bind to these new controls in onCreate:

  1. paymentMethod = (RadioGroup) findViewById(R.id.paymentMethod);
  2. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  3. amountPicker = (NumberPicker) findViewById(R.id.amountPicker);

This is the complete Donate 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.view.View;
  7. import android.widget.Button;
  8. import android.widget.RadioGroup;
  9. import android.widget.NumberPicker;
  10. import android.widget.ProgressBar;
  11. public class Donate extends Activity
  12. {
  13. private Button donateButton;
  14. private RadioGroup paymentMethod;
  15. private ProgressBar progressBar;
  16. private NumberPicker amountPicker;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState)
  19. {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_donate);
  22. donateButton = (Button) findViewById(R.id.donateButton);
  23. paymentMethod = (RadioGroup) findViewById(R.id.paymentMethod);
  24. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  25. amountPicker = (NumberPicker) findViewById(R.id.amountPicker);
  26. amountPicker.setMinValue(0);
  27. amountPicker.setMaxValue(1000);
  28. }
  29. @Override
  30. public boolean onCreateOptionsMenu(Menu menu)
  31. {
  32. getMenuInflater().inflate(R.menu.donate, menu);
  33. return true;
  34. }
  35. public void donateButtonPressed (View view)
  36. {
  37. Log.v("Donate", "Donate Pressed!");
  38. }
  39. }