1

I'm using getSupportActionBar().setIcon(R.drawable.ic_transparent); to set the icon of my toolbar. How do I get the ImageView from that icon? I need the Rect of the icon.

I'm trying to implement a version of this with a Toolbar instead of the old action bars.

EDIT: I came up with an alternate solution that puts a TextView and an ImageView in the toolbar instead of using the built in logo and title. This works fine on Android 5.0, but the text doesn't show up on anything below that. Any suggestions to fix either this or the above?

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="@android:color/transparent"
    android:elevation="10dp"
    tools:ignore="UnusedAttribute">

    <ImageView
        android:id="@+id/toolbar_icon"
        android:layout_width="?android:attr/actionBarSize"
        android:layout_height="?android:attr/actionBarSize"
        android:padding="10dp"
        android:scaleType="fitStart"
        android:src="@drawable/ic_transparent" />

    <TextView
        android:id="@+id/toolbar_title"
        style="@style/Base.TextAppearance.AppCompat.Widget.ActionBar.Title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="hello" />

</android.support.v7.widget.Toolbar>
sargunv
  • 2,548
  • 1
  • 13
  • 11

3 Answers3

1

Hard to say.

Right answer: you should not mess with internal widgets that were not designed by yourself (i.e., if there's not a getter method for that ImageView, you should probably leave it where it is). See here.

A bit more hacky answer: you might want to try this custom method. It searches for views depending on their ContentDescription. I never tried, but it could work.

Community
  • 1
  • 1
natario
  • 24,954
  • 17
  • 88
  • 158
  • Where would I get the content description for the logo? It's a different element from the navigation icon AFAIK. – sargunv Jan 06 '15 at 00:18
  • I think it should work as well. If it doesn't, use toolbar.setNavigationIcon() instead of actionbar.setIcon(). – natario Jan 06 '15 at 00:26
  • I don't want to use the navigation icon because it's clickable. Anyways, I solved it now. – sargunv Jan 06 '15 at 00:42
0

I give it a look and just saw a private function getActionBarIconView() which return an imageview inside the activity NoBoringActionBarActivity just change it to public if you need to use it in a external fragment.

0

I solved it by adding my own ImageView and TextView to the toolbar to replace the logo and title, and then fixed the invisible title on android 4.0-4.4 by moving the toolbar to the end of the layout file. I'm not sure why I had to do that, but it worked.

sargunv
  • 2,548
  • 1
  • 13
  • 11