Lesson 5. Activity Layout File. XML representation. Change screen orientation.

Lesson 5. Activity Layout File. XML representation. Change screen orientation.


In the last lesson, we learned that Activity reads the layout file and displays what is configured in it. Now let’s find out where Activity knows which layout file to read.

The lesson was updated on 2/02/2015

Let’s create an application for this lesson. To do this, you need to create a module in the Android lessons project. We created this project in the past lessons. If you have not yet learned how to create modules, you can revisit Lessons 3 and 4.

And let’s take a moment to look at it. The following lessons were written a long time ago and were written under the Eclipse development environment. Accordingly, all lessons contain information for creating a project in Eclipse.

But Eclipse is different from Android Studio in organizing projects / modules. And now we will understand how to use project creation information in Eclipse when creating modules in Android Studio.

The project creation info for Eclipse looks like this:

Project name: P0051_LayoutFiles
Build Target: Android 2.2
Application name: LayoutFiles
Package name: ru.startandroid.develop.LayoutFiles
Create Activity: MainActivity

Typically, this is the text at the beginning of each lesson.

And to create a module in Android Studio, we need information like this

Application / Library name: LayoutFiles
Module name: p0051layoutfiles
Package name: ru.startandroid.p0051layoutfiles

How to get new from old? Consider the example of this lesson. Let’s take values ​​for Eclipse and replace them in Android Studio.

for the field Application/Library name you take value Application name, Without any changes. That is LayoutFiles.

for Module name you can take values Project name, But capitalization should be lowercase and lower underscores should be deleted. That is, with P0051_LayoutFiles we get p0051layoutfiles.

Package name is it ru.startandroid. plc just received Module nameThat is, startartroid.p0051layoutfiles

Use this diagram in all of the following lessons to create modules.

Also, information for Eclipse has a field Create Activity. We will use it when creating the module, when we specify the name Activity, in the field Activity Name

You need to substitute a value with Create Activity. This is usually always MainActivity.

Remember these instructions and apply each lesson to create modules.

Let’s go back to the lesson.

In development, each Activity is mapped to the java class (the heir to the android.app.Activity class). When running the program, when the system must show Activity and continue to work with it, it will call methods of this class. And what we do in these methods depends on the behavior of Activity.

When creating the module, we indicated that we needed to create an Activity named MainActivity

We asked to create an Activity, and the development environment created a class for us (in the future we will learn to create them ourselves).

Let’s look at this class: double-click the file: java en startandroid p0051layoutfiles MainActivity.java

We look at the java code. We are interested in the method onCreate – it is invoked when an application creates and displays Activity (other methods are not yet addressed). Let’s see the onCreate implementation code.

The first line:

super.onCreate(savedInstanceState);

it is a call to the parent class method that performs the necessary procedures, we do not touch it.

We are now very interested in the following line:

setContentView(R.layout.activity_main);

SetContentView (int) method – sets the Activity content of the layout file. But as an argument, we do not specify the path to the layout file (res / layout / activity_main.xml) but the constant, which is the file ID. This constant is generated automatically in the R.java file, which we will not touch yet. This class will store the generated ID for all project resources (from the res / * folder) so that we can access them. The names of these ID constants match the names of the resource files (without extensions).

The res / layout / activity_main.xml file was created by the development environment along with Activity. Its name was required on the same screen as the name Activity (screen above).

In the following lessons, this file is usually called main.xml, not activity_main.xml

Double-click res / layout / activity_main.xml

let’s see what’s out there

Let’s launch the application and see what it shows us

That’s right – Activity mirrored what activity_main.xml says.

Let’s try to display the contents of another file. Let’s create another layout file, for example myscreen.xml. To do this, select the res / layout folder in our module and right-click on it. Select New> Layout resource file from the menu. For hotkey lovers, there is a more convenient way: with the res / layout folder selected, press ALT + Insert, and there is already Enter at Layout resource file.

the window opens

Enter the name of the file myscreen, the rest until we change, click OK.

A new myscreen.xml file should appear in the layout folder

This new layout file should open immediately for editing. Add a Plain TextView element from the list to the left and use Properties to change its text to: “new layout file myscreen for activity”.

We must save (CTRL + S).

When creating a new myscreen layout file, Wednesday added a new constant for this file, R.layout.myscreen, to R.java. And now we will be able to point to this new layout file through this constant.

We configure Activity to use the new myscreen.xml file, not the activity_main.xml that was originally. Open MainActivity.java and override the setContentView method argument. Replace “R.layout.activity_main” with “R.layout.myscreen” (ID of the new layout file). It should turn out like this:

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myscreen);
  }

Save the code (CTRL + S) and run the application (SHIFT + F10).

Now we will be asked to confirm that we want to run the application on the included emulator.

To prevent it from being asked every time, turn on the Use same device for future launches checkbox and click OK.

application started

We see that it now displays content from myscreen.xml because we explicitly specified it in the setContentView method that is executed when creating (onCreate) Activity

Layout file in XML format

When you open an activity_main or myscreen file in Android Studio layout, you can see its visual representation. That is, some preview of what it will look like on the screen. Below you can see two tabs – Design and Text. Click the Text tab

We see a fairly readable xml description of all View our layout file. The names of the xml elements are the classes of the View elements, the xml attributes are the parameters of the View elements, that is, all those parameters that we change through the Properties tab. You can also make changes right here and the changes will be displayed in the Design tab. For example, let’s change the text in TextView. Instead of “new layout file myscreen for activity”, we write “some new text”

Save. Open Design and see changes.

Usually, the authors of the textbooks give the contents of layout-files exactly in xml form. This is convenient – you can simply copy a snippet and use it, and you don’t have to manually add View items, run around Properties, and customize everything with your hands. I will do the same in my projects.

Layout file when changing screen orientation

By default, we adjust the layout file to the vertical orientation of the screen. But what if we turned our smartphone back and the horizontal orientation turned on? Let’s watch.

Let’s change myscreen.xml. Add a vertical row of buttons and change the caption.

xml code (you can copy and replace the contents of your myscreen layout file in the Text tab):



    
    
    
        
        
        
        
    

Note – I added a vertical LinearLayout and put 4 buttons in it. Let’s discuss this in more detail in the next lesson.

Save the file, launch the application.

In vertical orientation, everything is approx.

Click in the CTRL + F12 emulator, the orientation has changed to horizontal and our buttons no longer fit into the screen.

That is, we need another layout file, which would be sharpened to a horizontal orientation and in our case would bring the buttons horizontally.

But how to let Activity know that it has to use one layout file in the vertical orientation and another in the horizontal orientation? The creators of Android have already thought about this for us. We have the ability to create a layout file that will be used by the application when the device is horizontally oriented.

Creating such a file is almost the same as creating a regular layout file. We get to the res / layout folder and create a new Layout resource file. The file name is the same as myscreen. It is necessary to add a specifier that will allow the application to understand that this layout file should be used in a horizontal orientation. To do this, find Orientation in the list of specifiers at the bottom left

And press the right arrow button. Thus, we have included the use of an orientation specifier. We need to point out that we are interested in a horizontal orientation: Landscape. Select this value from the list.

Note that the value of the Directory name field has changed

By specifying the specifier, we have specified that our new layout file will be created in the res / layout-land folder, not res / layout as usual. That is, the -land specifier indicates that the layout files in this folder will be used in the horizontal orientation of the device.

click OK

Let’s look at the structure of the module

We see that we now have two myscreen files: normal and land. You can see it in the folder structure. To do this, change the project view from Android to Project from above

And you will see that the module now has res / layout and res / layout-land folders. And both contain a myscreen file.

Double-click the res / layout-land / myscreen file and change its contents to the following xml code:



    
    
    
        
        
        
        
    

The Design tab will show the following:

In this layout file, we position the buttons horizontally so that they are properly displayed in a horizontal orientation.

Note the file name at the top. There is a land specifier there so you always understand which of the two myscreen you are currently editing.

Let’s start the program.

Activity reads the layout file we specified in the setContentView method, that is, myscreen.xml, and displays its contents. In doing so, it takes into account the orientation of the device, and in the case of horizontal orientation takes myscreen from the res / layout-land folder (if it exists, of course).

Let’s switch the CTRL + F12 orientation.

Activity understands that it is in vertical orientation and uses the myscreen layout file from the res / layout folder.

P.S.

A little more about the lessons. Next, in almost every lesson, the main layout file will be called main.xml. Don’t be discouraged, just remember that your primary file is activity_main.xml.




Discuss in the forum [367 replies]

Leave a Comment