Solution 1 :
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
You can try this
Solution 2 :
EDIT
you need to set actionbar first
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
if you get
java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
this error you need to change your change that ativity theme to NoActionBar
in Manifest
Did you try this option?
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Solution 3 :
Change activity in manifest file to have the back button for the parent
<activity
android_name="com.example.mymallapp.DeliveryActivity"
android_parentActivityName="com.example.mymallapp.MyParentActivity">
<!-- Parent activity meta-data to support 4.0 (API 15) and lower -->
<meta-data
android_name="android.support.PARENT_ACTIVITY"
android_value="com.example.mymallapp.MyParentActivity" />
</activity>
Change MyParentActivity
to the activity that you want to return back when you hit the home button.
Solution 4 :
Thanks to all.. The problem is solved.. While the back button was not visible on the design tab, it is very much there when I ran the app on the device. Rather surprising!
Problem :
I am not able to get the back button displayed on the toolbar despite trying every trick including invalidating caches etc. As I seem to be wasting my time on something I seem to have overlooked, request help to solve this problem. Thanks in advance. My code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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_layout_width="match_parent"
android_layout_height="match_parent"
tools_context=".DeliveryActivity">
<com.google.android.material.appbar.AppBarLayout
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_theme="@style/AppTheme.AppBarOverlay"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android_id="@+id/toolbar"
android_layout_width="match_parent"
android_layout_height="?attr/actionBarSize"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintTop_toTopOf="parent"
app_popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
My Activity
package com.example.mymallapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
import android.view.MenuItem;
import java.util.Objects;
public class DeliveryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delivery);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle("Delivery");
}
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home){
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Comments
Comment posted by Venugopal
Hi Shiva, Zain and Jyotish..I tried each of the suggestions but the back arrow simply refuses to appear on the design tab.. Any suggestions?
Comment posted by Jyotish Biswas
hey Venugopal you can check now, edited the answer just now
Comment posted by Venugopal
Hi Jyotish..thanks…I had already set the actionbar as shown in my code snippet. Also the theme already has noactionbar. So, I think the bug is something else!