DAVIDBALAS1
DAVIDBALAS1

Reputation: 484

ImageView in action bar

I am trying to put an ImageView inside the action bar, it works, and I can animate it and everything, however, it takes too much horizontal space and I don't know why.

This is my menu:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

    <ImageView
        android:id="@+id/dicebutton"
        android:layout_width="wrap_content"
        android:layout_height="?attr/actionBarSize"
        android:contentDescription="dice"
        android:src="@drawable/dice_1" />
</android.support.v7.widget.Toolbar>

And this is the code in the Activity:

if (myToolbar != null) {
        setSupportActionBar(myToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setElevation(0);
        image = myToolbar.findViewById(R.id.dicebutton);
        if (image != null) {
            image.setImageResource(R.drawable.dice_1);
            image.setScaleX(0.5f);
            image.setScaleY(0.5f);
            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    rollDice(Helper.rand.nextInt(6) + 1, v);
                }
            });

        }

    }

Here is an image of the result: Result

The image I am using is a 256x256 dice image, why is it enlarging it to take all this space?

Upvotes: 0

Views: 48

Answers (1)

behzad.robot
behzad.robot

Reputation: 707

I too had so many problems with drawing and adjusting views in actionbar before. I think you should try Support Library Toolbar , it gives you a lot more control with this cases , also supports Menu inflate things as far as i recall: https://developer.android.com/training/appbar/setting-up

https://developer.android.com/reference/android/support/v7/widget/Toolbar

You can put images and other views inside this , with much more control.

Updated Answer: I'm not sure if this is what you need but try adding android:adjustViewBounds="true" to your image.

Turns sth like this:

enter image description here

To sth like this: enter image description here

Upvotes: 2

Related Questions