In this step you will learn how to make an action happen whenver you press a button.
Step 1: Enable Auto Imports
This allows Android Studio to automatically import any classes that are needed by the Java code.
Open the Settings Editor in Android Studio by going to File > New Project Setup > Settings for New Projects.
2, Go to Auto Imports. Underneath Java, ensure Add Unambiguous Imports on the fly is checked.
3. Press OK
Step 2. Make the Count Button Update the Number on the Screen.
In this step you will make the number on the screen increase by 1 whenever you press the Count button.
In fragment_first.xml make sure you notice the id for the Textview. The code should look like android:id="@+id/textview_first"
2. In FirstFragment.java create a new private method countMe() that takes a single parameter, View, and returns nothing. When finished this method will be invoked when the Count button is clicked. The method header should be the following:
private void countMe(View view) {
}
3. Get the value of showCountTextView which will be defined in the next step by implementing the following code.
String countString = showCountTextView.getText().toString();
4.Convert the value of countString from a String to an Integer and then increment.
Integer count = Integer.parseInt(countString);
count++;
5. Display the new value in the TextView:
showCountTextView.setText(count.toString());
6. The countMe method in its entirely should look like the following:
private void countMe(View view) {
// Get the value of the text view
String countString = showCountTextView.getText().toString();
// Convert value to a number and increment it
Integer count = Integer.parseInt(countString);
count++;
// Display the new value in the text view.
showCountTextView.setText(count.toString());
}
7. Now call the countMe method inside of onClick method.
view.findViewById(R.id.count_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
countMe(view);
}
});
8. Also change the random_button on click listener to
view.findViewById(R.id.random_button).setOnClickListener(new View.OnClickListener()
Step 4: Cache the TextView for Repeated Use
Since the method countMe() is called every time a button is clicked and findViewById() is relatively time consuming to call, it is better to call findViewById() and store it outside of the countMe() function.
In the FirstFragment class add a variable named showCountTextView of type TextView before any methods.
TextView showCountTextView;
2. In onCreateView(), calling findViewById() gets the TextView that shows the count. Inside of onCreateView(), make a new variable fragmentFirstLayout that is equal to the layout view.
View fragmentFirstLayout = inflater.inflate(R.layout.fragment_first, container, false);
3. Call findViewById() on the new variable you just made, fragmentFirstLayout, and specify the id of the view to find, textview_first. Store the value in showCountTextView.
showCountTextView = fragmentFirstLayout.findViewById(R.id.textView);
4. return fragmentFirstLayout from onCreateView();
return fragmentFirstLayout;
The whole method should look like this:
TextView showCountTextView;
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
View fragmentFirstLayout = inflater.inflate(R.layout.fragment_first, container, false);
// Get the count text view
showCountTextView = fragmentFirstLayout.findViewById(R.id.textView);
return fragmentFirstLayout;
}
5. Run the App and press Count and watch the TextView update.