Using the Android Studio layout editor

Android Studio provides a layout editor, which gives you the ability to build your layouts by dragging widgets into the visual editor. This will auto-generate the XML code for your UI.  

Open the content_main.xml file.

Make sure the Design tab at the bottom of the screen is selected, as shown in the following screenshot:

To add a component to your layout, you just drag the item from the Palette on the left side of the screen. To find a component, either scroll through the items on the Palette, or click on the Palette search icon and search for the item you need.

If the Palette is not showing on your screen, select View | Tool Windows | Palette to display it.                                         

Go ahead and add a ListView to your view. When a view is selected, its attributes are displayed in the XML Attributes editor on the right side of the screen. The Attributes editor allows you to view and edit the attributes of the selected component. Go ahead and make the following changes: 

  • Set the ID as list_view
  • Change both the layout_width and layout_height attributes to match_parent 
If the Attributes editor is not showing; select View | Tool Windows | Attributes to display it.                                                             

Now, select Text at the bottom of the editor window to view the generated XML code. You'll notice that the XML code now has a ListView placed within the ConstraintLayout: 

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.packtpub.eunice.todolist.MainActivity"
tools:showIn="@layout/activity_main">

<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="4dp"
tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
A layout always has a root element. In the preceding code, ConstraintLayout is the root element.

Instead of using the layout editor, you could have written the previous code yourself. The choice between using the layout editor or writing the XML code is up to you. You can use the option that you're most comfortable with. We'll continue to make additions to the UI as we go along.

Now, build and run your code. as shown in the following screenshot:

As you can see, the app currently doesn't have much to it. Let's go ahead and add a little more flesh to it. 

Since we'll use the FloatingActionButton as the button the user uses to add a new item to their to-do list, we need to change its icon to one that makes its purpose quite clear.

Open the activity_main.xml file: 

One of the attributes of the android.support.design.widget.FloatingActionButton is app:srcCompat. This is used to specify the icon for the FloatingActionButton. Change its value from @android:drawable/ic_dialog_email to @android:drawable/ic_input_add.

Build and run again. The FloatingActionButton at the bottom now looks like an Add icon, as shown in the following screenshot: 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset