Changing Action Bar Colour Programmatically may cause nullPointerException?

725 Views Asked by At

I tried to change the colour of my action bar in Android app using the following line of code:

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.background_actionbar)));

However, this gives a warning that reads:

Method invocation 'getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.backgr...' may produce 'java.lang.NullPointerException'

Any ideas how to work around this?

Note: I am changing the colour programmatically because changing it via XML theme/style didn't work.

Using minimum SDK 16.

Testing on Android 4.4.4 device.

1

There are 1 best solutions below

0
Ashish Tiwari On

Yes, If you are using themes with NoActionBar then you will get the NullPointerException.

Try this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // The Action Bar is a window feature. The feature must be requested
    // before setting a content view. Normally this is set automatically
    // by your Activity's theme in your manifest. The provided system
    // theme Theme.WithActionBar enables this for you. Use it as you would
    // use Theme.NoTitleBar. You can add an Action Bar to your own themes
    // by adding the element <item name="android:windowActionBar">true</item>
    // to your style definition.
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

    setContentView(R.layout.main);

    // experiment with the ActionBar 
    ActionBar actionBar = getActionBar();
    actionBar.setBackgroundDrawable(new  ColorDrawable(getResources().getColor(R.color.background_actionbar)));
        //actionBar.hide();
}

or

You can use Toolbar

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:background="@color/light_blue">
</android.support.v7.widget.Toolbar>

Include it in activity's layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include layout="@layout/toolbar" />
    </LinearLayout>
</RelativeLayout>

Use this code to Activity:

public class YourActivity extends AppCompatActivity {
    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);

        // Set a Toolbar to replace the ActionBar.
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //toolbar.setTitle("Setting");
    }

    public void setSupportActionBar(@Nullable Toolbar toolbar) {
        getDelegate().setSupportActionBar(toolbar);
    }
}