Skip to content
Search

Android.view.View

lastmod 2018-08-17created 2015-11-069 edits

This class represents the basic building block for user interface components. A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). The ViewGroup subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties.

Size of View

Describes how to obtain the size of a View.
In the normal Activity creation process, the following Event is available for checking the size of a View.

public void onWindowFocusChanged(boolean hasFocus) { }

However, Fragment has no onWindowFocusChanged(). So you have to check it using ViewTreeObserver as below.

final View myView =  getActivity().findViewById(R.id.widget_id);
ViewTreeObserver vto = myView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        Log.d("SizeOf", "Height: " + myView.getHeight() + ", Width: " + myView.getWidth());
        // ...
        ViewTreeObserver obs = myView.getViewTreeObserver();
        obs.removeGlobalOnLayoutListener(this);
    }
});

Basic usage of views (resizing, moving, etc.)

Add a child view.

view.addView(childView, LayoutParams);

Add a view as a window to the activity's top-level window.

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
wm.addView(View, WindowManager.LayoutParams);

Remove a child view.

removeView(childView);

Window view.

wm.removeView(childView);

Change the size of a view.

ViewGroup.LayoutParams param = view.getLayoutParams();
param.width = 너비;
param.height = 높이;
view.setLayoutParams(param);

Change the size of a child view.

updateViewLayout(chideView, LayoutParams);

Moving a view: only possible for views added as a window (WindowManager.LayoutParams).

WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.x = x위치;
params.y = y위치;
wm.updateViewLayout(chidView, params);

For a view that moves along with touch:

  1. Handle touch with onTouchEvent.
  2. Create WindowManager.LayoutParams.
  3. Get the WindowManager (getSystemService).
  4. Create the window and add the view (addView).
  5. Touch move.
  6. Move the view (change WindowManager.LayoutParams).
  7. Apply (updateViewLayout).

How Android Draws Views

Usage of forceLayout(), requestLayout() and invalidate()

  • View.forceLayout(): forces the layout to be recalculated.
  • View.invalidate(): only refresh the view.
  • View.requestLayout(): refresh the view and compute the size of the view in the screen.

How to obtain a MotionEvent

this.setOnLongClickListener(this);
this.setOnDragListener(this);
// ...
@Override
public boolean onLongClick(View v) {
    return false;
}
@Override
public boolean onDrag(View v, DragEvent event) {
    return false;
}

How to find the View that currently has focus

This is how to find the View that currently has focus. It is useful when you need to know the focus of an EditText or a View.

view.requestFocus(); // 포커스(Foucs 강제로 주기
View cView = getCurrentFocus();
if (cView instanceof View) {
    Log.d("DDDD", "focus ok");
}

Capturing what is displayed in a Layout or View

Drawing Cache
Caches what is displayed in the view as a Bitmap. Whether the Drawing Cache is used can be set by setting the drawing cache enabled XML attribute to true, or by using the View.setDrawingCache() method in code.
public void View.setDrawingCacheEnabled(boolean enable)
Determines whether to store the current view image in the Drawing Cache every time the view is updated. (automatic)
Since new view information is stored in the Drawing Cache every time it changes (Invalidate), this can cause performance degradation.
public void View.buildDrawingCache()
Stores the view image in the Drawing Cache. (manual)
public Bitmap View.getDrawingCache()
Returns the view image stored in the Drawing Cache as a Bitmap.

Creating a Custom View

Write it in the following order.

  1. Extend View or a View subclass.
  2. Override the required methods.

The list of methods to override is as follows.

  • Construct and Destructor.
    • Constructors: there are two constructors, one called when created in code and one called when created by being inflated from a layout file. The second constructor additionally needs code that parses and applies the attributes defined in the layout file.
    • onFinishInflate: called after the view and all of its children have been inflated from the layout XML file. If you want to do any additional work at that point, do it here.
  • Layout
    • onMeasure(int, int): calculates and applies the size of the view and its children within the width and height specs calculated by the parent.
    • onLayout(boolean, int, int, int, int): called when the view needs to assign a size and postion to all of its children.
    • onSizeChanged(int, int, int, int): called after the size of the view has changed.
  • Draw
    • onDraw(Canvas): called when the View needs to redraw its content.
  • Event handling
    • onKeyDown(int, KeyEvent): called when a new key event occurs.
    • onKeyUp(int, KeyEvent): called when a key up event occurs.
    • onTrackballEvent(MotionEvent): called when a trackball motion event occurs.
    • onTouchEvent(MotionEvent): called when a touchscreen motion event occurs.
    • onFocusChanged(boolean, int, Rect): called when the View gains or loses focus.
    • onWindowFocusChanged(boolean): called when the window containing the View gains or loses focus.
  • Attach
    • onAttachedToWindow(): called when the View is attached to a Window.
    • onDetachedFromWindow(): called when the View is detached from a Window.
    • onWindowVisibilityChanged(int): called when the visibility of the window containing the View changes.

See also

Favorite site

References


  1. Android_UI_implementations_-_Hierarchy_of_screen_components.pdf ↩