Android Back Arrow on Toolbar (AppCompatActivity)
Asked Answered
A

5

20

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);
                }
            }
        });
    }
}
Apoloniaapolune answered 7/9, 2015 at 7:43 Comment(4)
place this code toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); before this code setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true);Cathepsin
not work... same crashApoloniaapolune
@SimoneVarletta 'some crash' is not a helpful description.Brightness
@KaranMer Sorry man!! perfectly WORK!!Apoloniaapolune
P
33

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

Prom answered 21/8, 2016 at 11:54 Comment(1)
this is correct , you don't need to add your own Actionbar to the XML file, instead, just add this simple code android:parentActivityName=".TheParentActivityNameHere". to the Manifest and use any theme in the styles that have an Action bar. and for the Activity (to add your own menu) just override the onCreateOptionsMenu(Menu menu) and onOptionsItemSelected(MenuItem item)Inweave
S
28

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
            }
        });
Stithy answered 12/12, 2015 at 5:38 Comment(1)
Complete answer is here but yours having precise code for ToolbarSetaceous
L
8

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.

Lifelike answered 10/7, 2017 at 8:31 Comment(0)
G
3

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);
Gaily answered 7/9, 2015 at 7:51 Comment(0)
C
3

You need to set support actionbar first before your can get it. so place the following code

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

Before this Code

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Cathepsin answered 7/9, 2015 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.