LG Watch


Download SDK

LG WatchDownload SDK

Source Code Analysis

Here, we will take a look at how to handle obstruction area with the sample application's source code.

Detecting Obstruction Area

To obtain the obstruction area information of the target device, import com.lge.wearable.ObstructionHelper.

Then you can obtain the obstruction area information of RectF type by using the getObstructionRect() function.


//Import the com.lge.wearable.ObstructionHelper class. ①

import com.lge.wearable.ObstructionHelper;

public class MainActivity extends WearableActivity {

//Declare a variable of RecF type. ②

private static RectF mObstructionRect;


protected void onCreate(Bundle savedInstanceState) {


//Get the obstruction rectangle using ObstructionHelper. ③

mObstructionRect = ObstructionHelper.getObstructionRect();

  1. ① For a blinking effect, save the gray and black colors alternately.
  2. ② Draw the obstruction area with the color saved in ①.
  3. ③ The color of obstruction area changes at intervals of 0.5 seconds and it blinks.

You can know whether the obstruction area exists or not according to the obstruction area information (mObstructionRect value).


//If the obstruction rectangle is empty, no obstruction. ①

if (mObstructionRect.isEmpty()) {

TextView textView = findViewById(R.id.text_obstruction);


} else { //Otherwise, blink the obstruction rectangle. ②

ViewGroup viewGroup = findViewById(android.R.id.content);

viewGroup.addView(new ObstructionView(this));


  1. ① If the mObstructionRect value is empty, this device has no obstruction area.
  2. ② If the mObstructionRect value is not empty, this device has an obstruction area. If the obstruction area exists, add ObstructionView and display the obstruction area through blinking.


class ObstructionView extends View implements Runnable {

private Paint mPaint = new Paint();

public ObstructionView(Context context) {




protected void onDraw(Canvas canvas) {

//Set the blink colors. ①

int color = (mPaint.getColor() != Color.GRAY) ? Color.GRAY : Color.BLACK;


//Draw the obstruction rectangle with the colors. ②

canvas.drawRect(mObstructionRect, mPaint);

//Flashes at 0.5 second intervals. ③

postDelayed(this, 500);



public void run() {




  1. ① Import com.lge.wearable.ObstructionHelper class.
  2. ② Declare a variable in order to obtain the obstruction area information of RectF type.
  3. ③ Save the obstruction area information obtained using getObstructionRect() function to the variable declared in ②.

You can know whether the obstruction area exists or not according to the obstruction area information (mObstructionRect value).

Adjustion Layout Against Obsturction

If there is a view object in the obstruction area, adjust the layout dynamically by adding a margin.

Layout is not changed if there is no obstruction area. You can adjust layout by adding a margin only when the obstruction area exists. So, it is possible to implement an application to be run on all smart watches.


//Move the imageView if there is an obstruction area. ①

ImageView imageView = findViewById(R.id.image_logo);

FrameLayout.LayoutParams layoutParams =

(FrameLayout.LayoutParams) imageView.getLayoutParams();


layoutParams.topMargin + 2 * (int) mObstructionRect.height(),

layoutParams.rightMargin, layoutParams.bottomMargin)

  1. ① Obtain the current layout parameter information of the image in the obstruction area and add a margin of twice the height of obstruction area to topMargin in order to adjust the layout to display the image below the obstruction area.