Disable and enable view android

18,711

Solution 1

Yes you can switch between the receivers of touch events by returning false in your onTouchEvent, this should propagate event to the next view.

UPD: The code should be something like this:

@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isTouchable) return false;
// your DrawerView logic
}

where isTouchable is a boolean variable which should be false is you don't want to draw on your view and true if you want to draw.

Solution 2

You can enable or disable click on your drawing overlay:

findViewById(R.id.custView).setClickable(true/false);

While your drawing overlay is not clickable, you will be able to interact with the map.

Share:
18,711
Eman87
Author by

Eman87

Junior Mobile Developer

Updated on June 14, 2022

Comments

  • Eman87
    Eman87 almost 2 years

    My target is to draw free shapes above google map v2, so I created an app to draw free shapes by finger movement in a custom view and putted this view above the map fragment in the layout.xml and made the view background to be transparent. and now I can draw free shapes well but the problem now is that I can't control map or do zooming or dragging in it.

    Is there are any way to make me control both of them, for example a button to covert the control from map fragment to view of drawing and vise versa ? or to turn on/off the view of drawing?

    this is my code

    DrawerView.java

    public class DrawerView extends View {
    
    public Paint paint = new Paint();
    public Path path = new Path();
    public Path circlePath = new Path();
    
    private int lineCol;
    
    
    public LayoutParams params;
    
    
    public DrawerView(Context context, AttributeSet attrs){
        super(context, attrs);
    
        //get the attributes specified in attrs.xml using the name we included
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.LineView, 0, 0);
    
        try {
            //get the text and colors specified using the names in attrs.xml
    
            lineCol = a.getInteger(R.styleable.LineView_lineColor, 0);//0 is default
    
        } finally {
            a.recycle();
        }
    
        //paint object for drawing in onDraw
        paint.setAntiAlias(true);
        paint.setColor(lineCol);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(4f);
    
    }   
    
    public void onButtonPress(){
        // resets the screen
        path.reset();
    
        // Calls the onDraw() method
        postInvalidate();
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
    
        canvas.drawPath(path, paint);
        //canvas.drawPath(circlePath, circlePaint);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
        // Gives you x and y coordinates on the Event.
        float pointX = event.getX();
        float pointY = event.getY();
    
        // Checks for the event that occurs
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(pointX, pointY);
    
            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(pointX, pointY);
            circlePath.reset();
    
            circlePath.addCircle(pointX, pointY, 30, Path.Direction.CW);
            break;
    
        case MotionEvent.ACTION_UP:
            circlePath.reset();
    
            break;
        default:
            return false;
        }
    
        postInvalidate();
        return true;
    }
    }
    

    MainActivity.java

    public class MainActivity extends Activity 
    implements OnMapClickListener, OnMapLongClickListener, OnMarkerDragListener{
    
    final int RQS_GooglePlayServices = 1;
    private GoogleMap myMap;
    
    Location myLocation;
    
    boolean markerClicked;
    PolygonOptions polygonOptions;
    Polygon polygon;
    
    Button plane;
    Button tank;
    Button ship;
    
    DrawerView myView;
    public Button btnReset;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
         plane = (Button)findViewById(R.id.btn_plane);
         tank = (Button)findViewById(R.id.btn_tank);
         ship = (Button)findViewById(R.id.btn_ship);
    
        FragmentManager myFragmentManager = getFragmentManager();
        MapFragment myMapFragment = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
        myMap = myMapFragment.getMap();
    
        myMap.setMyLocationEnabled(true);
    
        myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    
        myMap.setOnMapClickListener(this);
        myMap.setOnMapLongClickListener(this);
        myMap.setOnMarkerDragListener(this);
    
    
        plane.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myMap.addMarker(new MarkerOptions().position(
                        new LatLng(0.00, 0.00)).icon(BitmapDescriptorFactory.fromResource(R.drawable.plane)).draggable(true));
    
            }
        });
    
        tank.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myMap.addMarker(new MarkerOptions().position(
                        new LatLng(0.00, 0.00)).icon(BitmapDescriptorFactory.fromResource(R.drawable.tank)).draggable(true));
    
            }
        });
    
        ship.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myMap.addMarker(new MarkerOptions().position(
                        new LatLng(0.00, 0.00)).icon(BitmapDescriptorFactory.fromResource(R.drawable.destroyer)).draggable(true));
    
            }
        });
    
        markerClicked = false;
    
        myView = (DrawerView)findViewById(R.id.custView);
        myView.setBackgroundColor(Color.TRANSPARENT);
    
        btnReset = (Button) findViewById(R.id.clear);
    
        btnReset.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
                myView.path.reset();
                myView.postInvalidate();
    
            }
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_settings:
            String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
                    getApplicationContext());
            AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
            LicenseDialog.setTitle("Legal Notices");
            LicenseDialog.setMessage(LicenseInfo);
            LicenseDialog.show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    protected void onResume() {
    
        super.onResume();
    
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
    
        if (resultCode == ConnectionResult.SUCCESS){
            Toast.makeText(getApplicationContext(), 
                    "isGooglePlayServicesAvailable SUCCESS", 
                    Toast.LENGTH_LONG).show();
        }else{
            GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
        }
    
    }
    
    @Override
    public void onMapClick(LatLng point) {
        myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
    
        markerClicked = false;
    }
    
    @Override
    public void onMapLongClick(LatLng point) {      
        markerClicked = false;
    }
    
    @Override
    public void onMarkerDrag(Marker marker) {
    }
    
    @Override
    public void onMarkerDragEnd(Marker marker) {
    }
    
    @Override
    public void onMarkerDragStart(Marker marker) {
    
    }
    
      }
    

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.example.dmeogooglemapv2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    tools:context=".MainActivity" >
    
    
    <Button
        android:id="@+id/btn_tank"
        android:layout_width="wrap_content"
        android:layout_height="60dp"        
        android:layout_alignParentTop="true"
        android:background="@drawable/tank" /> 
    
      <Button
        android:id="@+id/btn_plane"
        android:layout_width="45dp"
        android:layout_height="45dp"        
        android:layout_alignParentTop="true"
        android:background="@drawable/plane" /> 
    
       <Button
        android:id="@+id/btn_ship"
        android:layout_width="wrap_content"
        android:layout_height="60dp"        
        android:layout_alignParentTop="true"
        android:background="@drawable/destroyer" /> 
    
       <Button
        android:id="@+id/clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clear" />
    
       </LinearLayout>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.example.dmeogooglemapv2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"  >
    
        <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment"/>
    
        <com.example.dmeogooglemapv2.DrawerView
        android:id="@+id/custView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="5dp"
        custom:lineColor="#ff0099" />
    
    </RelativeLayout>
    
    </LinearLayout>
    
  • Eman87
    Eman87 almost 11 years
    Could you show me how by the code? and will this make me control the map again?
  • nyconing
    nyconing over 6 years
    Curious about UPD stand?
  • Fawwaz Yusran
    Fawwaz Yusran about 5 years
    Why not just simply return isTouchable; ?