Chapter 11: Designing Your First App in Android Studio
Currently, your app does not have anything on it except for the default TextView
widget/object that contains the text, “Hello World!” Now, you will be taught
how to add some elements on your main activity. But before that, you should
know more details about View and Viewgroup objects.
View, as mentioned a while ago, is a typical element in an Android app. Most
developers and users call them as widgets. Some of the View elements that you
can add on your app are text fields, buttons, sliders, and etcetera.
On the other hand, Viewgroups are containers for View elements. They serve as
the parent or root for View elements or as child for other Viewgroup objects.
Keep in mind that View elements are stuck with being a child of a Viewgroup
object.
Viewgroups house or group View objects together. Viewgroups make it easier to
handle and organize the View elements in your activities. They are invisible to
the users.
A Viewgroup can contain View objects such as text fields, buttons, and even
another Viewgroup object. If you are familiar with HTML, Viewgroups are like
div elements. They are invisible, but they do hold other elements within them.
Go and check your project’s component tree. As you can see, under Device
Screen, you will see two other items. The first one is RelativeLayout. The
second one is TextView. The RelativeLayout is a Viewgroup. And inside that
Viewgroup is TextView, which is a View object.
In the Layout View window, you can see the TextView object that says, “Hello
World!” On the other hand, you will not see any element other than that. When
you click on the TextView item on the Component Tree, the Hello World text
will be highlighted. But if you click on RelativeLayout, nothing will happen
except for the deselection of any selected View element on the preview screen.
You can add any widget that you want for your app. When you add a view
element, make sure that you check the changes on your activity’s xml file.
Familiarize yourself with the attributes and the name of the elements when they
are placed in an activity.
By the way, whenever you place a widget on the Design view, you might have
noticed that there are some green lines and arrows in the widgets. Those are
guiding lines. Those lines will help you set the location of your widgets relative
to the other elements on your app’s screen.
On the other hand, there are other layout modes available in Android. Each view
has different behaviors and can affect the way Android places the elements that
you place in the layout.
Anyway, you can run and test that app. As long as you did not change anything
else, the app can be opened on your device or with an Android emulator.
Do'stlaringiz bilan baham: |