Javacadabra
Javacadabra

Reputation: 5768

Changing the icon in actionbar

I would like to place the logo for my application in the actionbar at the top of the screen. Currently displayed is the default green android launcher icon.

enter image description here

I have tried the following but saw no change:

I went into my manifest file and altered the android:icon code...

 <application
    android:allowBackup="true"
    android:logo="@drawable/logo"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

This did not work, So I then created an ActionBar object within my code and used the setIcon method.

ab.setIcon(R.drawable.logo);

However the above line of code generates the following runtime error:

**java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.waitronproto3/com.example.waitronproto3.MainActivity}: java.lang.NullPointerException **

Can anybody see why this is happening, The resource R.drawable.logo exists because I can see it in the folder and no errors are generated within the IDE.

Any help is much appreciated.

Upvotes: 0

Views: 427

Answers (2)

Javacadabra
Javacadabra

Reputation: 5768

Everything was right except I forgot to get a reference to the ActionBar. The code below solved the problem.

ab = this.getActionBar();
    ab.setIcon(R.drawable.logo);

Upvotes: 0

Oleg Vaskevich
Oleg Vaskevich

Reputation: 12682

Yeah, there is no android:logo property as far as I know. You can't just create an ActionBar... have you used this?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(...);
    getActionBar().setLogo(R.drawable.logo)`
}

Upvotes: 1

Related Questions