Question

This is my code(Edited):

public class Main extends Activity
{

TextView txt;

@Override
public void onCreate(Bundle savedInstanceState) 
{       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  

    Button b=(Button) findViewById(R.id.button1);
      txt = (TextView) findViewById(R.id.textView1);   

      b.setOnClickListener(new Button.OnClickListener() 
      {

         public void onClick(View v) 
         {
             Intent intent= new Intent(Main.this,Preference.class);
             startActivity(intent);
         }
      });

}     

public void onResume()
{
    super.onResume();
    Toast.makeText(this, "onResume", Toast.LENGTH_LONG).show();
    SharedPreferences myPreference=PreferenceManager.getDefaultSharedPreferences(this);
    boolean first=myPreference.getBoolean("first", true);
    if(first)
    {

     Typeface font=Typeface.MONOSPACE;
     txt.setTypeface(font);
    }   
}

}

In main.xml ,Here there is textview whose text style is needed to be changed.The previous problem restarting the application to take effect check box preference is not needed now. But problem is that when i uncheck the checkbox preference its not going to its default state.Y so? Can any help me whats wrong in code?

Was it helpful?

Solution

I think you are using preferences to change a font?

You check a box and expect a font to change based on whether it is checked or not?

When you return to your activity after changing the preference this does not cause your onCreate() method to run as the activity already exists on your back stack and is simply being re-displayed.

You should put the font change code in the onResume() method or use the startActivityForResult method and handle the closing of preferences there.

Update:

if (first) {
     txt.setTypeface(Typeface.MONOSPACE);
} else {
     txt.setTypeface(Typeface.SERIF);
}

OTHER TIPS

Try calling txt.requestLayout(). This will repaint the view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top