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);
}
}
});
}
}
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.
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.
You can also just declare in the Manifest
a ParentActivity
with:
<activity
android:name=".ThisActivity"
android:parentActivityName=".TheActivityThatCalledThisOne">
</activity>
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
}
});
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)
}
@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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With