33

I don't know how to use a watch in Android Studio. I want to see how the value of a variable modifies through debugging. Does anyone know how to do this?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
uwe seeler
  • 343
  • 1
  • 3
  • 13
  • 2
    This VERY common debugging technique does not seem to be supported by intellij or android studio. And they think these are professional tools! (Intellij seems to think that a "watch" means variables that you commonly want to see, which is actually pretty silly. We want the debugger to break when a variable changes! THAT is what a watched variable is for.) – SMBiggs Sep 14 '19 at 20:19
  • 1
    @SMBiggs I agree it should be easier, but you can setup what's called a "watchpoint" to break when a variable changes. https://www.jetbrains.com/help/idea/2016.3/creating-field-watchpoints.html – jj. Nov 03 '20 at 22:55
  • @jj. Good to know--I'll try it out soon. This could be a real solution. – SMBiggs Nov 09 '20 at 16:53

2 Answers2

34

Start by putting a break point in the class where you'd want to watch a specific variable. Run the code and once it hits your breakpoint from the Variables window frame you should see all of the variables that are accessible. Simply choose the one you'd want to watch and then right click and choose "Add to watches" from the drop-down.

enter image description here

Keep debugging and you should see the variable from the Watches window frame update when appropriate based on your code.

enter image description here

Miguel
  • 19,793
  • 8
  • 56
  • 46
  • 1
    And if you can't see watches window >> restore default layout Shift + F12 – Eftekhari Aug 27 '16 at 14:36
  • 3
    Is there a way to see where and when it changes the value ? Without setting break points. I have a problem where I know the places it supposed to change, and with brake points I see that the value is correct, but then jump to the next break point and the value is already wrong. And jumping line by line does not seem optimal. – paakjis Jan 05 '17 at 07:18
  • @paakjis you can set Java Field Watchpoints: run-->View Brakepoints->"+" button on top left corner-->"Java Field Watchpoint". Set your class and field names in the dialog. – Gregory Stein May 16 '17 at 11:17
  • how do i see watches without setting a breakpoint? For instance, I want to see the current value of a static member of a class. In the watch field it doesn't show any value. – Michael Sep 23 '17 at 23:43
  • is there a way to copy value of a list during debugging? – Gauri Gadkari Aug 16 '22 at 21:38
13

Assuming you have the debug tabs selected:

enter image description here

If you can't find the Watches View, it might be hiding in a few spots.

Check the right side:

enter image description here

If its not there, check the left side of the Variables tab:

enter image description here

And if you don't find it in those places, then you can right click one of the variables at a breakpoint and choose "Add to watches" (as described in the accepted answer). After that the Watches View should either appear or be in one of the places I described above.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393