I know you can add an icon image to the app bar, however I was wondering if there is a way to put a larger logo image in the app bar on Android. The picture attached has it behind the app bar but I'm trying to get it inside the app bar. Is this possible?
Asked
Active
Viewed 2.3k times
3
-
http://stackoverflow.com/a/16029214/2826147 – Amit Vaghela Feb 12 '16 at 09:18
4 Answers
5
1) Use latest Android Toolbar
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Place your ImageView and Image-->
<!-- mention image size-->
</android.support.v7.widget.Toolbar>
Example code:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<LinearLayout
android:id="@+id/back_menu_ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_logo"
/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="@color/title_first_textcolor"
android:textSize="@dimen/abc_text_size_title_material"
android:gravity="left"
/>
</android.support.v7.widget.Toolbar>
2) You can set logo programmatically:
Toolbar tool_bar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(tool_bar);
tool_bar.setLogo(R.drawable.image);
For more information about Toolbar, check this link: http://developer.android.com/reference/android/widget/Toolbar.html

hoat4
- 1,182
- 12
- 9

Brahmam Yamani
- 4,475
- 5
- 21
- 30
-
Does this code go in the app_bar_home_page.xml file or some other location? I get an error: Error parsing XML: mismatched tag at this line "" – Casey Feb 12 '16 at 19:07
-
3
For the latest API xml should be,
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
Activity should be,
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setLogo(R.drawable.ic_call_black_24dp);
setSupportActionBar(toolbar);
}

Vivek C A
- 1,045
- 1
- 8
- 14
-
1Tried this and got an error: java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.csusb.ossmassistant/edu.csusb.ossmassistant.home_page}: java.lang.IllegalStateException: Already attached – Casey Feb 12 '16 at 19:20
-
I got the error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setLogo(int)' on a null object reference – ChumiestBucket Oct 26 '18 at 18:08
1
As your image is large and rectangle you can set it in background of your action bar :
final ActionBar actionBar = getActionBar();
BitmapDrawable background = new BitmapDrawable (BitmapFactory.decodeResource(getResources(), R.raw.actionbar_background));
actionBar.setBackgroundDrawable(background);
For this you can create one activity
say "BaseActivity" and add snippet in onCreate
method of it and extend that activity in your all activity where you want to show your change
Or you can have your different square logo like app icon and set it to your action bar icon.
public BaseActivity extends Activity{
@override
public void onCreate(Bundle bundle){
/// add snippet
}
}
yourActivity/yourActivities extends BaseActivity

KDeogharkar
- 10,939
- 7
- 51
- 95
0
You need to add imageview inside the toolbar

Rohit
- 49
- 1
-
I tried this and it gave me an error saying it had to be a color value starting with # – Casey Feb 12 '16 at 19:08