• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Getting Input from User and using it...

xmiasma

Newbie
Dec 28, 2009
13
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
 
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:
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    // R.layout.main is the XML file where your UI is defined
    setContentView(R.layout.main);

    // this will get an instance of the Show button
    Button showButton = (Button)findViewById(R.id.btnShow);

    // this will wire up the click event for the button
    showButton.setOnClickListener(new OnClickListener() {
          
         @Override
         public void onClick(View view){
             // this code will run when the button is clicked.
             EditText nameText = (EditText)findViewById(R.id.edtName);

             // this will display a notification with the text from the EditText view
             //    first parameter is the application context, second parameter is
             //    the text you want to display, and the third parameter is the length
             //    of time that you want the message to stay on the screen. 
             Toast.makeText(getApplicationContext(), nameText.getText(), Toast.LENGTH_SHORT).show();
         }
    });
}

It's really as simple as that.
 
Upvote 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:
showButton.setOnClickListener(new OnClickListener() {
          
         @Override
         public void onClick(View view){
             // this code will run when the button is clicked.
             EditText nameText = (EditText)findViewById(R.id.edtName);

             // this will display a notification with the text from the EditText view
             //    first parameter is the application context, second parameter is
             //    the text you want to display, and the third parameter is the length
             //    of time that you want the message to stay on the screen. 
             Toast.makeText(getApplicationContext(), nameText.getText(), Toast.LENGTH_SHORT).show();
         }
    });
 
Upvote 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?

In your AndroidManifest.xml file, you will need this inside of the "application" tag...

Code:
        <activity android:name=".YourActivityClassName"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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:
public class YourActivityClassName extends Activity {

}

This is also done by default by filling in the "Create Activity" in the New Project Wizard.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones