From the Android Studio 3.6 new features blog post:
New location to toggle Gradle's offline mode
To enable or disable Gradle's offline mode, first select View > Tool Windows > Gradle from the menu bar. Then, near the top of the Gradle window, click Toggle Offline Mode Gradle offline button in the Gradle panel..
To enable or disable Gradle's offline mode, select View > Tool Windows > Gradle from the menu bar and near the top of the Gradle window, click Toggle Offline Mode
In Android Studio 3.6, You can toggle Gradle from offline to online mode (See screenshotenter image description here) and hit Gradle sync. This solved my issue.
Please follow below steps :
1.Open Your project.
2.Go to the Left side of the Gradle button.
3.
4.Click button above image show .
5. if this type of view you are not in offline mode.
6. Go to Build and rebucild the project.
Many people have given correct answer, I am just adding pictures relevant to Android Studio 3.6.2. When I upgraded to Android Studio 3.6.2 I kept getting error while doing build that 'Gradle was in offline mode' but under File>Settings>Gradle> I could not see the option to switchoff Gradle offline mode. So I did a fresh install of Android Studio 3.6.2 and I continued to get the same problem. After checking various answers I found that the placement of Gradle offline switch has changed & you can do the switch in Gradle panel as per the 2nd image below.