0

I am trying to create a progress bar which does not need to show percentages.

public boolean onOptionsItemSelected( MenuItem item ) {
    switch( item.getItemId() ) {
        case R.id.back:
            this.dispatchKeyEvent( new KeyEvent( KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK ) );
            finish();
            return true;
        case R.id.read:
            // This is where I would start my spinner....
            if( myService.getState() != 3 ) {
                myService.connect( MainMenu.previousDevice, true );
            }
            readWaves();
            return true;
        default:
            return super.onContextItemSelected( item );
    }
}

The spinner will stop itself in the readWaves() function...

private void readWaves() {
        waveResults = RelayAPIModel.NativeCalls.GetWavesJava( RelayAPIModel.WAVES_V );
        // Turn the Spinner off Here.
        doOtherThings();
    }

I have found tons of code all over telling me how to use a progress bar using threads, adding the progress bar in the xml file, and many other methods.

I think the best way for me to do it would be to create a CustomDialog class to pop up and have the spinner spin until I call to end it.

The first answer shown here is by far the closest I have come to finding what I want, but unfortunately his solution does not work. The reason it does not work is because of the last section of code:

public MyProgressDialog(Context context) {
    super(context, R.style.NewDialog);
}

I am using Android 2.3 and there is no style attribute of R. I try to add it myself and get an error. Does anyone know how to fix this?

EDIT:

This is the code I tried using a ProgressDialog

case R.id.read:
            pDialog = new ProgressDialog( Waves.this );
            pDialog.setTitle( "Waves" );
            pDialog.setMessage( "Reading..." );
            pDialog.show();
            if( myService.getState() != 3 ) {
                myService.connect( MainMenu.previousDevice, true );
            }
            readWaves();
            return true;
        default:
            return super.onContextItemSelected( item );
    }
}

private void readWaves() {
    if( spinnerChoice == 0 ) {
        Log.i( "IN IF", "IN IF" );
        // Diff Voltage
        waveResults = RelayAPIModel.NativeCalls.GetWavesJava( RelayAPIModel.WAVES_V );
    } else {
        // Current 
        waveResults = RelayAPIModel.NativeCalls.GetWavesJava( RelayAPIModel.WAVES_I );
    }
    Log.i( "READ WAVES", "After If/Else" );
    pDialog.dismiss();
    /*
Community
  • 1
  • 1
JuiCe
  • 4,132
  • 16
  • 68
  • 119
  • do you have a style name NewDialog in your styles.xml file ? if so, are you sure you import the correct R class ? – njzk2 Dec 14 '12 at 15:33
  • I do not know where to find a `styles.xml` file. I am relatively new to Android, I assumed `R.style.NewDialog` worked similarly to calling `setContentView( R.layout.main )`, so `style` would be a directory in my project. Is there a `styles.xml` file somewhere? – JuiCe Dec 14 '12 at 15:35
  • 1
    style.xml goes to res/values(-*) – njzk2 Dec 14 '12 at 16:28

1 Answers1

1

There is already a Dialog that is created for this specific purpose. Check out ProgressDialog. You can use it like this:

//show the dialog
ProgressDialog pd = new ProgressDialog(YourActivity.this);
pd.setTitle("Your title");
pd.setMessage("Helpful message to the user");
pd.show();

/**************
* Do some stuff
**************/

//Hide the dialog
pd.dismiss();

EDIT: Here is a small sample of how it would look in the options menu:

public boolean onCreateOptionsMenu(Menu menu) {  
    menu.add(0, 42, 0, "Option"); 
    return true;  
}  
public boolean onOptionsItemSelected(MenuItem item) {  
    switch (item.getItemId()) {  
    case 42: 
           ProgressDialog pd = new ProgressDialog(this);
           pd.show();
        break;
    default:
        //Do nothing      
    }  
    return false;  
}
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • @JuiCe did you call `.show()` ? If possible edit your question with the code that you tried – FoamyGuy Dec 14 '12 at 15:42
  • Yes I did. I edited my original post to show my code. I did try adding the initialization and `.show()` call in my `onCreate()` method and it worked. I'm assuming the code somehow skips over the Dialog because it jumps right to the `waveResults = ...` line in `readWaves()`, but I don't understand why. Also, when the dialog was in my `onCreate()` method it was cancelled by simply pressing the hardware back button, which does not seem correct. Any idea about that? – JuiCe Dec 14 '12 at 15:49
  • to get rid of that use `pDialog.setCancelable(false);` before you show it. The back button won't clear it away then. – FoamyGuy Dec 14 '12 at 15:51
  • nope sorry =(. I just put together a small sample that shows it from an options selection same as you are and it shows fine on my device with this code that I just added to my answer. – FoamyGuy Dec 14 '12 at 15:55
  • Oh well, I'm assuming its because the function I'm calling inbetween is a JNI call and things get messy. Thanks anyway. – JuiCe Dec 14 '12 at 15:57