In the previous section, we've learnt a lot about variables of different types. Let's bring that back and apply it inside our Android app.
We learnt about string variables, which can hold text. So let's get text input from the user and use it. To get text input, we use a View called an EditText. Add this to your layout:
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/userInput" />
This is a view that lets users type in text using the keyboard. As usual, you can have other attributes positioning it differently if you want. The important thing is that you give it an ID so that our code can get a reference to it.
Run it and see that the user can type into the EditText.
Go ahead and add a button and onClick method like we've learnt before.
In MainActivity.java, we would like to write code that gets the contents of this EditText. So the first step is something we've done before: We need to get a reference to this EditText.
EditText input = (EditText) findViewById(R.id.userInput);
This is similar to what we did with TextView a few sections ago. Also, now that we've learnt about variables, more of this should look familiar:
- The equals sign is assignment.
- On the left, we've created a variable. The type of this variable is EditText. Like int or String, EditText is also a type.
- On the right, we get the reference to userInput and assign it to our variable named input. Before we do it, however, we are casting so that the type is EditText. We've learnt about casting as well. findViewById just gives us something of type View, and we are casting it to something of type EditText. This is similar to casting an int to a double.
Next, add this:
String text;
text = input.getText().toString();
The first line is something we know: we are defining a variable. In the second line, we are assigning to that variable. Let's look at the right side of the equals sign closer. input is the name of our EditText variable. getText() is a function that pulls out the contents of an EditText. toString() is a function that turns that into a String. We chain functions like this using a dot. We will see dot syntax more when we learn about functions.
We now have a variable that has the user's input inside of it. Now print it to the console and run the app! The final version should look something like this:
EditText input = (EditText) findViewById(R.id.userInput);
String text;
text = input.getText().toString();
Log.d("MainActivity", text);
We have successfully gotten the user's input and printed it to the console. Now let's try doing a bit more with it.
Try: When the user inputs their name, greet them by name. ie, say "Hello Bob!". Tip: You're going to have combine strings to do this.
Try: Print the greeting on the screen on a TextView, rather than the console.
Try: Get multiple details from the user, such as Name, Age, City. Print a summary of that information to the screen. For example: "Hello Bob! You are 18, and you are from Hong Kong." Tip: This string can be long, so figure out how to have a TextView display long text.
We just tried using String variables, but let's trying using ints as well.
Try: Ask for the user's age, and print out their year of birth.
Tip: To do this, you need to :
Take the user's input (which is a string).
Convert it to an int.
Do math to figure out the year of birth.
Do a search on Google to see if you can figure out how to convert a string to an int.