SyntaxHighlighter

Wednesday, September 22, 2010

Android Programming Jumpstart - 4

image This is part four of my series on jump-starting your Android programming experience. 
This lab focuses on Android Widgets.  The tutorials from here on out will gather speed and expect that you do some research of your own.  Not so much spoon feeding from now on.




  • TextView
    • Can do all the things you would expect, set bold, italic, set color
    • Can set the text in code, XML, or via a resource defined elsewhere res/values/strings.xml (@string/hello)
  • Button
    • Derives from TextView
    • Using 1.6 or better now can set the associated method an additional way to the OnClickListener
    • In the XML attributes for the button add android:onClick=“functionName”
  • Images
    • ImageView, ImageButton
    • In main.xml need to include android:src attribute
    • supports common still-image formats PNG, JPG, and GIF
  • EditText
    • Derives from TextView
    • Typical Edit box properties
      • Also items like autoText, digits, singleLine
  • Checkbox
    • Derives from CompoundButton which derives from TextView
    • Typical checkbox properties, isChecked, setChecked, toggle
    • Can wire up a handler right from the XML just like the button or set up the OnCheckedChangeListener
  • RadioButton
    • Derives from CompoundButton which derives from TextView
    • Place inside a RadioGroup
    • Typical properties, check, clearcheck, getCheckedRadioButtonId
  • Useful functions
    • setEnabled, isEnabled, requestFocus, isFocused, getParent, findViewById
    • android:autoText - control if the field should provide automatic spelling assistance
    • android:capitalize - control if the field should automatically capitalize the first letter of entered text (e.g., first name, city)
    • android:digits - configure the field to accept only certain digits
    • android:singleLine - control if the field is for single-line input or multiple-line input
  • Good Resource
Use your IDE to try to replicate what I have depicted below.  Be creative and use your own names/labels.
image



image


image

Some sample code for you
public class MyWidget101 extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }// end onCreate

    // This is the method wired to my button in main.xml    public void simpleBtnClick(View theButton)
    {
        Toast.makeText( getApplicationContext(),
                        "Dude, you clicked my button.",
                        Toast.LENGTH_SHORT  ).show();
    }


    // This is the method wired to my checkbox in main.xml    public void simpleCheckbox(View theCheckBox)
    {
        // cast the passed in view to a Checkbox        CheckBox chkbox = (CheckBox)theCheckBox;
        // update its text        if( chkbox.isChecked() )
        {
            chkbox.setText("Checked");
        }
        else
        {
            chkbox.setText("Unchecked");
        }
        /* OR we could have done it like this
        if (((CheckBox)theCheckBox).isChecked())
        {
            ((CheckBox)theCheckBox).setText("1Checked");
        }
        else
        {
            ((CheckBox)theCheckBox).setText("2Unchecked");
        }
        */
    }// end simpleCheckbox



    // This is the method wired to my radio buttons in main.xml
    public void simpleRadioButtonClick(View theButton)
    {
        RadioButton rb      = (RadioButton)theButton;
        EditText    editBox = (EditText)findViewByID(R.id.EditText01);
        editBox.setText(rb.getText());
    }
}// end class MyWidget101

No comments:

Post a Comment