When running my App from Android Studio can I force the emulator to the top?

Viewed 590

This is a pretty minor inconvenience, but I would like the Android Emulator to pop to the front when I start to run my app from Android Studio.

I know about the "Always on Top" setting in the emulator, but I have to minimize the emulator when I am working on my code. Running my app does not restore the emulator.

4 Answers

Following @Ves accepted answer did work but changed the batch file because it didn't work for me as it was, here is how I edited the batch file BringProcessToFront.ps1 based on another answer https://superuser.com/questions/647216/startup-program-bring-to-foreground.

if ( $args ){
   $processName = $args[0]
}
else{
   $processName = "qemu-system-i386"
}

Add-Type @"
  using System;
  using System.Runtime.InteropServices;
  public class Tricks {
     [DllImport("user32.dll")]
     [return: MarshalAs(UnmanagedType.Bool)]
     public static extern bool SetForegroundWindow(IntPtr hWnd);
  }
"@
sleep -sec 1
$h = (Get-Process $processName).MainWindowHandle
[void] [Tricks]::SetForegroundWindow($h)

Then I followed the rest of the steps from the accepted answer and it worked.

Created a solution to my problem - I am developing on Windows 10.

  1. Create a PowerShell batch file BringProcessToFront.ps1 (*Note your emulator might have a different name like qemu-system-x86_64 *)

    if ( $args ){
       $processName = $args[0]
    }
    else{
       $processName = "qemu-system-i386"
    }
    
    $sig = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
    Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32
    $hwnd = @(Get-Process $processName)[0].MainWindowHandle
    [Win32.NativeMethods]::ShowWindowAsync($hwnd, 4)
    
  2. Make sure you can actually run this script from a command line. The default security will not let you run scripts, as an admin you need to allow them using Set-Execution If everything is working (and the emulator is running) it will pop to the front.

    powershell -command C:\users\username\BringProcessToFront.ps1
    
  3. In Android Studio create a new External Tool "BringEmulatorToFront"

    Run>Edit Configuraions
    Expand Android App and select app
    Before launch:  (Hit the green + sign to add a new external tool)
       In the "External Tools Dialog" hit the green + to create a new tool)
           Name: BringEmulatorToFront
           Description: Launch PowerShell to make sure Emulator is visible
           Program: powershell
           Arguments: -command C:\users\username\BringProcessToFront.ps1
           Working directory: C:\users\username
           *IMPORTANT*
            Uncheck the Advanced options Synchronize files and Open console
            (If you leave Open console checked your run will not terminate cleanly)           
    
  4. Run your app and watch the Emulator pop to the front!

I use AutoHotkey for this and a very simple script:

#IfWinActive, ahk_class SunAwtFrame
$+F10::
Send, +{F10}
WinActivate, Android Emulator
return

This runs the app and activates the last used emulator when you press F10

You can also compile this and have it run without having AutoHotkey installed.

I have been trying to do that for some time now... I guess it can not be done.

I solved the problem by buying another monitor, and pushing the Emulator to the other one.

Related