Search code examples
javaandroidlayoutsharedpreferencessetbackground

How do I set the Background/ Layout of a class from a different class in Android


I am working with some code where I want to change the background image dynamically while referencing the shared preferences. An example of an activity I have is this:

public class Splash extends Activity {
    protected void onCreate(Bundle inputVariableToSendToSuperClass) {

        super.onCreate(inputVariableToSendToSuperClass);
        setContentView(R.layout.splash);
        Initialize();

        //Setting background
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String user_choice = prefs.getString("pref_background_choice","blue_glass");
        LinearLayout layout = (LinearLayout) findViewById(R.id.activity_splash_layout);
        ManagePreferences mp = new ManagePreferences();
        mp.setTheBackground(Splash.this, user_choice, layout);

        //More code after this...
    }
}

The ManagePreferences class looks like this:

public class ManagePreferences {

    //Empty Constructor
    public ManagePreferences(){
    }

    public void setTheBackground(Context context, String background_choice, LinearLayout layout){
        if (background_choice == "blue_glass"){
            layout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.blue_glass));
        } else if (background_choice == "blue_oil_painting")


             //etc... with more backgrounds
        }
}

The problem is, the code for setting the background is not working from a different class. I can get the code to work if I copy it into the Splash activity, but not if I reference the class and call the method; I would prefer to not clutter my code.

All I am trying to do is change the layout (setBackgroundDrawable) in the Splash Activity by making a call to this ManagePreferences class.

Thanks all!


Solution

  • 1) You doing it wrong. You shouldn't create Activity directly by using new.

    2) You should open new Activity using Intent and set arguments to it.

    Intent intent = new Intent(context, ManagePreferences.class);
    intent.putExtra("user_choice", user_choice);
    startActivity(intent);
    

    And in ManagePreferences get it:

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String user_choice = extras.getString("user_choice");
    }
    

    UPD: If you are using ManagePreferences just like utility class, make setTheBackground static:

    public static void setTheBackground(Context context, String background_choice, LinearLayout layout){
            if (background_choice == "blue_glass"){
                layout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.blue_glass));
            } else if (background_choice == "blue_oil_painting")
    
    
                 //etc... with more backgrounds
            }
            layout.requestLayout();
         }
    

    and call it:

    ManagePreferences.setTheBackground(this, user_choice, layout);
    

    UPD: as answered here, you cannot do this. When you refer a layout file using findViewById(), the android system looks for this in your current ContentView only. (i.e the view which you have set using setContentView() for the current activity).