0

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.

also i am using a toggle button if that can matters, and i want the spinning progressbar not a dialog.

any help?

here my 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);
            }
        }
    });
    }
MoschDev
  • 671
  • 2
  • 14
  • 16

3 Answers3

0

try this way, i am not sure it will help you or not

b1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        final ProgressDialog progress = ProgressDialog.show(THENAMEOFYOURACTIVITYCLASS.this,
                ProgressTitle, ProgressMessage, true, false);

        new Thread(new Runnable() {
            public void run() {
            loadFeed();
            progress.cancel();
            }
        }).start();
    }
});

Be careful about what loadFeed(statutory); do. Because now is working inside a THREAD and Threads con not modify UI, If this is the case then you should do something like this:

b1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        final ProgressDialog progress = ProgressDialog.show(THENAMEOFYOURACTIVITYCLASS.this,
                ProgressTitle, ProgressMessage, true, false);

        new Thread(new Runnable() {
            public void run() {
            loadFeed(); //just load data and prepare the model
            runOnUiThread(new Runnable() {
                             @Override public void run() {
                               //here you can modify UI here
                               //modifiying UI element happen here and at the end you cancel the progress dialog
                               progress.cancel();
                         }
                    }); // runOnUIthread

            }
        }).start();
    }
});
Devangi Desai
  • 1,355
  • 12
  • 17
0
            ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setProgressStyle(R.style.NewDialog);
    progressDialog.setMessage("Loading...");

Paste the below in your styles.xml New Dialog :

<style name="NewDialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">@android:color/transparent</item>
</style>

When you want to show you can cuse progressDialog.show() and to hide you can use progressDialog.dismiss().

Chirag_RB
  • 123
  • 2
  • 9
0

try following code...

handler1 = new Handler()
{
            @Override
            public void handleMessage(Message msg) 
            {
                switch(msg.what)
                {
                case 1:
                    static_class.digi_pd = ProgressDialog.show(Create_Digitizing_Job.this, "Loading...", "Please Wait..", true,false);
                    static_class.digi_pd.setCancelable(false);
                    break;
                case 2:
                    static_class.digi_pd.dismiss();
                    break;

                }
            }
        };

        thread1 = new Thread()
          {
                @Override
                public void run()
                {   
                       try
                        {  


                          handler1.sendEmptyMessage(1);

                    // write your code here....

                         handler1.sendEmptyMessage(3);
                         handler1.sendEmptyMessage(2);

                        } 
                       catch (Exception e) 
                       {
                           Log.w("thread error...",""+e);
                           //e.printStackTrace();
                       }   

             }

         };
        thread1.start();
Parag Ghetiya
  • 421
  • 2
  • 14