Reputation: 4201
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();
/*
Upvotes: 0
Views: 1328
Reputation: 46856
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;
}
Upvotes: 1