Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

Commit f3ad3d7

Browse files
committed
Issue #255 updating tutorial
1 parent 4843e17 commit f3ad3d7

File tree

1 file changed

+106
-40
lines changed

1 file changed

+106
-40
lines changed

docs/2-tutorials/2-develop/1-embed-webview/3-android-with-extension.html.md

Lines changed: 106 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,97 +11,158 @@ tabs:
1111
url: tutorials/develop/1-embed-webview/android-with-extension
1212
---
1313

14-
## Creating Apps with PhoneGap and Android Native Components using the Android Studio extension.
14+
This tutorial will show you how to add PhoneGap/Cordova support to an existing Android project using PhoneGap's Android Studio extension.
1515

16-
### Prerequisites:
16+
**Notice: make sure you're in `Text` mode when editing XML files. Select the `Text` tab at the bottom of the XML editor.**
1717

18-
* ***PhoneGap Android Studio extension***
18+
### Create Android project
1919

20-
Please install it directly in Android Studio. Under *Preferences* > *Plugins*.
21-
22-
![requirement](/images/android-webviews/req.png)
23-
24-
### Embedding a WebView
25-
26-
* create a new android project, set application name to "componentcase", company domain to "phonegapday.com" and edit package name to be "com.phonegapday". click next.
20+
* Create a new android project, set Application name to "ComponentCase", Company Domain to "phonegapday.com" and edit Package name to be "com.phonegapday". Click next.
2721

2822
![step1](/images/android-webviews/step1.png)
2923

30-
* check "phone and tablet" and set minimum sdk to api 21: android 5.0 (lollipop)
24+
* Check "Phone and Tablet" and set Minimum SDK to API 21: Android 5.0 (Lollipop)
3125

3226
![step2](/images/android-webviews/step2.png)
3327

34-
* click next and select "navigation drawer activity".
28+
* Click next and select "Navigation Drawer Activity".
3529

3630
![step3](/images/android-webviews/step3.png)
3731

38-
* click next and click finish
32+
* Click next and click Finish
3933

4034
![step4](/images/android-webviews/step4.png)
4135

42-
* clean up a few things
36+
* Clean up a few things
4337
* open `res/menu/activity_main_drawer.xml` and make sure it looks like this
44-
```xml
38+
```XML
4539
<?xml version="1.0" encoding="utf-8"?>
4640
<menu xmlns:android="http://schemas.android.com/apk/res/android">
47-
<group android:checkablebehavior="single">
41+
<group android:checkableBehavior="single">
4842
<item
4943
android:id="@+id/nav_webview"
50-
android:title="webview" />
44+
android:title="Webview" />
5145
<item
5246
android:id="@+id/nav_list_webview"
53-
android:title="list (webview)" />
47+
android:title="List (WebView)" />
5448
<item
5549
android:id="@+id/nav_list_native"
56-
android:title="list (native)" />
50+
android:title="List (Native)" />
5751
</group>
5852
</menu>
5953
```
60-
* open `res/layout/app_bar_main.xml` and delete the `floatingactionbutton`
54+
* open `res/layout/app_bar_main.xml` and delete the `FloatingActionButton`
6155
* delete all `ic_menu*` from `res/drawable`
62-
* open `res/layout/nav_header_main.xml` and change the first textview's text by "componentcase". delete the imageview and the other textview.
56+
* open `res/layout/nav_header_main.xml` and change the first `TextView`'s text to "**ComponentCase**". Delete the `ImageView` and the other `TextView`.
6357
* open `res/values/dimens.xml` and change `nav_header_height` to '100dp'
64-
* open 'mainactivity' and delete lines 26-33 (floatingactionbutton) and make sure your `onnavigationitemselected` method looks like this
65-
```java
66-
public boolean onnavigationitemselected(menuitem item) {
67-
// handle navigation view item clicks here.
68-
int id = item.getitemid();
58+
* open `MainActivity.java` and delete lines 26-33 (FloatingActionButton) and make sure your `onNavigationItemSelected` method looks like this
59+
```Java
60+
public boolean onNavigationItemSelected(MenuItem item) {
61+
// Handle navigation view item clicks here.
62+
int id = item.getItemId();
6963

70-
if (id == r.id.nav_webview) {
71-
// handle the camera action
72-
} else if (id == r.id.nav_list_webview) {
64+
if (id == R.id.nav_webview) {
65+
// Handle the camera action
66+
} else if (id == R.id.nav_list_webview) {
7367

74-
} else if (id == r.id.nav_list_native) {
68+
} else if (id == R.id.nav_list_native) {
7569

7670
}
7771

78-
drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);
79-
drawer.closedrawer(gravitycompat.start);
72+
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
73+
drawer.closeDrawer(GravityCompat.START);
8074
return true;
8175
}
8276
```
8377

78+
### Set up PhoneGap/Cordova in your project
79+
8480
* Make sure you have [NodeJS](https://nodejs.org) installed. If you already have [NodeJS](https://nodejs.org) installed make sure you `npm install -g plugman`
85-
* Go to **Android Studio** > `Preferences` > `Plugins` and click on _install JetBrains Plugin_ button.
81+
* Go to **Android Studio** > `Preferences` > `Plugins` and click on _Browse Repositories_ button.
8682
* Search for `PhoneGap` and install it. Make sure you don't install the **PhoneGap/Cordova Plugin**
8783
* Restart **Android Studio**
8884
* Go to `Tools` > `PhoneGap` > `Initialize Project`
89-
* Copy everything from www-shared/www to this newly created assets/www
85+
* You should see a notification: **"Gradle files have changed since last project sync. A project sync may be necessary for the IDE to work properly"**. Click on **Sync Now**
86+
* Copy everything from [www-shared/www](https://github.com/imhotep/PGDayEUWs2016/tree/master/www-shared) to this newly created `assets/www`
87+
* You can run the following to get `www-shared` easily inside your `app/src/main/assets` folder
88+
```Bash
89+
svn export --force https://github.com/imhotep/PGDayEUWs2016.git/trunk/www-shared/www
90+
```
9091
* Go to `Tools` > `PhoneGap` > `Install Plugin from npm`
9192
* Type in `cordova-plugin-device`
9293
* Go to `Tools` > `PhoneGap` > `Install Plugin from npm`
94+
* Type in `cordova-plugin-console`
95+
* Go to `Tools` > `PhoneGap` > `Install Plugin from filesystem`
96+
* Select `cordova-plugin-pgdayeu16` which can be found at [cordova-plugin-pgdayeu16](https://github.com/imhotep/PGDayEUWs2016/tree/master/cordova-plugin-pgdayeu16)
97+
* Again you can easily fetch the plugin to your filesystem using the following command
98+
```Bash
99+
svn export https://github.com/imhotep/PGDayEUWs2016.git/trunk/cordova-plugin-pgdayeu16
100+
```
93101

94-
1* Type in `cordova-plugin-console`
95-
1* Go to `Tools` > `PhoneGap` > `Install Plugin from filesystem`
96-
1* Select `cordova-plugin-pgdayeu16` which can be found at [cordova-plugin-pgdayeu16](https://github.com/imhotep/PGDayEUWs2016/tree/master/cordova-plugin-pgdayeu16)
102+
### Embedding CordovaWebView
97103

98-
* Add the following line to your `res/values/strings.xml`
104+
* open `res/layout/content_main.xml` and replace the TextView with the following
105+
```XML
106+
<org.apache.cordova.engine.SystemWebView
107+
android:id="@+id/WebViewComponent"
108+
android:layout_width="match_parent"
109+
android:layout_height="match_parent">
110+
</org.apache.cordova.engine.SystemWebView>
111+
```
112+
* Add the following attributes to `MainActivity.java`. Make sure you fix the imports.
113+
```Java
114+
private String TAG = "ComponentWrapper";
115+
private SystemWebView webView;
116+
private CordovaWebView webInterface;
117+
private CordovaInterfaceImpl stupidface = new CordovaInterfaceImpl(this);
118+
```
119+
* Add the following lines at the bottom of your `onCreate` method
120+
```Java
121+
//Set up the webview
122+
ConfigXmlParser parser = new ConfigXmlParser();
123+
parser.parse(this);
124+
125+
webView = (SystemWebView) findViewById(R.id.WebViewComponent);
126+
webInterface = new CordovaWebViewImpl(new SystemWebViewEngine(webView));
127+
webInterface.init(stupidface, parser.getPluginEntries(), parser.getPreferences());
128+
webView.loadUrl(parser.getLaunchUrl());
129+
```
130+
* These methods are required for `CordovaWebView` to work properly. Add them and fix imports
131+
```Java
132+
@Override
133+
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
134+
super.onActivityResult(requestCode, resultCode, intent);
135+
stupidface.onActivityResult(requestCode, resultCode, intent);
136+
}
137+
138+
@Override
139+
public void onDestroy() {
140+
webInterface.handleDestroy();
141+
super.onDestroy();
142+
}
99143

144+
public void onRequestPermissionsResult(int requestCode, String permissions[],
145+
int[] grantResults) {
146+
try
147+
{
148+
stupidface.onRequestPermissionResult(requestCode, permissions, grantResults);
149+
}
150+
catch (JSONException e)
151+
{
152+
Log.d(TAG, "JSONException: Parameters fed into the method are not valid");
153+
e.printStackTrace();
154+
}
155+
156+
}
157+
```
158+
159+
### Adding native and web list views
160+
161+
* Add the following line to your `res/values/strings.xml`
100162
```XML
101163
<string name="add_bookmark">Add Bookmark</string>
102164
```
103165
* Right click on `res/layout` and select `New` -> `XML` -> `Layout XML File`. Name it `bookmark_main`. Make sure it looks like this
104-
105166
```XML
106167
<?xml version="1.0" encoding="utf-8"?>
107168
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
@@ -135,7 +196,7 @@ Please install it directly in Android Studio. Under *Preferences* > *Plugins*.
135196
private LinearLayout bookmarkLayout;
136197
private ArrayList<String> bookmarks = new ArrayList<String>();
137198
```
138-
* Set up the native ListView by adding the following lines to your `onCreate` method. Fix imports
199+
* Set up the native ListView by adding the following lines to your `onCreate` method
139200

140201
```Java
141202
// Set up the bookmark view
@@ -182,6 +243,7 @@ Please install it directly in Android Studio. Under *Preferences* > *Plugins*.
182243
}
183244

184245
```
246+
* Fix imports
185247
* Add this line to your `res/layout/content_main.xml`
186248

187249
```XML
@@ -224,4 +286,8 @@ Please install it directly in Android Studio. Under *Preferences* > *Plugins*.
224286
```XML
225287
android:windowSoftInputMode="adjustPan"
226288
```
289+
290+
### Run the application
291+
292+
* Run the application by clicking on `Run` -> `Run 'app'`. You notice that elements added in the webview and native view are sychronized. Check out the plugin code for more details.
227293
* To avoid seeing weird `eglCodecCommon` errors in the console add this to your filter: `^(?!eglCodecCommon)`

0 commit comments

Comments
 (0)