Reader small image

You're reading from  Android Programming with Kotlin for Beginners

Product typeBook
Published inApr 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789615401
Edition1st Edition
Languages
Right arrow
Author (1)
John Horton
John Horton
author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton

Right arrow

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. Let's do this by going through the following steps:

  1. Create a new Empty Activity project and call it Device Detection. Leave all the other settings as their defaults.

  2. Open the activity_main.xml file in the Design tab and delete the default Hello world! TextView.

  3. Drag a Button to the top of the screen and set its onClick property to detectDevice. We will code this function 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 that you have a layout that looks something like the following screenshot:

    Note

    I have stretched my widgets (mainly horizontally) and increased the textSize attributes to 24sp to make them clearer on the screen, but this is not required for the app to work correctly.

  6. Click the Infer Constraints...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Android Programming with Kotlin for Beginners
Published in: Apr 2019Publisher: PacktISBN-13: 9781789615401

Author (1)

author image
John Horton

John Horton is a programming and gaming enthusiast based in the UK. He has a passion for writing apps, games, books, and blog articles. He is the founder of Game Code School.
Read more about John Horton