How do I pass data from custom view to parent activity? I've data of which box is touched in the custom view. I want to pass that data to the parent activity whenever a box is touched.
Here's my activity layout:
1
Edit: Here's parent activity:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible">
<SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
</SearchView>
</RelativeLayout>
<com.example.gaurav.dummyapp.FaceOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/face_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
FaceOverlayView.java
public class FaceOverlayView extends View {
private Bitmap mBitmap;
private SparseArray<Face> mFaces;
float left = 0;
float top = 0;
float right = 0;
float bottom = 0;
double sc;
private String faceData;
public FaceOverlayView(Context context) {
this(context, null);
}
public FaceOverlayView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FaceOverlayView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setBitmap( Bitmap bitmap ) {
mBitmap = bitmap;
FaceDetector detector = new FaceDetector.Builder( getContext() )
.setTrackingEnabled(true)
.setLandmarkType(FaceDetector.ALL_LANDMARKS)
.setMode(FaceDetector.ACCURATE_MODE)
.build();
if (!detector.isOperational()) {
//Handle contingency
} else {
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
mFaces = detector.detect(frame);
detector.release();
}
logFaceData();
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if ((mBitmap != null) && (mFaces != null)) {
double scale = drawBitmap(canvas);
drawFaceBox(canvas, scale);
}
}
private double drawBitmap(Canvas canvas) {
double viewWidth = canvas.getWidth();
double viewHeight = canvas.getHeight();
double imageWidth = mBitmap.getWidth();
double imageHeight = mBitmap.getHeight();
double scale = Math.min(viewWidth / imageWidth, viewHeight / imageHeight);
sc = scale;
Rect destBounds = new Rect(0, 0, (int)(imageWidth * scale), (int)(imageHeight * scale));
canvas.drawBitmap(mBitmap, null, destBounds, null);
return scale;
}
private void drawFaceBox(Canvas canvas, double scale) {
//This should be defined as a member variable rather than
//being created on each onDraw request, but left here for
//emphasis.
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
for( int i = 0; i < mFaces.size(); i++ ) {
Face face = mFaces.valueAt(i);
left = (float) ( face.getPosition().x * scale );
top = (float) ( face.getPosition().y * scale );
right = (float) scale * ( face.getPosition().x + face.getWidth() );
bottom = (float) scale * ( face.getPosition().y + face.getHeight() );
canvas.drawRect( left, top, right, bottom, paint );
}
}
private void drawFaceLandmarks(Canvas canvas, double scale ) {
Paint paint = new Paint();
paint.setColor( Color.GREEN );
paint.setStyle( Paint.Style.STROKE );
paint.setStrokeWidth( 5 );
for( int i = 0; i < mFaces.size(); i++ ) {
Face face = mFaces.valueAt(i);
for ( Landmark landmark : face.getLandmarks() ) {
int cx = (int) ( landmark.getPosition().x * scale );
int cy = (int) ( landmark.getPosition().y * scale );
canvas.drawCircle( cx, cy, 10, paint );
}
}
}
private void logFaceData() {
float smilingProbability;
float leftEyeOpenProbability;
float rightEyeOpenProbability;
float eulerY;
float eulerZ;
for( int i = 0; i < mFaces.size(); i++ ) {
Face face = mFaces.valueAt(i);
smilingProbability = face.getIsSmilingProbability();
leftEyeOpenProbability = face.getIsLeftEyeOpenProbability();
rightEyeOpenProbability = face.getIsRightEyeOpenProbability();
eulerY = face.getEulerY();
eulerZ = face.getEulerZ();
Log.e( "Face Detection", "Smiling: " + smilingProbability );
Log.e( "Face Detection", "Left eye open: " + leftEyeOpenProbability );
Log.e( "Face Detection", "Right eye open: " + rightEyeOpenProbability );
Log.e( "Face Detection", "Euler Y: " + eulerY );
Log.e( "Face Detection", "Euler Z: " + eulerZ );
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction() & MotionEvent.ACTION_MASK;
int x = (int) event.getX();
int y = (int) event.getY();
float eulerY;
float eulerZ;
switch(action) {
case MotionEvent.ACTION_DOWN : {
for( int i = 0; i < mFaces.size(); i++ ) {
Face face = mFaces.valueAt(i);
left = (float) ( face.getPosition().x * sc );
top = (float) ( face.getPosition().y * sc );
right = (float) sc * ( face.getPosition().x + face.getWidth() );
bottom = (float) sc * ( face.getPosition().y + face.getHeight() );
if((x > left && x < right) && (y > top && y < bottom)){
Log.e("BOX>>>>>>>>>>", String.valueOf(i));
}
}
break;
}
case MotionEvent.ACTION_MOVE : {
//path.lineTo(event.getX(), event.getY());
break;
}
}
invalidate();
return super.onTouchEvent(event);
}
Activity Class: public class dummyActivity extends AppCompatActivity {
private FaceOverlayView fooView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy);
fooView.setBitmap("bitmap");
}
i think what you need is onTouchListenter on the parent activity
i think you have a textfield inside the box that hold the integer value, so you do inside the onTouch
you can do another way, implementing the touch inside the parent and handle the touch, doing some ifs to check the View id and handle that
another way and easier is to give the TextView inside the box the match_parent for both direction and set the onClick, its the easier, you should use this one if you dont really need the touch