{"id":53137,"date":"2019-06-28T12:18:56","date_gmt":"2019-06-28T06:48:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=53137"},"modified":"2019-06-28T15:05:18","modified_gmt":"2019-06-28T09:35:18","slug":"custom-android-native-view-in-react-native-as-component","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/custom-android-native-view-in-react-native-as-component\/","title":{"rendered":"Add Custom Android Native View In React Native As a Component"},"content":{"rendered":"<p>A recent <a title=\"React Native Development Services\" href=\"http:\/\/www.tothenew.com\/react-native-development-services\">React Native<\/a> project at the company came with an additional requirement of integrating a video player within the application.<\/p>\n<p>There already are a lot of React Native video players available, but almost all of them come with inherent shortcomings. We could not find one with the perfect implementation of overlays and other customizations which were required in our project.<\/p>\n<p>This led us to develop our own <a title=\"Write once use anywhere : React native components\" href=\"http:\/\/www.tothenew.com\/blog\/write-once-use-anywhere-react-native-components\/\">native component<\/a> for use in the application.<\/p>\n<p>This blog will help you understand how to integrate an Android native view, or a custom view, in a React Native application as a component.<\/p>\n<p><strong>Step-1: Create the view<\/strong><\/p>\n<p>The React Native application contains folders for the <a title=\"Android Application Development\" href=\"http:\/\/www.tothenew.com\/mobile-android-application-development-services\">Android<\/a> and <a title=\"iOS App Development\" href=\"http:\/\/www.tothenew.com\/mobile-ios-application-development-services\">iOS<\/a> native codes. Open the Android code using Android Studio and create the custom view (video player) using the native code. In our project, we developed the custom video player using Exp-player for Android in the native Android code.<\/p>\n<p><strong>Step-2: Create custom ViewManager class<\/strong><\/p>\n<p>Native views are created and manipulated by extending <strong>ViewManager<\/strong> or more commonly <strong>SimpleViewManager<\/strong>. A <strong>SimpleViewManager<\/strong> is convenient in this case because it applies common properties such as background color, opacity, and Flexbox layout.<\/p>\n<p>In this step, we write a class which inherits from SimpleViewManager. In this class, we select which Android view is going to be used in the React Native code.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-53139 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/1.png\" alt=\"Custom Android Native View In React Native As a Component\" width=\"583\" height=\"52\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/1.png 583w, \/blog\/wp-ttn-blog\/uploads\/2019\/06\/1-300x26.png 300w\" sizes=\"(max-width: 583px) 100vw, 583px\" \/><\/p>\n<p>Here SimpleViewManager&lt;VideoView&gt; is used to expose the Android native view to the React Native code.<\/p>\n<p><strong>Step-3: Implement createViewInstance method<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-53140 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/2.png\" alt=\"Custom Android Native View In React Native As a Component\" width=\"732\" height=\"287\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/2.png 732w, \/blog\/wp-ttn-blog\/uploads\/2019\/06\/2-300x117.png 300w, \/blog\/wp-ttn-blog\/uploads\/2019\/06\/2-624x244.png 624w\" sizes=\"(max-width: 732px) 100vw, 732px\" \/><\/p>\n<p>Name returned by the <strong>getName()<\/strong> method is used to reference the native view type from JavaScript.<\/p>\n<p>Views are created in the createViewInstance method. The view should initialize itself in its default state and any properties will be set via a follow-up call to update the view.<\/p>\n<p><strong>Step-4: Expose view property setters using @ReactProp (or @ReactPropGroup) annotation<\/strong><\/p>\n<p>If we want to send some data from the React Native code to our component using props, we have to define a method in our component to accept it. For example, we defined <strong>setVideoPath<\/strong> in our component class.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-53141 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/3.png\" alt=\"Custom Android Native View In React Native As a Component\" width=\"736\" height=\"433\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/3.png 736w, \/blog\/wp-ttn-blog\/uploads\/2019\/06\/3-300x176.png 300w, \/blog\/wp-ttn-blog\/uploads\/2019\/06\/3-624x367.png 624w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/p>\n<p><strong>Step-5: Create a packaging module<\/strong><\/p>\n<p>Since we are all set with the VideoViewManager class, now we have to register this manager class to be called from the React Native module using a Packaging module.<br \/>\nFor this, we write a class that implements the ReactPackage interface. In the createViewManager() method, we instantiate the ViewManager class that we want to expose to the React Native code.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-53142 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/4.png\" alt=\"Custom Android Native View In React Native As a Component\" width=\"835\" height=\"328\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/4.png 835w, \/blog\/wp-ttn-blog\/uploads\/2019\/06\/4-300x117.png 300w, \/blog\/wp-ttn-blog\/uploads\/2019\/06\/4-624x245.png 624w\" sizes=\"(max-width: 835px) 100vw, 835px\" \/><\/p>\n<p><strong>Step-6: Add the package module to the application class<\/strong><\/p>\n<p>In the application class of the React Native project, add the package module in the <strong>getPackage()<\/strong> method.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-53143 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/5.png\" alt=\"Custom Android Native View In React Native As a Component\" width=\"390\" height=\"166\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/5.png 390w, \/blog\/wp-ttn-blog\/uploads\/2019\/06\/5-300x127.png 300w\" sizes=\"(max-width: 390px) 100vw, 390px\" \/><\/p>\n<p><strong>Step-7: Implement the javaScript module<\/strong><\/p>\n<p>The final step is to create a JavaScript module that defines the interface layer between <a title=\"Java Development Outsourcing\" href=\"http:\/\/www.tothenew.com\/java-development-services\">Java<\/a> and JavaScript for the users of the new view. To implement this, we create VideoView.js in the src folder as defined below.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-53144 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/6.png\" alt=\"Custom Android Native View In React Native As a Component\" width=\"596\" height=\"225\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/6.png 596w, \/blog\/wp-ttn-blog\/uploads\/2019\/06\/6-300x113.png 300w\" sizes=\"(max-width: 596px) 100vw, 596px\" \/><\/p>\n<p>Here, the first parameter for requireNativeComponent is the same one which we have already defined in ViewManager and returns by the <strong>getName()<\/strong> method.<\/p>\n<p><strong>Step-8: Use the component<\/strong><\/p>\n<p>Finally, we are ready with the native component to use in our React Native code.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-53145 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/7.png\" alt=\"Custom Android Native View In React Native As a Component\" width=\"636\" height=\"277\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2019\/06\/7.png 636w, \/blog\/wp-ttn-blog\/uploads\/2019\/06\/7-300x130.png 300w, \/blog\/wp-ttn-blog\/uploads\/2019\/06\/7-624x271.png 624w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/p>\n<p>Thank you for reading the blog. I hope you enjoyed it and found this to be helpful. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A recent React Native project at the company came with an additional requirement of integrating a video player within the application. There already are a lot of React Native video players available, but almost all of them come with inherent shortcomings. We could not find one with the perfect implementation of overlays and other customizations [&hellip;]<\/p>\n","protected":false},"author":1219,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":47},"categories":[518,1],"tags":[4845,4804,4803,4805,3505],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/53137"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1219"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=53137"}],"version-history":[{"count":7,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/53137\/revisions"}],"predecessor-version":[{"id":53158,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/53137\/revisions\/53158"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=53137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=53137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=53137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}