androidandroid-toolbarandroid-collapsingtoolbarlayoutmenu-items

OnOptionsItemSelected on Android not working in activity


Here is the problem, I'm using CollapsingToolbarBehavior which I get from this link https://github.com/saulmm/CoordinatorBehaviorExample. But when I click on an icon in Toolbar when collapse, it's like a dead link. Not even calling OnOptionsItemSelected, is it because stack with other layout or what?

Here is the code in main activity :

    mToolbar = (Toolbar) findViewById(R.id.main_toolbar);
    mToolbar.inflateMenu(R.menu.edit);
    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.edit, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            Log.d("in","hahhaah");
            if(id == R.id.btn_back_edit){
                onBackPressed();
                return true;
            }

            if (id == R.id.btn_edit) {
                invalidateOptionsMenu();
                Intent intent = new Intent(SettingProfileActivity.this, SettingProfileActivityOn.class);
                startActivity(intent);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        @Override
        public void onBackPressed() {
            super.onBackPressed();
            Intent intent = new Intent(SettingProfileActivity.this,MainActivity.class);
            intent.putExtra("LoadFragment", "settings");
            startActivityForResult(intent,0);
            finish();
        }

Here is the layout code :

<?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"
    tools:ignore="RtlHardcoded">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <ImageView
                android:id="@+id/main.imageview.placeholder"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                android:background="@color/colorPrimary"
                android:tint="#11000000"
                app:layout_collapseMode="parallax" />

            <FrameLayout
                android:id="@+id/main.framelayout.title"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_gravity="bottom|center_horizontal"
                android:background="@color/colorPrimary"
                android:orientation="vertical"
                app:layout_collapseMode="parallax">

                <LinearLayout
                    android:id="@+id/main.linearlayout.title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/sebelum.anim"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:gravity="bottom|center"
                        android:fontFamily="@font/raleway_medium"
                        android:layout_marginTop="30dp"
                        android:text="Robert"
                        android:textColor="@android:color/white"
                        android:textSize="24sp" />

                    <TextView
                        android:id="@+id/jenis_usaha"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/raleway_medium"
                        android:layout_gravity="center_horizontal"
                        android:gravity="bottom|center"
                        android:layout_marginTop="6dp"
                        android:layout_marginBottom="5dp"
                        android:text="Robert"
                        android:textColor="@android:color/white"
                        android:textSize="20sp" />

                </LinearLayout>
            </FrameLayout>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.constraint.ConstraintLayout 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:id="@+id/cl"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="0dp">

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

            </include>

            <View
                android:id="@+id/view2"
                android:layout_width="0dp"
                android:layout_height="52dp"
                android:background="@color/placeholder_bg"
                app:layout_constraintBottom_toTopOf="@+id/text_nama_profile"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0"
                app:layout_constraintVertical_chainStyle="packed" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="16dp"
                android:layout_marginEnd="24dp"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginStart="24dp"
                android:layout_marginTop="16dp"
                android:fontFamily="@font/raleway_semibold"
                android:text="PERSONAL DETAILS"
                android:textAlignment="center"
                android:textAppearance="@android:style/TextAppearance.Medium"
                android:textColor="#6b6c6d"
                app:layout_constraintBottom_toBottomOf="@+id/view2"
                app:layout_constraintEnd_toEndOf="@+id/view2"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="@+id/view2"
                app:layout_constraintTop_toTopOf="@+id/view2"
                app:layout_constraintVertical_bias="0.0" />

            <TextView
                android:id="@+id/text_nama_profile"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:fontFamily="@font/raleway_medium"
                android:text="Robert"
                android:textAppearance="@android:style/TextAppearance.Medium"
                app:layout_constraintBottom_toTopOf="@+id/text_jk_profile"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view2" />
<View
                android:id="@+id/batas6"
                android:layout_width="0dp"
                android:layout_height="1.75dp"
                android:layout_marginTop="9dp"
                android:background="@color/placeholder_bg"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/text_alamat_profile"
                app:layout_constraintVertical_bias="0.0" />

        </android.support.constraint.ConstraintLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.v7.widget.Toolbar
        android:id="@+id/main.toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:layout_anchor="@id/main.framelayout.title"
        app:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:title="">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Space
                android:layout_width="@dimen/image_final_width"
                android:layout_height="@dimen/image_final_width" />

            <TextView
                android:id="@+id/main.textview.title"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="8dp"
                android:gravity="center_vertical"
                android:text="Robert"
                android:textColor="@android:color/white"
                android:textSize="20sp" />

        </LinearLayout>
    </android.support.v7.widget.Toolbar>

    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="@dimen/image_width"
        android:layout_height="@dimen/image_width"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/person"
        app:finalHeight="@dimen/image_final_width"
        app:finalYPosition="2dp"
        app:civ_border_color="@android:color/white"
        app:civ_border_width="2dp"
        app:layout_behavior=".AvatarImageBehavior"
        app:startHeight="2dp"
        app:startToolbarPosition="2dp"
        app:startXPosition="2dp"
        />

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

And it's the code in menu file :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/btn_back_edit"
        android:title=""
        app:showAsAction="always"
        android:icon="@drawable/ic_back"
        />
    <item
        android:id="@+id/btn_edit"
        android:title=""
        app:showAsAction="always"
        android:icon="@drawable/ic_edit"
        />
</menu>

Here is the custom_toolbar layout :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/toolbar">

</android.support.v7.widget.Toolbar>

Solution

  • The problem here is related to the fact that you're using the Toolbar's inflateMenu method without using AppCompactActivity's setSupportActionBar.

    There are different ways of approaching this, one might be to use Toolbar's setOnMenuItemClickListener.