title: Cordova Custom Android Layout Plugin
body: <p>I am developing an android cordova custom layout plugin. When user click on a button, application will call the android plugin and display custom layout. User can move the object in the custom layout of android cordova plugin. However, I have problem with my plugin when I call my xml file in my java code. The following is my code.</p>
  
  <p>src/android/CustomLayout.java</p>
  
  <pre><code>public class CustomLayout extends CordovaPlugin {
      private static final String LOG_TAG = "CustomNotification";
      public CallbackContext callbackContext;
      Context context;
      Resources resources;
      String packageName;
  
      public CustomLayout(){
  
      }
  
      @Override
      public boolean execute(String action, String rawArgs, CallbackContext callbackContext) throws JSONException {
          this.callbackContext = callbackContext;
  
          context = cordova.getActivity().getApplicationContext();
          resources = context.getResources();
          packageName = context.getPackageName();
  
          if (action.equals("layout")) {
              customLayout();
              return true;
          }
          return false;
      }
  
      private void customLayout() {
          Log.e(TAG, "show");
          cordova.getActivity().runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  cordova.getActivity().setContentView(resources.getIdentifier("custom_layout", "layout", packageName));
                  ImageView motion = (ImageView) cordova.getActivity().findViewById(resources.getIdentifier("tvDragMe","id",packageName));
                  motion.setOnTouchListener(new MyTouchListener());
              }
          });
      }
  }
  
  class MyTouchListener implements View.OnTouchListener {
      @Override
      public boolean onTouch(View view, MotionEvent motionEvent) {
          if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
              ClipData data = ClipData.newPlainText("", "");
              View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
              view.startDrag(data, shadowBuilder, view, 0);
              view.setVisibility(View.INVISIBLE);
              return true;
          } else {
              return false;
          }
      }
  }
  </code></pre>
  
  <p>www/customlayout.js</p>
  
  <pre><code>var exec = require('cordova/exec');
  var platform = require('cordova/platform');
  
  module.exports = {
  
      alert: function(completeCallback) {
  
          exec(completeCallback, null, "CustomLayout", "layout", []);
      }
  };
  </code></pre>
  
  <p>res/layout/custom_layout.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"&gt;
  
  
      &lt;ImageView
          android:id="@+id/tvDragMe"
          android:src="@drawable/smiles"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@android:color/holo_blue_light"
          android:padding="10dp"
          android:textColor="#ffffff"
          android:layout_marginTop="35dp"
          android:text="Drag Me" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>XML file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
             id="cordova-plugin-custom_layout" version="1.0.0"&gt;
  
      &lt;name&gt;Custom Layout&lt;/name&gt;
      &lt;description&gt;Cordova Custom Layout Plugin&lt;/description&gt;
      &lt;keywords&gt;cordova,layout&lt;/keywords&gt;
  
      &lt;js-module src="www/customlayout.js" name="customlayout"&gt;
          &lt;merges target="customlayout" /&gt;
      &lt;/js-module&gt;
  
      &lt;platform name="android"&gt;
          &lt;config-file target="res/xml/config.xml" parent="/*"&gt;
              &lt;feature name="CustomLayout"&gt;
                  &lt;param name="android-package" value="org.apache.cordova.dialogs.CustomLayout"/&gt;
              &lt;/feature&gt;
          &lt;/config-file&gt;
  
          &lt;source-file src="res/layout/custom_layout.xml" target-dir="res/layout/custom_layout.xml" /&gt;
      &lt;/platform&gt;
  &lt;/plugin&gt;
  </code></pre>