P
P
Pavel Dmitriev2014-10-22 17:58:59
Android
Pavel Dmitriev, 2014-10-22 17:58:59

How can state be saved in AlertDialog?

How can I save the state in the activity that changes in the AlertDialog?
I have SeekBar in AlertDialog, AlertDialog is called via menu. SeekBar resizes the text in the TextView in the activity. It is necessary that after restarting the application, the text size is preserved.
java code

public class AndjeloOne extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
setContentView(R.layout.andjelo_one);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
  
private static final int MENU_SIZE = 1;

@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuItem add = menu.add(0, MENU_SIZE, 0, getString(R.string.menu_size));
add.setIcon(R.drawable.ic_size);
add.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}

//Назад к родительскому активити
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
case MENU_SIZE:
CreateView(this);
break;
}
return super.onOptionsItemSelected(item);
}
private void CreateView(final Context context){
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(AndjeloOne.this);
LayoutInflater inflater = AndjeloOne.this.getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_add, null);
builder.setView(v);
builder.setPositiveButton("Сохранить", new DialogInterface.OnClickListener(){

@Override
public void onClick(DialogInterface dialog, int position) {
}
});
builder.setTitle("Размер шрифта");
final SeekBar sbBetVal = (SeekBar)v.findViewById(R.id.seekBar1);
final TextView tvBetVal = (TextView)v. findViewById(R.id.tvBetVal);
final TextView t1=(TextView) findViewById(R.id.textViewNulin);
sbBetVal.setMax(24);
sbBetVal.setProgress(0);
sbBetVal.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
int size = 12;
t1.setTextSize(size + progress);
tvBetVal.setText(String.valueOf(progress));
}
});
builder.create();
builder.show();
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Pukhanov, 2014-10-22
@vpukhanov

You can store the font size in SharedPreferences in onProgressChanged and then get it from there when the application starts.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question