Skip to content

Instantly share code, notes, and snippets.

View rajsuvariya's full-sized avatar

Raj Suvariya rajsuvariya

View GitHub Profile
@rajsuvariya
rajsuvariya / SharedPreferenceModule.java
Last active August 6, 2017 07:26
Use Native Modules in React Native + Android - Hybrid Apps
public class SharedPreferenceModule extends ReactContextBaseJavaModule {
Context mContext;
public AppSharedPreferenceManager preferenceManager;
public SharedPreferenceModule(ReactApplicationContext reactContext) {
super(reactContext);
mContext = reactContext;
preferenceManager = new AppSharedPreferenceManager(new PreferencesHelper(reactContext));
}
public class SharedPreferencePackager implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new SharedPreferenceModule(reactContext));
return modules;
}
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new SharedPreferencePackager())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
@ReactMethod
public void getMpin(Callback successCallBack){
try {
successCallBack.invoke(preferenceManager.getMpin());
} catch (Exception e) {
Toast.makeText(mContext, "Unable to fetch shared preference", Toast.LENGTH_LONG).show();
}
}
NativeModules.SharefPreference.getMpin((mpin)=>console.log(mpin))
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new SharePreferencePackager()); // <-- Add this line with your package name.
}
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_group"
android:gravity="center" />
<item
android:id="@+id/ic_group_count"
android:drawable="@color/transparent" />
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<!-- add the following item to your menu.xml -->
<item
android:id="@+id/ic_group"
android:icon="@drawable/actionbar_group_icon.xml"
android:title="@string/group_title"
package com.bslamc.ifa.customComponent.badgeDrawable;
/**
* Created by rajsuvariya on 17/8/17.
*/
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
setCount(this, "9");
}
public void setCount(Context context, String count) {
MenuItem menuItem = defaultMenu.findItem(R.id.ic_group);
LayerDrawable icon = (LayerDrawable) menuItem.getIcon();
CountDrawable badge;