Android Apps

Update Android main UI from Webview – Phonegap Plugin

June 20, 2014 by Sarath DR in Android Apps, Tips with 0 Comments

Recently we have developed an Android hybrid application which uses native action bar along with Phonegap webview. For several occasions it was necessary for the Webview element to communicate with with main UI thread. This can be achieved by creating a phone gap plugin. The below code is a simple example for this.

I have named the file as Helper.java under the package name com.plugins.ui

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 
package com.plugins.ui;
 
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
 
import com.sarathdr.app.MyApp;
import com.sarathdr.ui.MainActivity;
import com.sarathdr.ui.TeamSelectionActivity;
 
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
 
/**
 * Created by Sarath DR on 15/04/2014.
 */
public class Helpers extends CordovaPlugin
{
    CallbackContext callbackContext;
    private Context context;
    private Integer tabIndex;
 
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException
    {
        this.context = cordova.getActivity().getApplicationContext();
        this.callbackContext =  callbackContext;
 
        if (action.equals("moveTab")) {
            tabIndex = args.getInt(0);
            this.moveTab();
            return true;
        }else if(action.equals("showToast")){
            String message = args.getString(0);
            this.showToast( message );
        }
        return false;
    }
 
    public void moveTab()
    {
        // Calls a function in the main activity to move the action bar tab 
 
            cordova.getActivity().runOnUiThread(new Runnable(){
                public void run() {
                    MainActivity.moveTab(tabIndex);
                    callbackContext.success();
                } });
    }
 
 
 
 
 
    // Show toast message 
    public void showToast(String message) {
        if (message != null && message.length() > 0) {
            Toast.makeText(cordova.getActivity().getApplicationContext(),
                    message, Toast.LENGTH_LONG).show();
            callbackContext.success(message);
        } else {
            callbackContext.error("Expected one non-empty string argument.");
        }
    }
 
 
 
}

Share this to

Leave a reply

Your email address will not be published. Required fields are marked *

*

About
View Sarath D R's profile on LinkedIn

I am a technology consultant and an entrepreneur focused on the following technologies Java,Spring, Android, Angular, Asterisk, Symfony, Cake , Code-Igniter, Jquery , XAMPP, XHTML, AJAX, MySql, Seo, Web Design, Wordpress, iPhone development.

Today’s Tips
  • Generate Codova.jar file from Phonegap project using ant
  • Google map javascript client  to find the geographic co-ordinates  from  Address or post code
  • Update Android main UI from Webview – Phonegap Plugin
  • Jplayer is not working  with HTC One Android 4.3 version browser
  • Regular expression to validate mobile phone numbers starting with + ( plus ) – php
  • Simple pagination script for PHP
  • Useful adb ( Android Debug Bridge ) commands
  • Change the javascript files url to absolute url – Symfony 1.4
  • Set Http authentication header from Client – PHP / CURL

This site uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.This site also uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies.

Close