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

Apps EventHandling

Discussion in 'Android Development' started by renegadeandy, Jun 6, 2009.

  1. renegadeandy

    renegadeandy Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    16
    Joined:
    Jun 6, 2009

    Jun 6, 2009
    16
    0
    15
    Hey!

    Right i am starting to make my app and am happily moving through the tutorials on the android developer site.

    Gota question.

    Lets say I have an EditText box:

    Code (Text):
    1.  
    2. <EditText android:text="Insert Team Name"
    3.                      android:id="@+id/teamInput"
    4.                      android:layout_width="fill_parent"
    5.                      android:layout_height="wrap_content"
    6.                      android:layout_weight="1">
    7.                 </EditText>
    How do I get it to call some code once a user has finished inputting it - or is the idea that you have a search button, which retrieves the text on click - either way - can you please paste a bit of code, or a class, where I can create a new object be it a button or a EditText item where I can retrieve the contents to do something with!

    Cheers

    Andy
     

    Advertisement

  2. devolio

    devolio Android Enthusiast
    Rank:
    None
    Points:
    43
    Posts:
    339
    Joined:
    Oct 30, 2008

    Oct 30, 2008
    339
    37
    43
    Developer
    Michigan
    Assuming this bit of XML as your layout...

    Code (Text):
    1. <EditText android:text="Insert Team Name"
    2.                      android:id="@+id/teamInput"
    3.                      android:layout_width="fill_parent"
    4.                      android:layout_height="wrap_content"
    5.                      android:layout_weight="1">
    6.                 </EditText>
    Code (Text):
    1. EditText teamInput = (EditText) findViewById(R.id.teamInput);
    2. String tiVal = teamInput.getText().toString();
     
  3. renegadeandy

    renegadeandy Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    16
    Joined:
    Jun 6, 2009

    Jun 6, 2009
    16
    0
    15
    Wow ok - farely easy then - and what about a button - so how do i tie an xml button into java code?!
     
  4. devolio

    devolio Android Enthusiast
    Rank:
    None
    Points:
    43
    Posts:
    339
    Joined:
    Oct 30, 2008

    Oct 30, 2008
    339
    37
    43
    Developer
    Michigan
    This page has a decent sample of using UI elements. Handling UI Events | Android Developers

    Also, in your SDK, there's samples that have tons of good, fast code you can use to teach yourself as well.
     
  5. renegadeandy

    renegadeandy Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    16
    Joined:
    Jun 6, 2009

    Jun 6, 2009
    16
    0
    15
    Alright cool - trying to get something to work - do standard System.out.println("wowow"); debug statements work - because I am finding it hard to see where my code is dieing :

    Ok so I have this :

    Code (Text):
    1.  
    2.  OnClickListener searchTeamListener = new OnClickListener() {
    3.                    public void onClick(View v) {
    4.                      TextView txt = (TextView) v.findViewById(R.id.textview3);
    5.                      EditText search = (EditText) findViewById(R.id.searchTeam);
    6.                      System.out.println("woof");
    7.                      txt.setText(search.getText());
    8.                    }
    9.                };
    10.                
    11.                    // Capture our button from layout
    12.                    Button button = (Button)findViewById(R.id.searchTeam);
    13.                    // Register the onClick listener with the implementation above
    14.                    button.setOnClickListener(searchTeamListener);
    15.  
    However I cannot detect if it gets run - when I hit the button, it crashes the app on the emulator! AAAAAAAAAAAAAAAAA!:eek:
     
  6. GeraldNunn

    GeraldNunn Lurker
    Rank:
    None
    Points:
    6
    Posts:
    6
    Joined:
    Jun 6, 2009

    Jun 6, 2009
    6
    1
    6
    Use the static methods in the Log class to output debug statements, you can view them in the Eclipse LogCat view. Here is a very simple example:

    public class MyClass {
    private static final String "MyApp/MyClass";

    public void someMethod {
    Log.d(TAG, "This is a debugging statement");
    }
     
  7. cp123127

    cp123127 Lurker
    Rank:
    None
    Points:
    5
    Posts:
    6
    Joined:
    Jun 26, 2009

    Jun 26, 2009
    6
    0
    5
    I don't know if this will help but try putting
    Code (Text):
    1. TextView txt = (TextView) v.findViewById(R.id.textview3);
    2. EditText search = (EditText) findViewById(R.id.searchTeam);
    as method level fields and giving them the final modifier so that the onClick can grab them.
    And System.out.println won't work. Use Log.d instead.
     
  8. sarandasl

    sarandasl Lurker
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Nov 3, 2009

    Nov 3, 2009
    2
    0
    5

Share This Page

Loading...