0

I've got a question about CheckBoxPreferences. I've read through the number of related threads and tried some of them but as I am a beginner I am still struggling to find the solution for the issue I am facing up. Below code is to apply a polygon over the google-map and in general it does it:

public class ZurichGreifensee extends Activity implements OnClickListener {

    String latLngUster = "lat=47.348491&lon=8.717529";

    private final LatLng LOCATION_GREIFENSEE = new LatLng(47.348124, 8.681688);

    private GoogleMap mMap;
    Button fishActivity;
    ToggleButton mapView;
    Polygon Naturschutzgebiet, Seeschutzzone;
    CheckBoxPreference naturschutzgebiet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.lakes_info);

        NetworkConnectivityCheck checkit = new NetworkConnectivityCheck();
        Boolean conn = checkit.checkNow(this.getApplicationContext());
        if(conn == true){

        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        CameraUpdate updateZurichGreifensee = CameraUpdateFactory.newLatLngZoom(LOCATION_GREIFENSEE, 13);
        mMap.animateCamera(updateZurichGreifensee);
        mMap.setMyLocationEnabled(true);

        mapView = (ToggleButton) findViewById(R.id.tbSatNorMapView);
        fishActivity = (Button) findViewById(R.id.bWhereAmI);
        mapView.setOnClickListener(this);
        fishActivity.setOnClickListener(this);

        }else{
            connectivityMessage("Kein Netzwerk");
        }

        SharedPreferences getGewasserRevierKarte = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        boolean naturschutzgebiet = getGewasserRevierKarte.getBoolean("naturschutzgebiet", false);{
            if (naturschutzgebiet == true){
                addNaturschutzgebiet();}
        }

        boolean seeschutzzone_1 = getGewasserRevierKarte.getBoolean("seeschutzzone_1", false);{
            if (seeschutzzone_1 == true)
                addSeeschutzzone();
        }
    }

    private void connectivityMessage(String string) {
        // TODO Auto-generated method stub
        Context context = getApplicationContext();
        Toast toast = Toast.makeText(context, "", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setText(string);
        toast.show();
    }


    private void addNaturschutzgebiet() {
        Naturschutzgebiet = mMap.addPolygon(new PolygonOptions()
            .add(new LatLng(47.366676,8.673754), new LatLng(47.367526,8.67236), new LatLng(47.369459,8.670965), new LatLng(47.371886,8.669538),
                 new LatLng(47.372525,8.669184), new LatLng(47.375453,8.661481), new LatLng(47.376819,8.657801), new LatLng(47.378032,8.652318), 
                 new LatLng(47.375962,8.651159), new LatLng(47.378839,8.646846), new LatLng(47.378759,8.646739), new LatLng(47.375562,8.651449),  
                 new LatLng(47.37432,8.650172), new LatLng(47.37291,8.652007), new LatLng(47.371639,8.650355), new LatLng(47.370403,8.650634),
                 new LatLng(47.368972,8.650859), new LatLng(47.368071,8.652973), new LatLng(47.368173,8.653337), new LatLng(47.368122,8.653788),
                 new LatLng(47.367904,8.654228), new LatLng(47.367947,8.654593), new LatLng(47.369829,8.653616), new LatLng(47.371057,8.653488), 
                 new LatLng(47.372155,8.65588), new LatLng(47.372656,8.657597), new LatLng(47.373608,8.661545), new LatLng(47.37262,8.663927), 
                 new LatLng(47.371145,8.666051), new LatLng(47.370098,8.667457), new LatLng(47.370055,8.668186), new LatLng(47.369386,8.66839), 
                 new LatLng(47.369241,8.669044), new LatLng(47.367497,8.67074), new LatLng(47.367243,8.670557), new LatLng(47.367293,8.670836), 
                 new LatLng(47.366792,8.671544), new LatLng(47.366836,8.671823), new LatLng(47.365935,8.672746))
                    .strokeWidth(2)
                    .strokeColor(Color.RED)
                    .fillColor(Color.argb(64, 255, 0, 0))); 
        }

    private void addSeeschutzzone() {
        Seeschutzzone = mMap.addPolygon(new PolygonOptions()
            .add(new LatLng(47.367947,8.654593), new LatLng(47.369829,8.653616), new LatLng(47.371057,8.653488), 
                 new LatLng(47.372155,8.65588), new LatLng(47.372656,8.657597), new LatLng(47.373608,8.661545), new LatLng(47.37262,8.663927),
                 new LatLng(47.371145,8.666051), new LatLng(47.370098,8.667457), new LatLng(47.370055,8.668186), new LatLng(47.369386,8.66839), new LatLng(47.369241,8.669044), 
                 new LatLng(47.368187,8.670085), new LatLng(47.368412,8.669001), new LatLng(47.36964,8.667097), new LatLng(47.370715,8.665166),
                 new LatLng(47.371442,8.663599), new LatLng(47.371413,8.661239), new LatLng(47.371006,8.658149), new LatLng(47.369989,8.656261))
                     .strokeWidth(2)
                     .strokeColor(Color.MAGENTA)
                     .fillColor(Color.argb(64, 170, 0, 120)));
    }



    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) { 
                case R.id.bWhereAmI:
                    String locationZurichGreifensee = latLngUster.toString();
                    Bundle basketZurich = new Bundle();
                    basketZurich.putString("location", locationZurichGreifensee);
                    Intent t = new Intent(ZurichGreifensee.this, MainActivity.class);
                    t.putExtras(basketZurich);
                    startActivity(t);
                    break;
                }

                //Switching between normal and hybrid view
                if (mapView.isChecked()) {
                    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                }else {
                    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                }   
        }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu);
        MenuInflater gewasserMenu = getMenuInflater();
        gewasserMenu.inflate(R.menu.gewasserkarte, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
        case R.id.gewasserkarte:
            Intent gewasser_revier_karte = new Intent(ZurichGreifensee.this, GewasserRevierKarte.class);
            startActivity(gewasser_revier_karte);
            break;
        }
        return false;
    }   
}

and this is my xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <CheckBoxPreference
        android:key="naturschutzgebiet"
        android:title="Naturschutzgebiet"
        android:summaryOn="Wege nicht verlassen, Hunde an der Leine führen"
        android:icon="@drawable/naturschutzgebiet"
        android:defaultValue="false" />

    <CheckBoxPreference
        android:key="seeschutzzone_1"
        android:title="Seeschutzzone"
        android:summaryOn="Befahren mit Booten oder anderen Schwimmkörpern sowie Baden ganzjährig verboten"
        android:icon="@drawable/seeschutzzone_1"
        android:defaultValue="false" />

</PreferenceScreen>

To get one of above defined polygons displayed on the map, the appropriate check-box is to be ticked-in in the preference menu. Now, even if I do it the polygon is not visible after I close the preference menu. Instead, I have to restart ZurichGreifensee activity by closing it and starting it again. Only then the code applies the polygon over the map.

My question is, what I would need to change/add within the code above so that polygon gets displayed immediately after I tick-in the desired preference Check-Box and close/ exit the preference menu?

Many thanks in advance.

Budjoni
  • 15
  • 1
  • 1
  • 4

1 Answers1

0

You need to override the onResume method and check if the user has pressed the CheckBox there

@Override
public void onResume () {
    super.onResume();
    //if user has checked the box
    //is rectangle isn't already drawn
    //draw rectangle
}
DDsix
  • 1,966
  • 1
  • 18
  • 23
  • @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); if (naturschutzgebiet.isChecked() && (Naturschutzgebiet == null)); addNaturschutzgebiet(); } below onCreate method throws a NullPointerException Was that correctly placed? – Budjoni Jul 03 '14 at 10:39