3

I am trying to make app's logo clickable using AppCompatActivity. The logo is in the home location of the toolbar and left-aligned. The logo displays correctly but the logo is not clickable. What am I missing here?

Activity.java:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    assert getSupportActionBar() != null;
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setLogo(R.drawable.ic_s_logo_48x48);
    getSupportActionBar().setDisplayUseLogoEnabled(true);
}

@Override
public boolean onOptionsItemSelected (MenuItem item){
         switch (item.getItemId()) {
         case R.id.home:
             Toast toast1 = Toast.makeText(MainActivity.this, "This is the homepage", Toast.LENGTH_LONG);
             toast1.setGravity(Gravity.CENTER_VERTICAL,0,0);
             toast1.show();
             return true;             
      default:
         return super.onOptionsItemSelected(item);
     }
}

toolbar.xml:

<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/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/home"
    android:title="@string/action_logo"
    android:contentDescription="@string/action_logo"
    android:icon="@drawable/ic_s_logo_48x48"
    android:gravity="start"
    android:clickable="true"
    app:showAsAction="always" />

</android.support.v7.widget.Toolbar>   
Raptor
  • 53,206
  • 45
  • 230
  • 366
AJW
  • 1,578
  • 3
  • 36
  • 77

1 Answers1

3

You can use another way to handle your home ImageButton clicked like this

ImageButton ivHome = (ImageButton) toolbar.findViewById(R.id.home);

ivHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Log("TAG","Home image click");
            }
});

====
OR

You can use setNavigationIcon for your Toolbar (it will display like your current home button)
To do that

1.Create toolbar xml like this

<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/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar> 

2.Change toolbar navigation icon in your Activity

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.your_home_icon));    

3. Handle event when click

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         Log.d("TAG", "home clicked");
     }
});

Hope this help

Linh
  • 57,942
  • 23
  • 262
  • 279
  • Thanks, I will try that. Do I assume correctly that I could use similar code for a TouchListener? – AJW Dec 28 '15 at 03:06
  • I tried the code you recommended and unfortunately it did not work. The icon seems frozen, like it is not enabled to accept clicks. When the overflow icon is clicked, a gray shadow appears momentarily to show that the icon has been clicked. When I click the home icon, nothing happens. Any thoughts? – AJW Dec 28 '15 at 03:13
  • inside onClick, you should add some `logcat` for check, maybe the Toast can cause problem. then tell me does the locat show or not – Linh Dec 28 '15 at 03:17
  • I replaced the Toast with an Intent on the onClick section and still no luck. – AJW Dec 28 '15 at 03:23
  • Can you change your `ImageView` to `Button` or `ImageButton`. It can easy to handle click event – Linh Dec 28 '15 at 03:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99081/discussion-between-phan-vn-linh-and-ajw). – Linh Dec 28 '15 at 03:27