I have an ImageView and on top of that, I have a CustomView which extends ImageView. ImageView is in BgPicture class handled by fragment class. But in CustomView I want to perform long touch which will show drop-down menu ( or something like it ) and after selected choose will save data with position into a database and draw a circle in touched position. If someone clicks in an existing circle I also want to show him some options and not allowing to create a new circle. I am not sure if I used the right approach for this task. Below will provide my code I came so far. Would anyone point me in the right direction? Also how to perform long touch?
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bg_picture_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/bg_picture"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/picture" />
<si.result.vehiclecheck.layouts.views.CustomView
android:id="@+id/custom_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
BgPicture:
@EFragment ( R.layout.bg_picture )
public class BgPicture extends Fragment {
private String regNumber;
@ViewById( R.id.bg_picture )
ImageView picture;
@AfterViews
void startBgPicture() {
Bundle arguments = getArguments();
if( arguments != null ) {
regNumber = arguments.getString( "REG_NUMBER" );
}
}
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (visible) {
EventBus.getDefault().post( new MessageEvent( regNumber )
);
}
}
}
CustomView:
@EView
public class CustomView extends
android.support.v7.widget.AppCompatImageView {
private String regNum = "";
private List<Elements> elementsList;
public CustomView( Context context ) {
super( context );
}
public CustomView( Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, AttributeSet attrs, int
defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Subscribe( threadMode = ThreadMode.BACKGROUND )
public void onMessageEvent( MessageEvent event ) {
this.regNum = event.message;
getPositions();
}
// Its still in main thread
@Background
void getPositions() {
AppDatabase database = Room.databaseBuilder(
getContext(),
AppDatabase.class, "PositionsData"
).allowMainThreadQueries().build();
elementsList = database.positionDataDao().getElementsByNum( regNum
);
database.close();
invalidate();
}
@Override
protected void onDraw( Canvas canvas ) {
super.onDraw( canvas );
Paint paint = new Paint();
paint.setColor( Color.GREEN );
if( elementsList != null ) {
for( Elements el : elementsList ) {
canvas.drawCircle( el.getX(), el.getY(), 50, paint );
}
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
EventBus.getDefault().register( this );
}
@Override
protected void onDetachedFromWindow() {
EventBus.getDefault().unregister(this);
super.onDetachedFromWindow();
}
// TODO in background
@Background
void writeIntoDB( String regNum, String type, float x, float y ) {
AppDatabase database = Room.databaseBuilder(
getContext(),
AppDatabase.class, "PositionsData"
).allowMainThreadQueries().build();
Elements el = new Elements( regNum, false, type, x, y );
database.positionDataDao().insertNewElement( el );
database.close();
getPositions();
}
@Override
public boolean onTouchEvent( MotionEvent event ) {
if( event.getAction() == MotionEvent.ACTION_DOWN ) {
if( elementsList != null ) {
for( Elements el : elementsList ) {
if( Math.sqrt(Math.pow(event.getX() - el.getX(), 2) +
Math.pow(event.getY() - el.getY(), 2)) <= 50 ) {
return true;
}
}
}
writeIntoDB( regNum,"C", event.getX(), event.getY() );
invalidate();
return true;
}
if( event.getAction() == MotionEvent.ACTION_UP ) {
System.out.println("ACTION UP");
return true;
}
return false;
}
}