Codeable Codeable - 13 days ago 11
Android Question

Android Progress Bar reset and start over on some score value

I´m making a simple game where user clicks and score will increase its value. If score is on some point like 200, beast will evolve on different imageView and so on.

Problem is that if I reach point in my if statement it works but when I change activity by clicking upper button and then change it back to the mainActivity Progress bar change its state to different value, than i click and it shows correct filling.

How can it be fixed? I need also progress saves itself and fill the bar after closing and opening the app.... I have Score and Proggress values saved in SharedPreferences so i don´t know where is problem. (sorry for EN). Adding some pictures to make it easier to understand.

how it works

My Main Activity's code:

public class MainActivity extends Activity
{

int score;
int progress = score;
ImageButton beast;
SoundPool sp = new SoundPool(15, AudioManager.STREAM_MUSIC, 0);
ProgressBar mProgress;

DecimalFormat df = new DecimalFormat("#,###,###");
private ImageView mScanner;
private Animation mAnimation;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);

setVolumeControlStream(AudioManager.STREAM_MUSIC);

final int soundDd = sp.load(this, R.raw.menu, 1);


mProgress = (ProgressBar) findViewById(R.id.progressBar);

mProgress.getProgressDrawable().setColorFilter(
Color.rgb(0, 199, 140), android.graphics.PorterDuff.Mode.SRC_IN);
mProgress.setScaleY(5f);


fontChange();
addIntegerValue();

mScanner = (ImageView) findViewById(R.id.imageChanger);
mScanner.setVisibility(View.VISIBLE);
mAnimation = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.05f);
mAnimation.setDuration(2000);
mAnimation.setRepeatCount(- 1);
mAnimation.setRepeatMode(Animation.REVERSE);
mAnimation.setInterpolator(new LinearInterpolator());
mScanner.setAnimation(mAnimation);


score = PreferenceManager.getDefaultSharedPreferences(this).getInt("value", score);

TextView ts = (TextView) findViewById(R.id.textview2);
beast = (ImageButton) findViewById(R.id.beastButton);
beast.setSoundEffectsEnabled(false);


if (progress >= 0 && progress <= 199)
{

mProgress.setMax(200);

mScanner.setBackgroundResource(R.drawable.worm);

}
if (progress >= 200 && progress <= 399)
{

mProgress.setMax(400);

mScanner.setBackgroundResource(R.drawable.worm);

}
if (progress >= 400 && progress <= 599)
{

mProgress.setMax(600);

mScanner.setBackgroundResource(R.drawable.worm);

}
if (progress >= 600 && progress <= 799)
{

mProgress.setMax(800);

mScanner.setBackgroundResource(R.drawable.worm);
}


beast.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub

sp.play(soundDd, 1, 1, 0, 0, 1);
Intent i = new Intent(getApplicationContext(), BeastSelect.class);
startActivity(i);
}
});


}


public void fontChange()
{
//font set
TextView ts = (TextView) findViewById(R.id.textview2);
TextView tl = (TextView) findViewById(R.id.textLadder);
TextView tb = (TextView) findViewById(R.id.textBeast);
TextView te = (TextView) findViewById(R.id.textevolve);

int low = PreferenceManager.getDefaultSharedPreferences(this).getInt("value", score);

mProgress.setProgress(low);


ts.setText("SCORE : " + " " + df.format(low));
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/Phage Regular.otf");
ts.setTypeface(custom_font);
tl.setTypeface(custom_font);
tb.setTypeface(custom_font);
te.setTypeface(custom_font);
//font set over
}


public void addIntegerValue()
{
RelativeLayout rl = (RelativeLayout) findViewById(R.id.activity_main);
rl.setSoundEffectsEnabled(false);

final TextView ts = (TextView) findViewById(R.id.textview2);
final int soundId = sp.load(this, R.raw.coin, 1);


rl.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
score++;

mProgress.setProgress(score);
sp.play(soundId, 1, 1, 0, 0, 1);

ts.setText("SCORE : " + " " + df.format(score));

if (progress >= 0 && progress <= 199)
{

mProgress.setMax(200);

mScanner.setBackgroundResource(R.drawable.worm);

}
if (progress >= 200 && progress <= 399)
{

mProgress.setMax(400);

mScanner.setBackgroundResource(R.drawable.worm);

}
if (progress >= 400 && progress <= 599)
{

mProgress.setMax(600);

mScanner.setBackgroundResource(R.drawable.worm);

}
if (progress >= 600 && progress <= 799)
{

mProgress.setMax(800);

mScanner.setBackgroundResource(R.drawable.worm);
}


}

});


}


protected void onPause()
{
super.onPause();

PreferenceManager.getDefaultSharedPreferences(this).edit().putInt("value", score).apply();

}


protected void onDestroy()
{
super.onDestroy();
sp.release();
sp = null;
}

}


My Second Activity's code:

public class BeastSelect extends Activity
{

ImageButton back;
SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_beast_select);

setVolumeControlStream(AudioManager.STREAM_MUSIC);

final int soundDd = sp.load(this, R.raw.menu, 1);

back = (ImageButton) findViewById(R.id.imageBack);
back.setSoundEffectsEnabled(false);
fontChange();


back.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
sp.play(soundDd, 1, 1, 0, 0, 1);
Intent b = new Intent(getApplicationContext(), MainActivity.class);
startActivity(b);
}
});


}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
// do something on back.
Intent b = new Intent(getApplicationContext(), MainActivity.class);
startActivity(b);
return true;
}

return super.onKeyDown(keyCode, event);
}


public void fontChange()
{
//font set
TextView hd = (TextView) findViewById(R.id.beasttext);
TextView cm = (TextView) findViewById(R.id.textcommon);
TextView tm = (TextView) findViewById(R.id.textmedium);
TextView pr = (TextView) findViewById(R.id.textpro);


Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/Phage Regular.otf");
hd.setTypeface(custom_font);
cm.setTypeface(custom_font);
tm.setTypeface(custom_font);
pr.setTypeface(custom_font);
//font set over
}
}

Answer

in your activity onResume method do this:

mProgress = (ProgressBar) findViewById(R.id.progressBar);
score = PreferenceManager.getDefaultSharedPreferences(this).getInt("value", score);
mProgress.setProgress(score);
Comments