Getting Input from User and using it...


Last Updated:

  1. xmiasma

    xmiasma Member This Topic's Starter

    Joined:
    Dec 28, 2009
    Messages:
    13
    Likes Received:
    0
    Whats up guys I just got my droid eris phone (its sweet as hell) but what really got me into the phone was the android OS cuz its open source and im interested in developing an app for it. I've been looking around online for tutorials and found some good ones on th developers.android site about views and what-not.

    Now, one that they give an example of is the Relative View (one that has a textbar and two buttons ok and cancel) they dont do much else with the tutorial besides show in an xml file how to make the buttons and stuff. my questions are now:

    1) writing the code in the main.xml file cannot be the way to go (or is it?) for that kind of stuff
    2) how would you get input from the user like when they type into the text box and hit okay? you would have to handle some kind of event for the ok button being pressed?

    im very familiar with c++ and class's, inheritance, object oriented programming so im not a totaly nub but just unfamiliar with this API...any suggestions for a good place to get some specifics like this? thanks for reading this and the help
     

    Advertisement
  2. eclipsed4utoo

    eclipsed4utoo Well-Known Member

    Joined:
    Nov 12, 2009
    Messages:
    435
    Likes Received:
    27
    The XML files are simply for designing the UI. The actual code is written in the java files.

    When you create a new project, the project wizard will have a "Create Activity" at the bottom. Always fill that in. This will give you a default Activity, which is mainly the "entry" point of an application.

    As for getting input from the user, this code would work.

    Lets say you have named the input box(called an "EditText" view) as "edtName". Your button names are "btnShow" and "btnClose".

    Inside the Activity class, you would have this...

    Code (Text):
    1.  
    2. @Override
    3. public void onCreate(Bundle savedInstanceState){
    4.     super.onCreate(savedInstanceState);
    5.  
    6.     // R.layout.main is the XML file where your UI is defined
    7.     setContentView(R.layout.main);
    8.  
    9.     // this will get an instance of the Show button
    10.     Button showButton = (Button)findViewById(R.id.btnShow);
    11.  
    12.     // this will wire up the click event for the button
    13.     showButton.setOnClickListener(new OnClickListener() {
    14.          
    15.          @Override
    16.          public void onClick(View view){
    17.              // this code will run when the button is clicked.
    18.              EditText nameText = (EditText)findViewById(R.id.edtName);
    19.  
    20.              // this will display a notification with the text from the EditText view
    21.              //    first parameter is the application context, second parameter is
    22.              //    the text you want to display, and the third parameter is the length
    23.              //    of time that you want the message to stay on the screen.
    24.              Toast.makeText(getApplicationContext(), nameText.getText(), Toast.LENGTH_SHORT).show();
    25.          }
    26.     });
    27. }
    28.  
    It's really as simple as that.
     
  3. xmiasma

    xmiasma Member This Topic's Starter

    Joined:
    Dec 28, 2009
    Messages:
    13
    Likes Received:
    0
    thanks eclipsed thats exactly what i was looking for. I do have one other question though with the code you wrote.

    After you call the setOnClickListener() why does it have a bracket that encompasses the onClick function?

    EDIT: nevermind i realized that i was creating my own onClick() and the setOnClickListener creates its own version. thanks again

    Code (Text):
    1.  
    2. showButton.setOnClickListener(new OnClickListener() {
    3.          
    4.          @Override
    5.          public void onClick(View view){
    6.              // this code will run when the button is clicked.
    7.              EditText nameText = (EditText)findViewById(R.id.edtName);
    8.  
    9.              // this will display a notification with the text from the EditText view
    10.              //    first parameter is the application context, second parameter is
    11.              //    the text you want to display, and the third parameter is the length
    12.              //    of time that you want the message to stay on the screen.
    13.              Toast.makeText(getApplicationContext(), nameText.getText(), Toast.LENGTH_SHORT).show();
    14.          }
    15.     });
    16.  
     
  4. xmiasma

    xmiasma Member This Topic's Starter

    Joined:
    Dec 28, 2009
    Messages:
    13
    Likes Received:
    0
    Hmm i used the same code you did and no notification popped up... i read online something about setting the activity to the launch activity or something...do i need to do this?
     
  5. eclipsed4utoo

    eclipsed4utoo Well-Known Member

    Joined:
    Nov 12, 2009
    Messages:
    435
    Likes Received:
    27
    In your AndroidManifest.xml file, you will need this inside of the "application" tag...

    Code (Text):
    1.  
    2.         <activity android:name=".YourActivityClassName"
    3.                   android:label="@string/app_name">
    4.             <intent-filter>
    5.                 <action android:name="android.intent.action.MAIN" />
    6.                 <category android:name="android.intent.category.LAUNCHER" />
    7.             </intent-filter>
    8.         </activity>
    9.  
    This is added by default if you fill in the "Create Activity" in the New Project Wizard.

    Also, your class needs to extend the Activity class.

    Code (Text):
    1.  
    2. public class YourActivityClassName extends Activity {
    3.  
    4. }
    5.  
    This is also done by default by filling in the "Create Activity" in the New Project Wizard.
     
  6. xmiasma

    xmiasma Member This Topic's Starter

    Joined:
    Dec 28, 2009
    Messages:
    13
    Likes Received:
    0
    yeah i had that stuff i forgot to scroll over to see that you call .show() :eek:
     
  7. eclipsed4utoo

    eclipsed4utoo Well-Known Member

    Joined:
    Nov 12, 2009
    Messages:
    435
    Likes Received:
    27
    yeah, that gets everybody.
     

Share This Page

Loading...