Android Application Development Notes

Conventions:
The text listed in blue should be changed.
The text listed in green can optionally be changed.
1: How to find the view for a given ID.

  some_name = (VIEW_TYPE) findViewById(R.id.id_name);


2. How to create OnClickListener and OnLongClickListener
A: How to create OnClickListener:

Insert this code into "onCreate"

  Button button_name;
  button_name = (Button) findViewById(R.id.id_name);
  button_name.setOnClickListener(new clicker ());
Create a new class
  class clicker implements Button.OnClickListener  {
    public void onClick(View v)  {
      // INSERT CODE HERE.  
    }
  }      
B: How to create OnLongClickListener:

Insert this code into "onCreate"

  Button button_name;
  button_name = (Button) findViewById(R.id.id_name);
  button_name.setOnLongClickListener(new clicker ());
Create a new class
  class clicker implements Button.OnLongClickListener  {
    public boolean onLongClick(View v)  {
    // INSERT CODE HERE.  

    return true;
    }
  }      


3. How to read information from an EditText
For numbers:
  variable_name = Float.valueOf(((EditText)findViewById(R.id.id_name)).getText().toString());

For strings:
  variable_name = ((EditText)findViewById(R.id.id_name)).getText().toString();


4. How to write information to a TextView:
TextView some_name = (TextView) findViewById(R.id.id_name);
some_name.setText(String.valueOf(variable name or value));


5. Animation
A. How to add Animation:
ViewType logo_name = (ViewType) findViewById(R.id.id_name);
Animation fade_name = AnimationUtils.loadAnimation(this, R.anim.file_name);
logo_name.startAnimation(fade_name);

Add the following to file_name:

<?xml version="1.0" encoding="utf-8" ?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="4500">
    </alpha>
</set>
B. How to create AnimationListener:
fade_name.setAnimationListener(new AnimationListener() {
  public void onAnimationEnd(Animation animation) {
    // The animation has ended 
  }

  @Override
  public void onAnimationRepeat(Animation arg0) {
    // TODO Auto-generated method stub
  }

  @Override
  public void onAnimationStart(Animation arg0) {
    // TODO Auto-generated method stub
  } 
});




6. RadioButtons:

How to declare:

    RadioButton radio_button_name;
    radio_button_name = (RadioButton) findViewById(R.id.id_name);
How to determine if "RadioButton" is checked:
  if(radio_button_name.isChecked() == true) {
    // Do something
  }


7. Spinners:

  Spinner spinner_name = (Spinner)findViewById(R.id.id_name);
  String selected_value = spinner_name.getSelectedItem().toString();
       	       
  if (selected_value.equals("some value")) { 
    // Do something
  }

  Place the following in: res/values/arrays.xml
  Or just place the "string-array" in: res/values/strings.xml

  <?xml version="1.0" encoding="utf-8"?>
  <resources>
    <string-array
        name="array_name">
        <item>item 1</item>
        <item>item 2</item>
    </string-array>
  </resources> 


8. CheckBoxes:

How to declare:

  CheckBox checkbox_name; 
  checkbox_name = (CheckBox) findViewById(R.id.id_name); 

How to determine if "CheckBox" is checked:

  if (checkbox_name.isChecked()) {
    // Do something
  }


9. Rollover Buttons:

Place the following code in: res/xml/buttons.xml

  <?xml version="1.0" encoding="utf-8"?>
  <selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_pressed="true"
          android:drawable="@drawable/image1" />
    <item android:drawable="@drawable/image2" />
  </selector> 

Specify the following for "src":

  android:src="@xml/buttons" 


10. Toast Messages:

  Toast.makeText(this, "Some string", Toast.LENGTH_LONG).show();  
     
  Toast.makeText(getApplicationContext(), someString, Toast.LENGTH_SHORT).show(); 


11. Notifications:

  String ns = Context.NOTIFICATION_SERVICE;
  NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
           
  int icon = R.drawable.notification_icon;
  CharSequence tickerText = "Notification Ticker Text";

  long when = System.currentTimeMillis();
  Notification notification = new Notification(icon, tickerText, when);          
  Context context = getApplicationContext();
  CharSequence contentTitle = "Notification Content Title";
  CharSequence contentText = "Notification Content Text";
  Intent notificationIntent = new Intent(getApplicationContext(), MyClass.class);
  PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
  notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);                 
  final int HELLO_ID = 1;
  mNotificationManager.notify(HELLO_ID, notification);      	


12. How to start and finish Activities:

  startActivity(new Intent(currentActivity.this, newActivity.class));
  currentActivity.this.finish();
Make sure the new activity is listed in the file: AndroidManifest.xml
  <activity android:name="newActivity" /> 


13. How to make the mobile device vibrate.

  // Get instance of Vibrator from current Context
  Vibrator v1 = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
  // Vibrate for 500 milliseconds
  v1.vibrate(500);

Add the following line to: AndroidManifest.xml

   <uses-permission android:name="android.permission.VIBRATE"/>


14. How to use the camera

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == CAMERA_PIC_REQUEST) {
    Bitmap image = (Bitmap) data.getExtras().get("data");
    ImageView imageview = (ImageView) findViewById(R.id.ImageView01);
    imageview.setImageBitmap(image);
  }
}

Add the following line to: AndroidManifest.xml

    <uses-feature android:name="android.hardware.camera"></uses-feature>  


Other helpful information

  1. Helpful links:
    Android Developer
    Android Widgets

  2. Image formats supported in Android:
    PNG - Portable Network Graphics (Recommended)
    JPEG (Joint Photographic Experts Group) (Supported)
    GIF (Graphics Interchange Format) (discouraged)

  3. DIP or DP - stands for - Device Independent Pixels
  4. SIP or SP - stands for Scale-independent pixels

  5. What is the difference between match_parent and wrap_content?
    Either attribute can be applied to View's (visual control) horizontal or vertical size. It's used to set a View or Layouts size based on either it's contents or the size of it's parent layout rather than explicitly specifying a dimension.
    Match_parent     (Note: prior to Android 2.2 (API level 8) this was called "fill_parent")
    Setting the layout of a widget to match_parent will force it to expand to take up as much space as is available within the layout element it's been placed in. It's roughly equivalent of setting the dockstyle of a Windows Form Control to Fill. Setting a top level layout or control to match_parent will force it to take up the whole screen.
    Wrap_content
    Setting a View's size to wrap content will force it to expand only far enough to contain the values (or child controls) it contains. For controls -- like text boxes (TextView) or images (ImageView) -- this will wrap the text or image being shown. For layout elements it will resize the layout to fit the controls / layouts added as its children. It's roughly the equivalent of setting a Windows Form Control's Autosize property to True.

    wrap_content takes the actual size of the child
    match_parent takes the size of the parent
    sp takes the size of what you given

  6. Image sizes used in class textbook
    LDPI - 240 x 360
    MDPI - 320 x 480
    HDPI - 480 x 854

  7. How to set "Auto Activation triggers for Java" for the Eclipse editor.
    Add the following information to the textbox shown in red:
    .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

    Auto Activation

  8. In Eclipse press "CTRL+space" to view available completions.

  9. Screen captures showing exporting and importing projects into Eclipse.

    Export screen capture

    Import screen capture