title: Android - how to hide / show progressbar onclick
body: <p>I am trying to show a progressbar when a button is clicked. When i test the app it force closes / stops. My app works fine before the progressbar code is added in. </p>
  
  <p>also i am using a toggle button if that can matters, and i want the spinning progressbar not a dialog.</p>
  
  <p>any help?</p>
  
  <p>here my code:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      button = (Button) findViewById(R.id.buttonFlashlight);
      Context context = this;
      PackageManager pm = context.getPackageManager();
  
  
      if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
          Log.e("err", "Device has no camera!");
          return;
      }
  
      camera = Camera.open();
      final Parameters p = camera.getParameters();
  
      button.setOnClickListener(new OnClickListener() {
          public void onClick(View arg0) {
  
              if (isLighOn) {
  
                  progressBar1.setVisibility(View.INVISIBLE);
  
                  final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.drawable.click);  
                  mp1.start(); 
  
                  Log.i("info", "torch is turn off!");
                  p.setFlashMode(Parameters.FLASH_MODE_OFF);
                  camera.setParameters(p);
                  camera.stopPreview();
                  isLighOn = false;
  
                  WindowManager.LayoutParams params = getWindow().getAttributes();
                  params.screenBrightness = 1.0f;
                  getWindow().setAttributes(params);
  
              } else {
  
                  progressBar1.setVisibility(View.VISIBLE);
  
                  final MediaPlayer mp1=MediaPlayer.create(getBaseContext(), R.drawable.click);  
                  mp1.start(); 
  
                  Log.i("info", "torch is turn on!");
                  p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                  camera.setParameters(p);
                  camera.startPreview();
                  isLighOn = true;
  
                  WindowManager.LayoutParams params = getWindow().getAttributes();
                  params.screenBrightness = 0.05f;
                  getWindow().setAttributes(params);
              }
          }
      });
      }
  </code></pre>