In Android, there are many views available already, using them any developer creates a UI of an android application, that a developer wants to create. In-Built Views are the following:
1. TextView
A user interfaces element that displays text to the user.
XML
<!-- TextView in XML --> < TextView android:id = "@+id/neveropenText" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:text = "Welcome to geeksforGeeks!" /> |
2. EditText
A user interfaces element for entering and modifying text.
XML
<!-- EditText in XML --> < EditText android:id = "@+id/GFGinput" android:layout_height = "wrap_content" android:layout_width = "match_parent" android:inputType = "Text" /> |
3. Button
A user interfaces element the user can tap or click to perform an action.
XML
<!-- button in XML --> < Button android:id = "@+id/neveropen_Button" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:text = "Log In To Lazyroar" /> |
4. ImageView
Displays image resources, for example, Bitmap or Drawable resources.
XML
<!-- ImageView Code in XML under Linear Layout --> < LinearLayout android:layout_width = "match_parent" android:layout_height = "match_parent" > < ImageView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:src = "@drawable/neveropenImage" android:contentDescription = "Lazyroar Image" /> </ LinearLayout > |
There are many more inBuilt views, layouts, and ViewGroups.
Visit it to know more: Android View
Custom Views
If any developer wants to create a UI which have views according to his thought. That created view is called CUSTOM VIEW. Android gives a powerful model for building your UI, based on the fundamental layout classes.
onMeasure():
We want to draw a View, first, we need to know the size of the View, and how the system draws it, and then tell the system, that this process is done in the onMeasure() method.
Syntax:
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
So, basically to decide the size of the view by the developer we use the onMeasure() method. we do overriding onMeasure to get the desired size of the view. When onMeasure is called you get widthMeasureSpec and heightMeasureSpec. This Spec is a way for the parent view to informing you about the requested size and size mode for your view. A size mode is a constraint the parent view sets for its child.
Below is the example of onMeasure overriding Use:
Java
// implementation of onMeasure Custom View in java @Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) { // new width you want int newWid = 60 ; // new height you want int newht = 50 ; int wM = MeasureSpec.getMode(widthMeasureSpec); int wS = MeasureSpec.getSize(widthMeasureSpec); int hM = MeasureSpec.getMode(heightMeasureSpec); int hS = MeasureSpec.getSize(heightMeasureSpec); int width; int height; // Measure Width custom view if (wM == MeasureSpec.EXACTLY) { // Must be of width size width = wS; } else if (wM == MeasureSpec.AT_MOST) { // Can't be bigger than new // width and width size width = Math.min(newWid, wS); } else { // Be whatever you want width = newWid; } // Measure Height of custom view if (hM == MeasureSpec.EXACTLY) { // Must be of height size height = hS; } else if (hM == MeasureSpec.AT_MOST) { // Can't be bigger than new // height and height size height = Math.min(newht, hS); } else { // Be whatever you want height = newht; } // for making the desired size setMeasuredDimension(width, height); } |