Reputation: 235
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);
Upvotes: 0
Views: 1170
Reputation: 321
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:
Get v7 appcompat
I think it is neccessary still to make your Activity extend AppCompatActivity
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.drawable.ic_close_white_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//whatever you want to happen on click
}
});
}
Optionally you can do things like:
toolbar.inflateMenu(R.menu.save_menu);
toolbar.setTitle("Hey");
Heres where the custom behavior you wanted comes in. If you want an ImageView in the toolbar, you simply add it to the layout resource:
<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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_logo"/>
</android.support.v7.widget.Toolbar>
In fact, if you want a title with custom styles, instead of banging your head against the wall trying to figure out Toolbar styling, simply add a TextView where the ImageView is. You can modify that however you want, and make it your title!
Upvotes: 1