Android Native Bridging in React Native using TextView to display Html

I have tried using a few libraries that can take html text and render the html like inside of WebView libraries but I have been trying something different.

I’m trying Android Native Bridging so that I can take Java Text View and return the Text View inside my component.

First I created a MyCustomViewManager:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> package com.pollit;
import android.view.ViewGroup.LayoutParams;
import android.text.Html;
import android.widget.TextView;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
public class MyCustomViewManager extends SimpleViewManager<TextView> {
public static final String REACT_CLASS = "MyCustomView";
@Override
public String getName() {
return REACT_CLASS;
}
@Override
protected TextView createViewInstance(ThemedReactContext reactContext) {
TextView tv = new TextView(reactContext);
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return tv;
}
@ReactProp(name = "text")
public void setText(TextView view, String text) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
view.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));
} else {
view.setText(Html.fromHtml(text));
}
}
}
</code>
<code> package com.pollit; import android.view.ViewGroup.LayoutParams; import android.text.Html; import android.widget.TextView; import com.facebook.react.uimanager.SimpleViewManager; import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.annotations.ReactProp; public class MyCustomViewManager extends SimpleViewManager<TextView> { public static final String REACT_CLASS = "MyCustomView"; @Override public String getName() { return REACT_CLASS; } @Override protected TextView createViewInstance(ThemedReactContext reactContext) { TextView tv = new TextView(reactContext); tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); return tv; } @ReactProp(name = "text") public void setText(TextView view, String text) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { view.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT)); } else { view.setText(Html.fromHtml(text)); } } } </code>
        package com.pollit;
        import android.view.ViewGroup.LayoutParams;
        import android.text.Html;
        import android.widget.TextView;
        import com.facebook.react.uimanager.SimpleViewManager;
        import com.facebook.react.uimanager.ThemedReactContext;
        import com.facebook.react.uimanager.annotations.ReactProp;

        public class MyCustomViewManager extends SimpleViewManager<TextView> {

            public static final String REACT_CLASS = "MyCustomView";

            @Override
            public String getName() {
                return REACT_CLASS;
            }

            @Override
            protected TextView createViewInstance(ThemedReactContext reactContext) {
                TextView tv = new TextView(reactContext);
                tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                return tv;
            }

            @ReactProp(name = "text")
            public void setText(TextView view, String text) {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    view.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));
                } else { 
                    view.setText(Html.fromHtml(text));
                }
            }
        }

Then created MyPackage:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> package com.pollit;
import java.util.ArrayList;
import android.widget.TextView;
import com.facebook.react.ReactPackage;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.ViewManager;
import java.util.List;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.JavaScriptModule;
import java.util.Collections;
import com.facebook.react.bridge.ReactApplicationContext;
public class MyPackage implements ReactPackage {
@Override
public List<NativeModule>
createNativeModules(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
List<ViewManager> viewManagers = new ArrayList<>();
viewManagers.add(new MyCustomViewManager());
return viewManagers;
}
}
</code>
<code> package com.pollit; import java.util.ArrayList; import android.widget.TextView; import com.facebook.react.ReactPackage; import com.facebook.react.uimanager.SimpleViewManager; import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.annotations.ReactProp; import com.facebook.react.uimanager.ViewManager; import java.util.List; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.JavaScriptModule; import java.util.Collections; import com.facebook.react.bridge.ReactApplicationContext; public class MyPackage implements ReactPackage { @Override public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) { return Collections.emptyList(); } @Override public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) { List<ViewManager> viewManagers = new ArrayList<>(); viewManagers.add(new MyCustomViewManager()); return viewManagers; } } </code>
        package com.pollit;
        import java.util.ArrayList;
        import android.widget.TextView;
        import com.facebook.react.ReactPackage;
        import com.facebook.react.uimanager.SimpleViewManager;
        import com.facebook.react.uimanager.ThemedReactContext;
        import com.facebook.react.uimanager.annotations.ReactProp;
        import com.facebook.react.uimanager.ViewManager;
        import java.util.List;
        import com.facebook.react.bridge.NativeModule;
        import com.facebook.react.bridge.JavaScriptModule;

        import java.util.Collections;
        import com.facebook.react.bridge.ReactApplicationContext;

        public class MyPackage implements ReactPackage {

            @Override
                public List<NativeModule>
                createNativeModules(ReactApplicationContext reactContext) {
                    return Collections.emptyList();
                }

                @Override
                public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
                List<ViewManager> viewManagers = new ArrayList<>();
                viewManagers.add(new MyCustomViewManager());
                return viewManagers;
            }
        }

Then added MyPackage() inside packages in the MainApplication.java:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
//packages.add(new TwitterSigninPackage());
packages.add(new MyPackage());
return packages;
}
</code>
<code>@Override protected List<ReactPackage> getPackages() { @SuppressWarnings("UnnecessaryLocalVariable") List<ReactPackage> packages = new PackageList(this).getPackages(); // Packages that cannot be autolinked yet can be added manually here, for example: //packages.add(new TwitterSigninPackage()); packages.add(new MyPackage()); return packages; } </code>
@Override
  protected List<ReactPackage> getPackages() {
    @SuppressWarnings("UnnecessaryLocalVariable")
    List<ReactPackage> packages = new PackageList(this).getPackages();
    // Packages that cannot be autolinked yet can be added manually here, for example:
    //packages.add(new TwitterSigninPackage());
    packages.add(new MyPackage());
      
    return packages;
  }

But whats happening is when i import the TextView from Native UI:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const txtView = requireNativeComponent('MyCustomView');
<txtView text={e["message"]["mess"].message}/>
</code>
<code>const txtView = requireNativeComponent('MyCustomView'); <txtView text={e["message"]["mess"].message}/> </code>
const txtView = requireNativeComponent('MyCustomView');

<txtView text={e["message"]["mess"].message}/>

The text is blank, its because the text is not wrapping and resizing the Text View, but when I set a height:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><txtView style={{height:200,}} text={e["message"]["mess"].message}/>
</code>
<code><txtView style={{height:200,}} text={e["message"]["mess"].message}/> </code>
<txtView style={{height:200,}} text={e["message"]["mess"].message}/>

The text is visible, but I don’t want to set the height, I want the height of to set the height containing the text inside automatically, how can I achieve this?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật