Skip to content

Instantly share code, notes, and snippets.

@yunusemredilber
Last active February 8, 2020 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yunusemredilber/558b7080e6235d9df33856465c920aad to your computer and use it in GitHub Desktop.
Save yunusemredilber/558b7080e6235d9df33856465c920aad to your computer and use it in GitHub Desktop.
Webview In Navigation View (Drawer) [ Android ]
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
public class SomeActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_in_drawer_layout);
// ...
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawerLayout, null, R.string.app_name, R.string.app_name);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
// To make webview full screen
View view = findViewById(R.id.some_full_screen_view);
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) navigationView.getHeaderView(0).findViewById(R.id.webview).getLayoutParams();
params.height = view.getHeight()-50;
navigationView.getHeaderView(0).findViewById(R.id.webview).setLayoutParams(params);
}
});
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Your content -->
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:backgroundTint="#fff"
android:layout_gravity="start|left"
android:nestedScrollingEnabled="true"
app:headerLayout="@layout/navigationview_header"/>
</androidx.drawerlayout.widget.DrawerLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment