Home Ask Login Register

Developers Planet

Your answer is one click away!

Craig Gallagher February 2016

Moving Logo on action bar Android Studio

I have put an image from my drawable folder and added it onto my action bar and would now like to move it left over beside the back arrow. Anyone any ideas on how to do this in Android Studio. Thanks

My Code is as fallows

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayUseLogoEnabled(true);
    getSupportActionBar().setLogo(R.drawable.mainlogo);

enter image description here

Answers


Da-Jin C February 2016

Try using a Toolbar, the new way to make AppBars. It is more easily customized and reused, among other advantages.

Android's docs make it seem a lot harder than it really is. They use Toolbar but connect it to the AppBar API's still. If you use a lot of ActionBar utility methods like ActionBar.hide(), then it makes sense to call

setSupportActionBar(myToolbar);

But it can actually be significantly easier. Why you don't need to setSupportActionBar

According to the article above, if we don't use the AppBar API's we don't need the custom theme, onCreateOptionsMenu(), or any of the AppBar mess.

Here is how to do Toolbars simply:

  1. Get v7 appcompat

  2. I think it is neccessary still to make your Activity extend AppCompatActivity

  3. Add the following code:

At to the top of your layout resource file, inside the top level Layout of course:

<android.support.v7.widget.Toolbar
   android:id="@+id/my_toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:background="?attr/colorPrimary"
   android:elevation="4dp"
   android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
   app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>

In your activity:

protected void onCreate(Bundle savedInstanceState) {

    ...
    //Get the toolbar
    Toolbar toolbar = ((Toolbar)findViewById(R.id.my_toolbar));
    //Set up the toolbar's navigation icon and behavior
    toolbar.setNavigationIcon(R 

Post Status

Asked in February 2016
Viewed 1,932 times
Voted 8
Answered 1 times

Search




Leave an answer


Quote of the day: live life