{"id":2167,"date":"2017-01-30T06:34:30","date_gmt":"2017-01-30T11:34:30","guid":{"rendered":"https:\/\/cindypotvin.com\/?p=2167"},"modified":"2017-02-18T16:39:14","modified_gmt":"2017-02-18T21:39:14","slug":"how-to-get-started-with-android-development-layout-managers","status":"publish","type":"post","link":"https:\/\/cindypotvin.com\/how-to-get-started-with-android-development-layout-managers\/","title":{"rendered":"How to get started with Android development – Layout Managers"},"content":{"rendered":"

\"\"At the end of my last article, I left you hanging after describing activities<\/a>, without telling you how to build a UI, so here we go. In the Android SDK, layouts are separate from the Java code that defines how an activity behaves. Using this separation of concern, it’s easier to modify the layout without having to worry about the code.<\/p>\n

Android layouts are in XML, and are located in the res\\layout<\/code> folder. You can have multiple versions of a layout for various orientations, screen size or culture and the Android SDK will choose the right one according to the device. We won’t worry about this right now; for simple layouts one version is enough.<\/p>\n

In this article, I’m going to show you how to use a layout manager to position a widget, called a View<\/code> in the Android SDK, using XML and Java. Next time, I’ll complete this explanation with more details about views themselves, and how they can be controlled in the layout or programmatically.<\/p>\n

There is also a designer tool in Android Studio to help build your layouts. I’ll stick to the code for now since I believe it’s best to understand how things are built manually first, and then you’ll know enough to be able to explore it on your own.<\/p>\n

Your First Layout<\/h3>\n

Here is a simple layout with two TextView<\/code> views, each showing a text string, inside a LinearLayout<\/code> layout manager in the res\\layout\\activity_main.xml<\/code> file:<\/p>\n

\r\n<?xml version="1.0" encoding="utf-8"?>\r\n<LinearLayout\r\n    xmlns:android="http:\/\/schemas.android.com\/apk\/res\/android"\r\n    android:layout_width="match_parent"\r\n    android:layout_height="match_parent"\r\n    android:orientation="horizontal">\r\n\r\n    <TextView\r\n        android:layout_width="100dp"\r\n        android:layout_height="100dp"\r\n        android:text="My first textview"\/>\r\n    <TextView\r\n        android:layout_width="100dp"\r\n        android:layout_height="100dp"\r\n        android:text="My second textview"\/>\r\n<\/LinearLayout>\r\n<\/pre>\n

\"\"<\/p>\n

Each layout has a least one view as a root, but it’s usually a layout manager such as the LinearLayout<\/code>, RelativeLayout<\/code> or ConstraintLayout<\/code> since those views are built to position other views. I used the linear layout in this example since it’s simple to understand: if the property android:orientation<\/code> is set to vertical<\/em>, all the views will be aligned one over the other, and if set to horizontal<\/em> they will be side by side.<\/p>\n

To use your layout with your activity, you must set the layout in the onCreate()<\/code> event of the activity using the setContentView()<\/code> method :<\/p>\n

\r\npublic class MainActivity extends Activity {\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n    }\r\n} \r\n<\/pre>\n

Views and Layout Managers Properties<\/h3>\n

All views, including layout managers, have proprieties that can be added to their XML declaration to control their size and padding. For example, you can specify an android:layout_height<\/code> and android:layout_width<\/code> in dp (density-independent pixels) like the layout above, or use the wrap_content<\/code> and match_parent<\/code> values:<\/p>\n