Lesson 8. Working with screen elements from the code

Lesson 8. Working with screen elements from the code


In this lesson we:

– learn how to handle code on View-elements on the screen and change their properties

Let’s create a project:

Project name: P0081_ViewById
Build Target: Android 2.3.3
Application name: ViewById
Package name: ru.startandroid.develop.viewbyid
Create Activity: MainActivity

To access the screen element from the code, we need it ID. It is prescribed or in Properties, Or d layout filesas you see fit. There is a clear format for ID – @ + Id / name, where + means it is a new resource and should be added to R.java class, if it does not already exist.

Let’s open it main.xml, for TextView let’s say ID = @ + id / myText and save

now open R.java and we see that for the class id a constant appeared myText. That is, to address it, it is necessary to write R.id.myText.

It is associated with a TextView element and we can use it to access the element programmatically. To do this, we need the findViewById method. It returns the View ID. Let’s write a call to this method. Let me remind you that while we are writing our code in method onCreate. This is the method that is called when creating an Activity. If it’s suddenly unclear where to write, you can peek at the end of the lesson, that’s where I posted the code.

Open MainActivity.java after the method call string setContentView write:

View myTextView = findViewById(R.id.myText);

If View is highlighted in red, then most likely this class is added to the import section. Press CTRL + SHIFT + O to automatically import again.

now myTextView (View Type) is our on-screen TextView. But the View type is the ancestor of TextView (and other View elements). And it does not suit us if we want to perform operations corresponding to TextView. So we need to convert View to TextView. Let’s change our code to the following:

TextView myTextView = (TextView) findViewById(R.id.myText);

now myTextView has type TextView, And the result of the findViewById method will be converted from View to TextView. Now we can apply the TextView class methods to myTextView. Take the setText method as an example. The text = is now displayed Hello World, MainActivity!. We will programmatically change it to New text and TextView

myTextView.setText("New text in TextView");

Save, run (CTRL + F11) and see that the text has changed

Add the button (Button) to the screen, Id = @ + id / myBtn, leave the text as default. Save – CTRL + SHIFT + S (if not saved, R.java may not display an ID).

We write the code:

Button myBtn = (Button) findViewById(R.id.myBtn);

Note that I have the same object name and ID

They do not interfere with each other and do so even more logically. This is at your discretion. Yes, we found the button, now let’s change its text:

myBtn.setText("My button");

Let’s start the program. The text on the button has changed, you can press the button, but nothing will happen. Because we did not specify anywhere what to do when pressed. Let’s deal with this in the next lesson. In the meantime, let’s make the button inactive.

myBtn.setEnabled(false);

We changed the parameter Enabled. You can now press the button. Let’s save, run and make sure.

Add CheckBox, id = @ + id / myChb. By default, the checkmark is not worth it. Let’s put it programmatically, using the setChecked method, which changes the Checked parameter.

CheckBox myChb = (CheckBox) findViewById(R.id.myChb);
myChb.setChecked(true);

When we launch the application, we see that the code has worked.

As you can see, everything is easy. we use the method findViewByIdTo by ID get an object matching some View-element (Button, TextView, CheckBox) and then call the required object methods (setText, setEnabled, setChecked).

The result should be:

package ru.startandroid.develop.viewbyid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        TextView myTextView = (TextView) findViewById(R.id.myText);
        myTextView.setText("New text in TextView");
        
        Button myBtn = (Button) findViewById(R.id.myBtn);
        myBtn.setText("My button");
        myBtn.setEnabled(false);
        
        CheckBox myChb = (CheckBox) findViewById(R.id.myChb);
        myChb.setChecked(true);
    }
}

In the next lesson:

– Learn how to handle the push of a button




Discuss in the forum [257 replies]
Related Posts
Lesson 190. Notifications. channels

Android Oreo (API 26) has the ability to create message channels. In this lesson we will understand how to Read more

Lesson 189. Notifications. message grouping

Android 7 (API 24) has the ability to group messages. Even if you don't explicitly implement it, the system Read more

Lesson 188. Notifications. custom messages

Android enables us to create a layout for messages ourselves. Consider a simple example: layout / notification.xml Height 64dp Read more

Lesson 187. Notifications. Action buttons. Reply.

Android 4.1 has the ability to add buttons to messages. This is done using the addAction method. Intent deleteIntent Read more

Leave a Comment