Jacobo Koenig Jacobo Koenig - 2 months ago 16
Android Question

Setting Logo on Android Toolbar using the Toolbar's size

I am trying to set a logo on the center of my app's toolbar on the main activity, but since the logo is in HD, this results in the toolbar increasing it's size to accomodate the logo's size. Is there a way to decrease the logo's size proportionally to be set correctly on the toolbar?

Here is my code:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setLogo(R.drawable.logotrans);

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:titleTextColor="@android:color/white"></android.support.v7.widget.Toolbar>

<style name="ToolbarStyle" parent="@style/ThemeOverlay.AppCompat.ActionBar">
<item name="colorControlNormal">@color/white</item>
</style>


And here is the result:

Toolbar

Answer

Toolbar in Android is a ViewGroup. Therefore you can place widgets inside the Toolbar as its child. Also to make the log the size of the toolbar, give a definite height to toolbar and set the Image size to match_parent. For example:

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/black">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/ic_clock" 
        android:layout_gravity="center_horizontal"
        />
    </FrameLayout>
</android.support.v7.widget.Toolbar>

Still I would recommend to reduce the size of the logo as it can cause memory leaks in future.