Skip to content

Instantly share code, notes, and snippets.

@AkaashSaini
Last active August 29, 2022 10:50
Show Gist options
  • Save AkaashSaini/a718c4c953a6d5f3b95ddfcc20734043 to your computer and use it in GitHub Desktop.
Save AkaashSaini/a718c4c953a6d5f3b95ddfcc20734043 to your computer and use it in GitHub Desktop.
how to create bottom navigation bar with webview (next,previous,refresh) no item selected
//create new menu resource file bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Home"
android:id="@+id/home"
android:icon="@drawable/ic_baseline_home_24"
app:itemIconTint="@color/black"
app:itemTextColor="#000000"
/>
<item android:title="previous"
android:id="@+id/prev"
android:icon="@drawable/ic_baseline_arrow_back_24"
app:itemIconTint="@color/black"
app:itemTextColor="#000000"
/>
<item android:title="next"
android:id="@+id/next"
android:icon="@drawable/ic_baseline_arrow_forward_24"
app:itemIconTint="@color/black"
app:itemTextColor="#000000"
/>
<item android:title="reload"
android:id="@+id/reload"
android:icon="@drawable/ic_baseline_refresh_24"
app:itemIconTint="@color/black"
app:itemTextColor="#000000"
/>
<item android:id="@+id/none"
android:title=""
android:visible="false"/>
</menu>
//add BottomNavigationView to Activity
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="6"
app:menu="@menu/bottom_nav_menu"
/>
//inside java file
BottomNavigationView bottomNavigationView;
bottomNavigationView=findViewById(R.id.bottom_nav_menu);
//seleted invisible items
bottomNavigationView.setSelectedItemId(R.id.none);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.home:
webView.loadUrl(url);
break;
case R.id.prev:
onBackPressed();
break;
case R.id.next:
if (webView.canGoForward()){
webView.goForward();
}
else{
Toast.makeText(MainActivity.this, "sorry unable to go next", Toast.LENGTH_SHORT).show();
}
break;
case R.id.reload:
webView.reload();
break;
}
return true;
}
});
@Override
public void onBackPressed() {
if (webView.canGoBack()){
webView.goBack();
}
else{
new AlertDialog.Builder(this)
.setCancelable(false)
.setTitle("Exit App!")
.setMessage("Are you sure to exit app!")
.setNegativeButton("no", null)
.setPositiveButton("sure", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finishAffinity();
}
}).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment