I have a layout for an on which I have added a toolbar using android.support.v7.widget.Toolbar
. Following 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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.riyaz.activities.lessons.EvaluationActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_gravity="top"
android:layout_height="@dimen/session_rec_page_title_bar_height"
android:orientation="vertical"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/RiyazThemeModernLight.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:gravity="end"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/RiyazThemeModernLight.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
...........
For enabling the back button I am using the following code inside onCreate()
method of my activity (extending android.support.v7.app.AppCompatActivity
):
// Setting up the toolbar ..
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
// setting the action on clicking on the back button ..
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
The problem I am facing is that this works perfectly fine for android version 5.1.1 (Nexus 5 device) but I cannot click on the back buttons (and the menu options as well) for a device which runs android 4.2.2 (Nexus 4 device). What amazes me is that for some activities where I have used the android.support.v7.widget.Toolbar
earlier works perfectly on both the devices using the code mentioned above. Anyone has any idea where I might be going wrong?
PS: The layout for which the back button is not working for android version 4.2.2 has a few custom views. Can that be an issue?
If anyone is still having problem with this problem, I found a solution in my case. I guess the error was from my side. I had a FrameLayout
overlaying my toolbar. Just go to the view option in the Android Studio and just see if your layout are not overlaying.