-1

Here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

        <include layout="@layout/custom_toolbar"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:visibility="gone"
        android:src="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>

The custom toolbar is rendering perfectly below toolbar. It contains 4 LinearLayouts stacked horizontally inside a LinearLayout, each containing an ImageView and a TextView. They represent different sections of the app and I need to make them clickable but I cannot. The Java code is:

//MainActivity.onCreate()
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
setOnClickListenersOnToolbarItems();
...

private void setOnClickListenersOnToolbarItems(){

        View toolbarView = getLayoutInflater().inflate(R.layout.custom_toolbar, null);

        //LinearLayouts
        View clothingView = toolbarView.findViewById(R.id.clothingView3);
        View sportsView = toolbarView.findViewById(R.id.sportsView3);
        View shoesView = toolbarView.findViewById(R.id.shoesView3);
        View moreView = toolbarView.findViewById(R.id.moreView3);

        clothingView.setOnClickListener(this);
        sportsView.setOnClickListener(this);
        shoesView.setOnClickListener(this);
        moreView.setOnClickListener(this);

}

I have tried the following things:

  1. This is not applicable as it positions my view wrongly:

    getSupportActionBar().setCustomView() //or getCustomView()

  2. The top 2 answers to this question did not work:

onClick not triggered on LinearLayout with child

Community
  • 1
  • 1
Utsav
  • 241
  • 5
  • 10

1 Answers1

2

Give a id to your custom toolbar like this

     <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

        <include 
           android:id="@+id/mCustomToolbar"
           layout="@layout/custom_toolbar"/>

    </android.support.design.widget.AppBarLayout>

then in java

private void setOnClickListenersOnToolbarItems(){

    View toolbarView = findViewById(R.id.mCustomToolbar);

    //LinearLayouts
    View clothingView = toolbarView.findViewById(R.id.clothingView3);
    View sportsView = toolbarView.findViewById(R.id.sportsView3);
    View shoesView = toolbarView.findViewById(R.id.shoesView3);
    View moreView = toolbarView.findViewById(R.id.moreView3);

    clothingView.setOnClickListener(this);
    sportsView.setOnClickListener(this);
    shoesView.setOnClickListener(this);
    moreView.setOnClickListener(this);

}
Tabish Hussain
  • 852
  • 5
  • 13