Toggle Button to turn layer on/off

7061
7
06-17-2013 12:54 PM
JohnAllen
New Contributor III
Does anyone have any code they can post showing how they used a toggle button in a alertdialog to turn a featurelayer on and off in the map view?

I've tried doing this and I can get images, like the esri logo, to turn on and off in the mapview, but as soon as i use fLayer.setVisible(true); my app will crash. Same goes for when I use radio buttons and check boxes.

Any help would be great.

Thanks,
0 Kudos
7 Replies
JohnAllen
New Contributor III
This crashes my app:


Java: Mapscreen


/** Called when the activity is first created. */
 public void onCreate(Bundle savedInstanceState) {
  // Initial activity content view. (START)
  super.onCreate(savedInstanceState);
  setContentView(R.layout.mapscreen);

  // Retrieve the map layout and initial extent from mapscreen XML layout.
  map = (MapView) findViewById(R.id.map); 
  
  // Add ESRI basemap layer to MapView.
  map.addLayer(new ArcGISTiledMapServiceLayer("" +
  "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"));
  
  // Add Facility Feature layer to the MapView
  String URL = "http://PUT YOUR SERVER INFO HERE/ArcGIS/rest/services/Mobile/PUT YOUR MAP SERVICE HERE";
  ArcGISFeatureLayer fLayer = new ArcGISFeatureLayer(URL, MODE.ONDEMAND);
  map.addLayer(fLayer); 

  // This puts the ERSI logo on the bottom left corner of the screen once the map screen appears.
  map.setEsriLogoVisible(true);
               }

        //Check Box Setup
 public void onCheckboxClicked(View view) {
     // Is the view now checked?
     boolean checked = ((CheckBox) view).isChecked();
     
     // Check which checkbox was clicked
     switch(view.getId()) {
         case R.id.checkBox1:
             if (checked)
               fLayer.setVisible(true);
             else
               fLayer.setVisible(false);
             break;        
     }
 }



XML: mapscreen


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/app_frame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.esri.android.map.MapView
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        initExtent="-9246560.42264577, 3775709.72082467, -8746857.60892366, 4180002.76740144" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="77dp"
        android:layout_height="54dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/logo_white" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="onCheckboxClicked"
        android:text="WORK!!!!!" />

</RelativeLayout>

0 Kudos
JohnAllen
New Contributor III
I got it to partially work, but still need to work out my logic a little.

Java:


final CheckBox Layeronoffcheckbox = (CheckBox) layout2.findViewById (R.id.checkBox1);
    Layeronoffcheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
     
     @Override
     public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
     
      if (isChecked()){
       fLayer.setVisible(true); 
      }
      else{
       fLayer.setVisible(false);
      }
      
     }
     
    });


My logic is off because the check box should be checked when I open the alert dialog. Even if I tell the XML code to have it checked, it will stay checked every time I open the alert dialog even when I previously told it to turn off.

but

for now the checkbox on and off function works.
EricBader
Occasional Contributor III

Yes, this should work.

0 Kudos
CenterlineMapping
New Contributor III

Thanks, I recently figure it out.

0 Kudos
CenterlineMapping
New Contributor III

Are there any sample codes for adding a featureServer from a Map Server such as this..

http://services.arcgis.com/arcgis/rest/services/layer/FeatureServer/0

I've read the API and do not see any examples.  I figured out DynamicMapServiceLayer and can't seem to find code for a feature layer.

0 Kudos
WencelausNabiswa
New Contributor II

Yes this has indeed worked for me.

As for the part where it should stay checked or unchecked depending with what is on the MapView, I used this;

final CheckBox legend_image_check = drawerDialog.findViewById(R.id.hide_legend_image);
legend_image_check.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view)
    {
        if (legend_image_check.isChecked())
        {
            featureLayer.setVisible(true);
        }
        else
        {
            featureLayer.setVisible(false);
        }
    }
});
CenterlineMapping
New Contributor III

layer.setVisible(true) does not seem to work.

John, have you been successful at figuring a way to toggle layer visibility?  This is a old post but I just stumbled across it.

Thanks!

0 Kudos