1. Download our Official Android App: Forums for Android!

Apps Button

Discussion in 'Android Development' started by andrewcweaver, Sep 4, 2010.

  1. andrewcweaver

    andrewcweaver Newbie
    Thread Starter
    Rank:
    None
    Points:
    35
    Posts:
    17
    Joined:
    Jan 1, 2010

    Jan 1, 2010
    17
    0
    35
    Shreveport, LA
    I'm new to development, and i'm trying to make a button display the word "Green" and another button to display "Orange"

    I have made the button, and changed the text, but I can't get it do to anything. I have been using this code to attempt to make the button work

    <Button android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="Green!">
    android:gravity="bottom"
    public class MyActivity extends Activity {
    protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.content_layout_id);

    final Button button = (Button) findViewById(R.id.button_id);
    button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    // Perform action on click android:background="@color/green"</Button>

    }
    });
    }
    }

    Please help.
     

    Advertisement

  2. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    Well first of all. You are not supposed to combine the XML code with the java code... The XML code goes in an XML file in the res/layout/ folder, where-as the java code goes in a java file in the src/package/ folder.


    EDIT: I am feeling generous today, so I am gonna help you out:

    res/layout/main.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout
    4.     xmlns:android="http://schemas.android.com/apk/res/android"
    5.     android:id="@+id/LinearLayout01"
    6.     android:orientation="vertical"
    7.     android:layout_width="fill_parent"
    8.     android:layout_height="fill_parent">
    9. <Button android:id="@+id/Button01"
    10.     android:layout_width="wrap_content"
    11.     android:layout_height="wrap_content"
    12.     android:text="Green!"
    13.     android:gravity="bottom"/>
    14. </LinearLayout>
    15.  
    MyActivity.java
    Code (Text):
    1.  
    2. public class MyActivity extends Activity {
    3. Button button;
    4. LinearLayout layout;
    5. protected void onCreate(Bundle icicle) {
    6. super.onCreate(icicle);
    7.  
    8. setContentView(R.layout.main);
    9.  
    10. button = (Button) findViewById(R.id.Button01);
    11. layout = (LinearLayout) findViewById(R.id.LinearLayout01);
    12. button.setOnClickListener(new View.OnClickListener() {
    13.    public void onClick(View v) {
    14.       if(v == button) {
    15.          layout.setBackgroundColor(getResources().getColor(R.color.green); /*must have green defined in the res/values/colors.xml file.*/
    16.       }
    17.    }
    18. });
    19. }
    20. }
    21.  
     
  3. andrewcweaver

    andrewcweaver Newbie
    Thread Starter
    Rank:
    None
    Points:
    35
    Posts:
    17
    Joined:
    Jan 1, 2010

    Jan 1, 2010
    17
    0
    35
    Shreveport, LA
    It's in the main.xml folder.
     
  4. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    Here is what the structure should look similar to:

    [​IMG]

    Also see my edited post above.
     
  5. andrewcweaver

    andrewcweaver Newbie
    Thread Starter
    Rank:
    None
    Points:
    35
    Posts:
    17
    Joined:
    Jan 1, 2010

    Jan 1, 2010
    17
    0
    35
    Shreveport, LA
    Crashed my app. Hmm... Will show up in the layout, but not on the emulator.
     
  6. jonbonazza

    jonbonazza Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    post the logcat.
     

Share This Page

Loading...