Device detection mini-app

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:

  1. Create a new Basic Activity project and call it Device Detection. Leave all the other settings at their defaults.
  2. Open the activity_main.xml file in the design tab and delete the default Hello world! TextView.
  3. Drag a Button onto the top of the screen and set its onClick property to detectDevice. We will code this method in a minute.
  4. Drag two TextView widgets onto the layout, one below the other, and set their id properties to txtOrientation and txtResolution respectively.
  5. Check you have a layout that looks something like the following screenshot:


    I have stretched my widgets ...

Get Android Programming for Beginners - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.