'Trying to pass a background color through intentPutExtra

I am trying to set the background color with intentPutExtra but it doesn't seem to change, this is my intent from the MainActivity:

  @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {

                    Intent intent = new Intent(MainActivity.this, ktmList.class);
                   intent.putExtra("backgroundColor", getResources().getColor(R.color.colorOrange));
                    startActivity(intent);
}

and this is how I try to change the background color:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.models_list);

    Intent intent = new Intent();
    int backgroundColor = intent.getIntExtra("backgroundColor", -1);

    LinearLayout rootView = (LinearLayout)findViewById(R.id.models_rootView);
    rootView.setBackgroundColor(backgroundColor);

and this is the models_list xml file:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/models_rootView"
    android:orientation="vertical">

<ListView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/models_list"
                android:padding="16dp"/>

</LinearLayout>


Solution 1:[1]

Use rootView.setBackgroundResource(backgroundColor); instead of rootView.setBackgroundColor(backgroundColor);

Also change

intent.putExtra("backgroundColor", getResources().getColor(R.color.colorOrange));

to

intent.putExtra("backgroundColor", R.color.colorOrange);

That worked for me

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1