Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Back Arrow on Toolbar (AppCompatActivity)

I want to show the back arrow button in my app, but the app crashes when I put this in my code:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

style.xml

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/PrimaryColor</item>
    <item name="colorPrimaryDark">@color/PrimaryColorDark</item>
    <item name="colorAccent">@color/PrimaryColor</item>
    <item name="colorControlHighlight">@color/ControlHighlight</item>
    <item name="android:statusBarColor" tools:targetApi="21">@android:color/transparent</item>
</style>


<style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Light" >
    <item name="android:textColor">#000000</item>
</style>


<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorColor">#FFFFFF</item>
    <item name="tabIndicatorHeight">3dp</item>
    <item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
</style>


<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
    <item name="android:textColor">#FFFFFF</item>
</style>

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:layout_height="@dimen/abc_action_bar_default_height_material"
android:elevation="0dp"
android:layout_width="match_parent"
android:id="@+id/toolbar"
android:background="@color/PrimaryColor"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ActionBarPopupThemeOverlay"/>

Class (not Fragment):

public class WebViewCanale extends AppCompatActivity {

    Toolbar toolbar;
    TextView textView;
    WebView webView;
    ProgressBar progressBar;

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

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

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


        //FindViewById
        webView = (WebView) findViewById(R.id.webview);
        textView = (TextView) findViewById(R.id.textView);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        //Impostazioni WebView
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setLoadsImagesAutomatically(true);
        webView.loadUrl(this.getIntent().getDataString());
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setAppCacheEnabled(false);

        //Titolo Toolbar
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (!TextUtils.isEmpty(title)) {
                    WebViewCanale.this.setTitle(title);
                }
            }
        });
    }
}
like image 797
Simone Varletta Avatar asked Sep 07 '15 07:09

Simone Varletta


People also ask

How do I get the back arrow on my android toolbar?

Show back button using actionBar. setDisplayHomeAsUpEnabled(true) this will enable the back button. Custom the back event at onOptionsItemSelected. This will enable the back function to the button on the press.

What is android Action Bar?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.


4 Answers

You can also just declare in the Manifest a ParentActivity with: <activity android:name=".ThisActivity" android:parentActivityName=".TheActivityThatCalledThisOne"> </activity>

like image 65
ra99nano Avatar answered Oct 23 '22 08:10

ra99nano


I wonder why there's not a complete answer for this on SO, but finally I solved the matter on my own:

Use the following snippet to have back button on the toolbar in an AppCompatActivity:

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

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //do something you want
            }
        });
like image 27
Shayan Amani Avatar answered Oct 23 '22 08:10

Shayan Amani


Update

      private lateinit var toolbar: Toolbar
    
      override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = DataBindingUtil.setContentView<DetailActivityBinding>(
          activity,
          R.layout.detail_activity
        )
        toolbar = binding.toolbar
        activity.setSupportActionBar(toolbar)
        activity.supportActionBar!!.setDisplayShowHomeEnabled(true)
        activity.supportActionBar!!.setDisplayHomeAsUpEnabled(true)
      }

Original

 @BindView(R.id.toolbar)
 Toolbar toolbar;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_events);
    ButterKnife.bind(this);

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 }

 @Override
 public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
 }

onSupportNavigateUp() This method is called whenever the user chooses to navigate Up within your application's activity hierarchy from the action bar.

onBackPressed(); Take care of popping the fragment back stack or finishing the activity as appropriate.

like image 8
Akshay Nandwana Avatar answered Oct 23 '22 08:10

Akshay Nandwana


add this theme:

<style name="AppTheme2" parent="Theme.AppCompat.Light">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/appColor</item>
    <item name="colorControlNormal">@color/whiteColor</item>
    <item name="colorControlActivated">@color/appColor</item>
</style>

and in you activity:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
like image 3
Lalit Pratap Singh Avatar answered Oct 23 '22 09:10

Lalit Pratap Singh