I made a custom Navigation drawer in the active with dynamic ListView instead of the menu. The Navigation drawer itself works fine, but all the View (buttons \ checkboxes, etc.) do not work on the OnClickListener. before that everything worked fine and in other activations everything works fine.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_height="match_parent"><android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginTop="16dp"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_fz_main" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="54dp"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/nav_header_fz_main" />
<ImageView
android:id="#+id/nwBackButton"
android:layout_width="50dp"
android:layout_height="34dp"
android:layout_margin="8dp"
android:visibility="gone"
app:srcCompat="#drawable/ic_backspace" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout></android.support.design.widget.CoordinatorLayout>
In the active via Butter Knife initialization and with the Navigation drawer does not work anymore
in onCreate:
setSupportActionBar(toolbar);
toolbar.setTitle(getResources().getString(R.string.app_name));
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
sorry for my english.
someone can help me?
I found the problem
just add to content layout
content.bringToFront();
Related
I'm trying to add a drawer menu in two activities, one only has the drawer menu and the other one has the drawer menu and a ConstraintLayout beneath, the one without the ConstraintLayout works good, but the drawer menu of the other one doesn't respond, it doesn't scroll and if I try to select an option it just closes the drawer and does nothing.
Both XML are exactly the same except for the extra ConstraintLayout, if I remove the ConstraintLayout it works well but I need it for some controls.
this is the XML of the activity that doesn't work:
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drwlayout"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
tools:context=".ac_Menu">
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/linearLayoutCompat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="395dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/mitoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/Theme.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
</androidx.appcompat.widget.LinearLayoutCompat>
<com.google.android.material.navigation.NavigationView
android:id="#+id/navview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/drwamenu"
app:headerLayout="#layout/nav_header"
></com.google.android.material.navigation.NavigationView>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">
<ListView
.... />
<TextView
...... />
<TextView
...... />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
I have made a toolbar in main activity where i have set the scroll flags to it but the problrm is that in a fragment hide and show is not working except one fragment it is working in all other fragments and moreover my fab is also not visible but the fab is showing in the preview i do not know where i'm going wrong so please if some one can help me out here
MainActivity.java
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
main activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary">
<!-- AppBarLayout is a wrapper for a Toolbar in order to apply scrolling effects. -->
<!-- Note that AppBarLayout expects to be the first child nested within a CoordinatorLayout -->
<android.support.design.widget.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<!-- Toolbar is the actual app bar with text and the action items -->
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextColor="#color/White"
app:layout_scrollFlags="scroll|enterAlways|snap">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_navigation"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:animateLayoutChanges="true">
</FrameLayout>
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom" />
</android.support.design.widget.CoordinatorLayout>
and the fragment where it is not working
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.RecyclerView
android:id="#+id/feed_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="normal"
android:layout_marginBottom="5dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:src="#drawable/ic_search_black_24dp" />
</RelativeLayout>
I finished my app with all my xml layouts in the "layout" folder, which is used by normal-sized screens. So now I am starting with small screens and create a folder called "layout-small" right under the "layout" folder in "res".
I did some modifications in the smaller layout with smaller buttons and margins and then tried to running it on a normal screen and a small screen to see if each screen would use the layout it should, but both use the normal layout. The small screen doesn't use the small layout.
I believe this is due to the fact that the Java class that uses the setContent() method uses a layout which in turn includes another layout which also includes a third layout.
Here is my code for more clarity. The 3 activity_main.xml app_bar_main.xml and content_main.xml layout files are in res/layout and res/layout-small as well, with differences in button sizes only in content_main.xml.
Home.java :
public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
/**fields*/
private Button butVentes, butLocations, butRecherche, butFavoris, butContact, butSocial; //Buttons for home screen
private Toolbar toolbar;
private DrawerLayout drawer;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBA_8888);
setContentView(R.layout.activity_main);
/**toolbar*/
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/**drawer*/
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
/**navigationView*/
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
/*...*/
}
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
app_bar_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivities.Home">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
content_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="#+id/home_background_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/app_accueil_background"
android:orientation="horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivities.Home"
tools:showIn="#layout/app_bar_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:layout_marginBottom="40dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="240dp"
android:orientation="horizontal">
<Button
android:id="#+id/buttonVentes"
android:layout_width="60dp"
android:layout_height="48dp"
android:layout_marginRight="5dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/ic_view_list"
android:paddingTop="10dp"
android:text="#string/content_main_vente"
android:textColor="#color/colorBlackText"
android:textSize="13sp" />
<Button
android:id="#+id/buttonLocations"
android:layout_width="60dp"
android:layout_height="48dp"
android:layout_marginRight="5dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/ic_view_list"
android:paddingTop="10dp"
android:text="#string/content_main_location"
android:textColor="#color/colorBlackText"
android:textSize="13sp" />
<Button
android:id="#+id/buttonRecherche"
android:layout_width="60dp"
android:layout_height="48dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/ic_search"
android:paddingTop="10dp"
android:text="#string/content_main_recherche"
android:textColor="#color/colorBlackText"
android:textSize="13sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:orientation="horizontal">
<Button
android:id="#+id/buttonFavoris"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_marginRight="14dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/ic_star_accueil"
android:paddingTop="10dp"
android:text="#string/content_main_favoris"
android:textColor="#color/colorBlackText"
android:textSize="13sp" />
<Button
android:id="#+id/buttonContact"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_marginRight="14dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/ic_contacts"
android:paddingTop="10dp"
android:text="#string/content_main_contact"
android:textColor="#color/colorBlackText"
android:textSize="13sp" />
<Button
android:id="#+id/buttonSocial"
android:layout_width="100dp"
android:layout_height="80dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/ic_share"
android:paddingTop="10dp"
android:text="#string/content_main_social"
android:textColor="#color/colorBlackText"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>
instead of creating other layout folders, just maintain different dimension folders like :
values-sw320dp - 1dp
values-sw360dp - 1.12dp
values-sw410dp - 1.28dp
values-sw480dp - 1.5dp
I am currently redesigning my app to include a navigation drawer for navigating through the app. Currently, each screen is an activity and the way I have designed the home screen, is that it contains a toolbar and another xml file.
<?xml version="1.0" encoding="utf-8"?>
<Toolbar
android:layout_width="match_parent"
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay">
<ImageView
android:layout_width="#dimen/_30sdp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/_95sdp"
android:src="#drawable/logo"
android:id="#+id/logo"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sync_green"
android:id="#+id/sync_button"
android:layout_marginStart="#dimen/_75sdp"/>
</Toolbar>
<include layout="#layout/activity_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/run"
/>
Now, I have made another XML file for the navigation drawer:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/activity_main_drawer" />
My dilemna is that since my toolbar is in the home.xml fragment, when I switch fragments, I will lose the toolbar. I want to keep the toolbar for all the views in the navigation drawer.
Also, in my java code, the setActionBar(toolbar) is throwing up an error saying cannot resolve method setActionBar.
One simple and straightforwrd solution for integrating NavigationDrawer along with Fragment in an Activity, is to design the activity_main.xml the following way :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layoutDirection="rtl"
tools:openDrawer="start">
<include
layout="#layout/content_act_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="#color/flat_white_light">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Now, in the content_act_main.xml you can have :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ToolbarColoredBackArrow"
app:popupTheme="#style/AppTheme.PopupOverlay" >
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
If you notice, in the conten_act_main.xml, there is a toolbar that will be available to all the content that are shown in the FrameLayout, and you can use this FrameLayout as the container of your Fragments.
You can of course maintain this toolbar, you need ActionBarDrawerToggle to bind the drawerLayout and Toolbar together.
Minimal configuration would be more or less :
ActionBarDrawerToggle actionBarDrawerToggle;
DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
}
And you put the Toolbar in your base activity layout and other views wrapped in DrawerLayout.
<Android.support.v4.widget.DrawerLayout
xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:id="#+id/navigation_drawer"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:fitsSystemWindows="true">
<Android.support.v7.widget.Toolbar
xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
Android:id="#+id/toolbar"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
></Android.support.v7.widget.Toolbar>
....
....
</Android.support.v4.widget.DrawerLayout>
Try This in your activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
This is my .xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:paddingBottom="#dimen/design_navigation_separator_vertical_padding"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/apptoolbar" />
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/nav_toolbar"
android:clickable="true">
</FrameLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/appColor"
android:fitsSystemWindows="true"
android:paddingBottom="#dimen/design_navigation_separator_vertical_padding"
app:itemTextAppearance="#style/NavigationDrawerStyle"
app:headerLayout="#layout/nav_header_home"
app:itemBackground="#color/appColor"
app:itemTextColor="#color/drawable_selector_drawer_item"
app:menu="#menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>
everybody, I am new in Android development and I am trying to create an app with custom navigation drawer. I have a code:
<android.support.v4.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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/fragment_home" />
</LinearLayout>
<ListView
android:id="#+id/navigation_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
And this code creates a navigation drawer perfectly fine. Although, when I provide a layout like this, my navigation drawer seems to take place in whole screen and doesn't react to any actions:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/fragment_home" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/navigation_menu_background" >
<include layout="#layout/navigation_menu_header" />
<ListView
android:id="#+id/navigation_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
My JAVA code is:
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
#Override
public void onDrawerOpened(View drawerView) {
// TODO: Handle drawer open action here!
}
#Override
public void onDrawerClosed(View drawerView) {
// TODO: Handle drawer close action here!
}
};
drawer.setDrawerListener(toggle);
toggle.syncState();
// Sets up navigation view.
ListView navigationListView = (ListView) findViewById(R.id.navigation_list_view);
NavigationMenuListAdapter navigationMenuListAdapter = new NavigationMenuListAdapter(this);
navigationListView.setAdapter(navigationMenuListAdapter);
navigationListView.setOnItemClickListener(this);
Where could be a problem causing this issue? And why is that so?
The android:layout_gravity="start" attribute needs to be on the View that you're using for the drawer; in this case, the NavigationView.
You might also want to set the layout_width to "wrap_content", so the drawer doesn't cover more of the content than it should. NavigationView will measure itself out to an appropriate width, but if you were to use another View that doesn't, you might need to use an exact measure instead of wrapping; e.g., "240dp".
The reason it's taking up the whole screen is because you have "match_parent" specified as the height and width values- it's matching the size of the entire view, and therefore the entire screen.