Wednesday, March 11, 2020

Android View Example

The basic components for building a user interface (UI) in Android are View, ViewGroup and Layout. From those basic components that combine them together to create complex types of UIs for Android applications
To create a user interface in the Android application, we build from a combination of components named View, ViewGroup, Layout.
Interface components are built from Android's View base class (android.view.View), these components are available as diverse as Button, TextView, CheckBox ... we all call it View. The diagram of the View is described in the diagram view in android as shown below.
diagram view in android

The view represents a rectangle in which it displays certain information to the user, and the user can interact with the view. But the basic types of View to learn first are: TextView, ImageView, Button Android, ImageButton, EditText
Create a View using Java Code and XML
Java Code
View objects such as TextView, Button, ImageView, LinearLayout, ConstraintLayout ... can all be created and set with regular Java code. Objects created with the new operator with the View constructor have a Context (Activity is a Context). For example, in OnCreate, create TextView object:
TextView textview = new TextView(this);
Several properties / methods are available in all views
Whether the View is Button, FrameLayout ... are inherited from View, so it has common methods that you can use such as:
ID of View
Each View you can assign it an identifier as an integer using the setId (int) method, the ID used to find the View in the interface system using the findViewById (id) method of Activity or a certain ViewGroup.
int textview_id = 100;
textview.setId(textview_id);
Padding setting
The square represents the content of the View a distance from the actual edges of the View set by padding. Using method: setPadding (int left, int top, int right, int bottom)
Set LayoutParams
When designing an interface, you often create multiple views, which have a parent view structure containing child views inside. As the parent View is a ConstraintLayout object, inside it contains sub-Views of objects such as TextView, Button ... , to set LayoutParams for a View using the method: setLayoutParams (LayoutParams). For example:

ConstraintLayout.LayoutParams txtParams
        = new ConstraintLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT );
textview.setLayoutParams(txtParams);
Note that depending on the parent's ViewGroup type (LinearLayout, FrameLayout, CoordinatorLayout, RelativeLayout, ConstraintLayout ...) that parameters of setLayoutParams (), need to initialize from that ViewGroup's LayoutParams class (LinearLayout.LayoutParams, FrameLayout.LayoutParams , CoordinatorLayout.LayoutParams ...)
As the code above, the textview will be a child element in a parent class of ConstraintLayout, so we instantiate a class object ConstraintLayout.LayoutParams
Initialize LayoutParams

Although there are many LayoutParams classes like LinearLayout.LayoutParams, FrameLayout.LayoutParams ... but it all inherits from ViewGroup.LayoutParams should create objects with the new operator.The initialization parameter is the width and height of the element (calculated according to the pixels). For example:
int width = 100; //pixe
int height  = 100; //pixel
ConstraintLayout.LayoutParams txtParams
        = new ConstraintLayout.LayoutParams(width, height);

No comments:

Post a Comment