Reputation: 2574
I'm trying to reuse a customView inside a compose view. I follow the guidelines to achive this, but I can't display my customView in the DefaultPreview and when run the application no show nothing.
This is that I'm trying:
@Composable
fun Total() {
Row(Modifier.padding(top = 24.dp)
.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween) {
AndroidView(
factory = { context ->
// Creates custom view
CustomView(context).apply {
}
},
)
Column(modifier = Modifier.align(Alignment.Bottom)) {
Text("Total",
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.End,
fontSize = 12.sp,
color = colorResource(id = R.color.app_grey_dark))
Text("1 €",
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.End,
fontSize = 18.sp,
color = colorResource(id = R.color.app_black_dark))
}
}
}
Java view code:
public class CustomView extends FrameLayout {
...
...
private Scene myView;
public CustomView(@NonNull Context context) {
super(context);
}
public CustomView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CustomView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
screenRoot = findViewById(R.id.scene_root_myView);
initControlAddToCardOne();
myView.enter();
TransitionInflater transitionInflater = TransitionInflater.from(getContext());
transitionManager = transitionInflater.inflateTransitionManager(R.transition.scene_transition_manager,
screenRoot);
}
private void initControlAddToCardOne() {
myView= Scene.getSceneForLayout(screenRoot, R.layout.my_layout, getContext());
myView.setEnterAction(() -> {
ViewGroup defaultView = myView.getSceneRoot();
});
});
}
Layout xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true">
<ImageView
android:id="@+id/img_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/image"
android:adjustViewBounds="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Upvotes: 1
Views: 1902
Reputation: 460
AndroidView require width and height,use modifier:
AndroidView(
modifier = Modifier
.fillMaxWidth()
.height(100.dp),
factory = { context ->
CustomView(context = context)
},
update = {
}
)
Upvotes: 2