Creating your first working page with Java!

Following on from the first page of our app. We are now going to make some of the features work!

Image

src folder

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. 

Image

Extending the class Activity

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:

implements View.OnClickListener Image

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.

Image

Button variable

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.

Image

TextView variable

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.

Image

ExitText variable

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.

setContentView(R.layout.main);

8. Within the method you should also link the Button, TextView and EditText up to there id’s in the main page.

Image

Initialising variables and linking them up with the appropriate id’s

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:

submit.setOnClickListener(this);

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:

@Override

public void onClick(View view)

{

}//onClick

You then want to add in a statement which will enable you to change the TextView to the EditTexts which the user has entered. 

Place:

changing.setText(editing.getText());

Complete Code: 

Image

Completed Code

One thought on “Creating your first working page with Java!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s