Home and Learn: Android Course


Designing an XML layout for landscape view

Now that we have a portrait layout designed, let's tackle the landscape layout.

Load up your activity_main.xml file, the one in the layout-land folder. You'll know when you have the right one because it looks like a wide rectangle in landscape mode. Portrait view is a long rectangle.

Now drag an ImageView from the Palette onto the Component Tree, or onto the white layout canvas. When you get the Pick a Resources dialogue box up, select your bridge image:

Pick a Resources dialogue box with a Drawable selected in Android Studio.

Click OK to add the image. The ImageView on the white layout might be in the upper left corner. Drag it out and down a bit to give yourself some room to add the constraints.

 

Have a look at the Attributes area on the right. Set the top constrain to 8 and the left one to 16. Set the bottom one to 8:

Attributes area for an ImageView in Android Studio 3

You can set a bigger left margin, if you like. We've set ours to 16:

An Android activity showing an ImageView with constraints added.

For the contentDescription attribute, set that to your tower_bridge_landscape string resource:

A string resource being added for the contentDescription property

Now we can add the TextView.

 

Android Landscape TextView

Now that we have an ImageView on the left, we need a TextView to go alongside it.

From the Palette, drag a TextView widget onto your Component Tree (or onto the layout). If it ends up in the top left corner, drag it somewhere in the middle:

To add the constraint now, drag from the right middle of the TextView to the right edge of the screen. Now drag from the left middle of the TextView to the right edge of the ImageView. Drag from the top middle to the top of the screen to add a top contraint for your TextView. Add a constraint from the bottom of the TextView to the bottom of the screen:

Constraints added to the TextView

Android TextView Constraints

With the TextView still selected, click the Pick a Resource button:

The Pick a Resource button highlighted in Android Studio 3

From the Pick a Resources dialogue box, select your tower_bridge string:

A string resource being selected from the Resources dialogue box in Android Studio

Click OK and your layout may look like this:

Landscape layout with ImageView and TextView aligned

You can add adjust the left and right margins.

In the Attributes area, change the left margin for the TextView to 128, and the right margin to 128, as well. For the layout_width, change it to 439dp: (But you may need to play around with these values.)

Attributes of an Android TextView.

You can try it out, now. Run your app. Rotate the device so that it's in landscape view. You should see your new design displayed:

Android app running in landscape mode.

The new display is being pulled from the layout-land folder and is the activity_main XML file we created there. Android is switching between the two all by itself!

In the next section, we'll move on and tackle Action Bars and Toolbars.

Back to the Android Contents Page

 


Email us: enquiry at homeandlearn.co.uk