Sunday, March 4, 2012

Background Color Changer (Part 1)

In this tutorial, you will learn how to create a simple app in which the user can change the background color by entering the names of certain colors into a text field.
App Specifications:
  • Views:
    • EditText for text input
    • Button for submitting text
  • Colors
    • Red
    • Green
    • Blue
    • Cyan
    • Magenta
    • Yellow
    • Black (Default color, will be set to black when no color keyword is entered)
  • Actions:
    • Button onClick Event:
      • If the text in the EditText matches one of the color keywords, change the background color
      • Otherwise, set the background color to black
Project Setup:

Create a new Android project that meets the following specifications:
  • Project Name: Background Color Changer
  • Application Name: Background Color Changer
  • Package Name: com.example.bgcolorchanger
  • Create Activity: MainActivity
Layout
  1. In the Package Explorer, expand the project and open main.xml from the res/layout folder
  2. Select the TextView with the Hello World message and delete it
  3. Add an EditText to the layout (Found under Text Fields as "abc") by dragging and dropping
  4. Add a Button to the layout
  5. Open the Outline View in Eclipse
  6. Right click on Linear Layout, then click Edit ID, then type "container"
  7. Change the ID of the EditText to "inputField"
  8. Change the ID of the Button to "submitButton'
  9. Click the "main.xml" tab at the bottom of the visual editor to view the layout as an XML file.
  10. Edit the text for the Button so it says "Submit" instead of "Button"
  11. Fixing Warnings:
    1. Put your cursor on the EditText tag with the orange underline
    2. Press Ctrl+1 (Quick Fix) 
    3. Choose the option "Set input type"
    4. Type "text" as the input type
    5. Put your cursor on the orange underline under the Button's text property
    6. Press Ctrl+1
    7. Choose the option "Extract String"
    8. change the reference to "txt_submit_button" and click OK.
If you would like, un the app on an Android device or emulator to see the layout as shown below:

Now that the layout is complete, you are ready to begin coding. See the next part of this tutorial here.

5 comments:

  1. Could you make it so that it takes the text from the input and directly routs it to the color value, instead of requiring each color to be defined

    ReplyDelete
  2. That sounds like a good idea. how would the color be entered? would it be a name or a html color code or both?

    ReplyDelete
  3. I guess that it could be either, but i don't know how perhaps if the color value was set to the getText method

    ReplyDelete
    Replies
    1. I'm not sure what you mean. Java's Color class has a set number of color constants like Color.BLUE and Color.GREEN. I'm not sure if you can select them straight from a string like "blue" or not. I'll look into it and get back to you. do you have any other ideas on this in the meantime?

      Delete
    2. I looked up on how to get a color from a string, and it's much more complicated than we need for this basic tutorial. Even the HTML color codes would be a stretch because it will involve string searching (For splitting #000000 into three hexadecimal integers and checking for errors). I'll keep it in mind for tutorials in the future though.

      Delete