Following on from the first page of our app. We are now going to make some of the features work!
1. Go to MyActivity in the src folder.
2. The first thing you need to look at is that it extends activity.
You are saying to java you want to use some of the predefined methods in this class one of which is onCreate.
In every Java file you are most likely going to have an onCreate method, therefore they always have to extend Activity.
Only Java classes should be extended.
3. You should also implement an onClickListener. In doing so you are saying to Java that there is going to some form of button on this page.
To do so it should look like this:
Dont worry about if it comes up red this is because you haven’t added in the method yet.
4. Just bellow the class name you should declare a button.
Declare a private Button submit;
For each button you have on the page you should also have a button in the Java code. Try to ensure that the variables have applicable names it will help you later on when you have a larger project.
5. Create a variable called TextView. I am creating a TextView because when the user click submit it is going to change! to let you see it actually doing something.
private TextView changing;
6. Create a variable called EditText. As you are having the user enter some information into it. It needs to be within the Java also.
private EditText editing;
I’ve chosen to call all of the variables private as they are not going to be used outside of this class. Therefore it is best to have them restricted.
7. The onCreate method.
Within the on create method you should get it to display which page the code is for. In this case intelliJ has already set ours up.
8. Within the method you should also link the Button, TextView and EditText up to there id’s in the main page.
It should look something like this:
9. Once you have initialised the variables you should then add on onClickListener to the Button. To do so:
In doing so Java will now be watching to see when the user clicks on that button.
10. You should now create the onClick method. This is where all the changes till the page should go. For example we want to change the TextView to the text from the EditText this is where it should go.
to create the method:
public void onClick(View view)
You then want to add in a statement which will enable you to change the TextView to the EditTexts which the user has entered.