Reputation: 139
I'm currently trying to pass the current value of a seek bar between activities in order to change the size of text in another activity. However I'm having difficulties doing this.
secondActivityButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startIntent = new Intent(getApplicationContext(), SecondActivity.class);
startIntent.putExtra("com.example.willh.seekbar.something", seekBarValue);
startActivity(startIntent);
}
});
I'm able to pass static variables between the activites, e.g. creating an int named seekBarValue with the value of 5, however I need the variable to be dynamic.
slidingBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int seekBarValue = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBarValue = progress;
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
TextView resultText = (TextView) findViewById(R.id.resultText);
resultText.setText(seekBarValue + "");
resultText.setTextSize(seekBarValue);
}
});
Any help would be greatly appreciated, thanks.
Upvotes: 0
Views: 1251
Reputation: 498
Create an instance variable. Try that:
public class YOURCLASS {
private int seekBarValue;
...(other code)...
slidingBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBarValue = progress;
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
TextView resultText = (TextView) findViewById(R.id.resultText);
resultText.setText(seekBarValue + "");
resultText.setTextSize(seekBarValue);
}
});
secondActivityButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startIntent = new Intent(getApplicationContext(), SecondActivity.class);
startIntent.putExtra("com.example.willh.seekbar.something", seekBarValue);
startActivity(startIntent);
}
});
}
Upvotes: 1
Reputation: 271
Your problem is with following command:
startIntent.putExtra("com.example.willh.seekbar.something", seekBarValue);
You need to declare your KEY variable in your first activity while passing values in intents. like follows
public static final String KEY = "key";
Then do the following in your first activity-
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putInt(Key, IntValue);
intent.putExtra(bundle);
startActivity(intent);
in onCreate() method of your SecondActivity do the following-
int seekBarValue = getIntent().getIntExtra(FirstActivity.KEY,0);
Now the value will be in seekBarValue variable. You can now use it in your seconActivity as well.
Let me know if this doesn't work. Also if it crashes send logcat output from android monitor.
Upvotes: 0