The best way to learn about detecting and responding to devices and their varying attributes (screens, orientations, and so on) is to make a simple app. Let's do this by going through the following steps:
Create a new Empty Activity project and call it
Device Detection
. Leave all the other settings as their defaults.Open the
activity_main.xml
file in the Design tab and delete the default Hello world!TextView
.Drag a Button to the top of the screen and set its onClick property to
detectDevice
. We will code this function in a minute.Drag two TextView widgets onto the layout, one below the other, and set their id properties to
txtOrientation
andtxtResolution
, respectively.Check that you have a layout that looks something like the following screenshot:
Click the Infer Constraints...