I am new on Android.
Through this simple program I want to to open a drawer using button onclick().But This gives null exception error.I tried the solution by googling but unfortunately didn't solve it.
This is onCreate() method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navListener();
}
navListener()
public void navListener(){
final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
ImageView nav = (ImageView) findViewById(R.id.navbarimagebutton);
nav.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout.openDrawer(GravityCompat.START);
}
});
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<include layout="#layout/app_bar" />
</RelativeLayout>
DrawerLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerlayout"
android:layout_height="match_parent"
android:layout_width="120dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"></ListView>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
app_bar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/red">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/navbarimagebutton"
android:layout_width="0dp"
android:layout_weight=".25"
android:layout_height="fill_parent"
android:src="#drawable/menu"
/>
<View
android:id="#+id/homepagefirstview"
android:layout_width="10dp"
android:layout_height="fill_parent"></View>
<ImageView
android:layout_weight="1"
android:layout_width="0dp"
android:src="#drawable/hometitle"
android:layout_height="fill_parent" />
<View
android:layout_width="10dp"
android:layout_height="fill_parent"/>
<ImageView
android:layout_weight=".25"
android:layout_width="0dp"
android:src="#drawable/search"
android:layout_height="fill_parent" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
Your problem is your Asking your activity to setContentView(R.layout.activity_main);
And this file does not include your DrawerLayout
you need to do:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<!-- add an id here since your using RelativeLayout -->
<include layout="#layout/app_bar" android:id="#+id/app_bar" />
<!-- include the drawer this way, probably need to add layout_below="#+id/app_bar" or whatever the id is for your app bar -->
<!-- Uncomment this <include layout="#layout/drawer_layout"/> -->
<!-- Or simply copy the drawer layout xml and add layout_below="#+id/app_bar" -->
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerlayout"
android:layout_below="#+id/app_bar"
android:layout_height="match_parent"
android:layout_width="match_parent">
<!-- main content area -->
<FrameLayout
android:id="#+id/content_area"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<!-- Slide out drawer section -->
<FrameLayout
android:layout_gravity="start"
android:layout_width="320dp"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
You are running into NullPointerException because your inflated view does not contain this DrawerLayout you are referencing
Related
[Updated] I've used Bottombar library and default android Navigation Drawer in my project. Now simple TextView Fragment is overlapping top Toolbar. This is my activity layout code.
You can check Screenshot of Layout here.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/toolbar" />
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:layout_width="#dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
layout="#layout/list_view" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
<!-- Start - Container to show Fragments -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomBar" />
<!-- End - Container to show Fragments -->
<com.roughike.bottombar.BottomBar
android:id="#+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:background="#color/main_color_500"
app:bb_activeTabAlpha="1"
app:bb_inActiveTabAlpha="0.3"
app:bb_activeTabColor="#android:color/white"
app:bb_inActiveTabColor="#android:color/white"
app:bb_titleTextAppearance="#style/TextAppearance.AppCompat.Caption"
app:bb_showShadow="true"
app:bb_tabXmlResource="#xml/menu_bottombar" />
</RelativeLayout>
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<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="?android:attr/actionBarSize"
android:background="#color/main_color_500"
app:popupTheme="#style/Theme.AppCompat.Light.DarkActionBar"
app:theme="#style/AppTheme.Title" />
list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_view"
style="#style/ListViewStyle" />
OnCreate method(Navigation Drawer Code) of MainActivity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//getSupportActionBar().setTitle("Sample Title");
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mTitle = mDrawerTitle = getTitle();
mDrawerList = (ListView) findViewById(R.id.list_view);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
View headerView = getLayoutInflater().inflate(
R.layout.header_navigation_drawer_social, mDrawerList, false);
ImageView iv = (ImageView) headerView.findViewById(R.id.image);
// init universal image loader library
ImageUtil.initImageUtil(this);
ImageUtil.displayRoundImage(iv, "http://www.sample.com/0.jpg", null);
mDrawerList.addHeaderView(headerView);// Add header before adapter (for pre-KitKat)
mDrawerList.setAdapter(new DrawerSocialAdapter(this));
mDrawerList.setOnItemClickListener(new MainActivity.DrawerItemClickListener());
int color = ContextCompat.getColor(getApplicationContext(),R.color.material_grey_100);
color = Color.argb(0xCD, Color.red(color), Color.green(color),
Color.blue(color));
mDrawerList.setBackgroundColor(color);
mDrawerList.getLayoutParams().width = (int) getResources()
.getDimension(R.dimen.drawer_width_social);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.addDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// open drawer oncreate
//mDrawerLayout.openDrawer(mDrawerList);
}
How to fix it?
Thanks in advance.
Even though the question has already been answered the another cheeky way to do is add this in your frame layout
android:layout_marginTop="?attr/actionBarSize
Give you LinearLayout a id of something like android:id="#+id/ToolBar" and in your FrameLayout set below like android:layout_below="#+id/ToolBar"
Try this, I just tested and it works:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/bottomBar">
<include layout="#layout/toolbar"
android:id="#+id/toolbar"/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:layout_width="#dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
layout="#layout/list_view" />
</android.support.v4.widget.DrawerLayout>
<!-- Start - Container to show Fragments -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
/>
<!-- End - Container to show Fragments -->
</RelativeLayout>
<com.roughike.bottombar.BottomBar
android:id="#+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:background="#color/main_color_500"
app:bb_activeTabAlpha="1"
app:bb_inActiveTabAlpha="0.3"
app:bb_activeTabColor="#android:color/white"
app:bb_inActiveTabColor="#android:color/white"
app:bb_titleTextAppearance="#style/TextAppearance.AppCompat.Caption"
app:bb_showShadow="true"
app:bb_tabXmlResource="#xml/menu_bottombar" />
</RelativeLayout>
Please see if the following fixes the problem:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"/>
<include
android:layout_width="#dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
layout="#layout/list_view" />
</android.support.v4.widget.DrawerLayout>
<RelativeLayout
android:id="#+id/nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<include layout="#layout/toolbar"
android:id="#+id/toolbar"/>
<!-- Start - Container to show Fragments -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
/>
<!-- End - Container to show Fragments -->
</RelativeLayout>
<com.roughike.bottombar.BottomBar
android:id="#+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:background="#color/main_color_500"
app:bb_activeTabAlpha="1"
app:bb_inActiveTabAlpha="0.3"
app:bb_activeTabColor="#android:color/white"
app:bb_inActiveTabColor="#android:color/white"
app:bb_titleTextAppearance="#style/TextAppearance.AppCompat.Caption"
app:bb_showShadow="true"
app:bb_tabXmlResource="#xml/menu_bottombar" />
</RelativeLayout>
If this doesn't fix it I will have to give you a totally different approach.. Let me know.
You need to use android:layout_below on FrameLayout to get it under tool bar.
<include
android:id="#+id/tool_bar"
layout="#layout/toolbar" />
and change FrameLayout code to.
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomBar"
android:layout_below="#+id/tool_bar"
/>
This approach might help (I faced a similar problem and this technique worked, but I wanted to add few views and not a complete fragment.)
Define a new xml layout and set whatever content you want to in that. (If you used the standard Navigation Drawer activity then an xml file by the name of content_test.xml or something similar might have been created automatically for you).
In your toolbar.xml add
<include layout="content_test.xml">
For example
content_test.xml
<android.support.constraint.ConstraintLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.yourdomain.yourappname.YourActivity"
tools:showIn="#layout/toolbar.xml">
<!--Add your views/layouts here. Don't forget to set
android:layout_below="#id/idOfToolbarDefinedInYourActivity-->
</android.support.constraint.ConstraintLayout>
Continuing with the example
toolbar.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"
tools:context="com.yourdomain.yourappname.YourActivity">
<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_test" />
</android.support.design.widget.CoordinatorLayout>
I'm trying to have a listView in my navigation drawer header. I have the list there and it populates fine, the only issue is that I can't scroll the list. It seems the nav drawer itself is intercepting the scroll action and not allowing me to scroll the ListView
nav_header_drawer.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:layout_width="match_parent"
android:layout_height="260dp"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical">
<ListView
android:id="#+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
and the drawer
<?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_drawer"
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_drawer"
app:menu="#menu/activity_drawer_drawer" />
If you need any more files, I can post
You can customize navigation drawer as you want. design a separate layout say nav_layout.xml and in your activity_home.xml replace your navigation view with below code.
<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">
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/nav_layout" />
</android.support.design.widget.NavigationView>
Now open your nav_layout.xml and put below code.
<?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:layout_width="match_parent"
android:layout_height="260dp"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical">
<ListView
android:id="#+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
in this way you can customize navigation drawer as you want.
For the listview xml try adding one or both:
android:focusable="true"
android:focusableInTouchMode="true"
I had to do this for buttons created in a listview which were not responding to touch events.
I have an activity where I show a map. I want to can change when click an item on my navigation drawer with a different fragment.
I have next main xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.main.activities.Activity_main">
<include layout="#layout/my_bar" />
<android.support.design.widget.NavigationView
android:layout_marginTop ="45dp"
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/navigation_menu"
app:itemIconTint="#000000"
android:background="#FFFFFF"
app:itemTextColor="#000000"
app:headerLayout="#layout/navigation_header">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Bar layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/my_bar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar -->
<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:minHeight="?attr/actionBarSize"
android:background="#color/primary_color"
android:titleTextAppearance="#color/text_and_icon_color"
app:popupTheme="#style/Theme.AppCompat"
android:elevation="1dp"
android:textAlignment="center"
android:layout_height="45dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My app"
android:layout_gravity="center"
android:id="#+id/toolbar_title"
android:layout_weight="1"
android:textSize="24sp"
android:textColor="#android:color/white" />
</android.support.v7.widget.Toolbar>
<include layout="#layout/my_content" />
</LinearLayout>
My content:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/my_content"
android:background="#color/window_background_color">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="#+id/adView"/>
</RelativeLayout>
I try to replace the map with a fragment :
<FrameLayout 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"
tools:context="com.main.activities.catalog">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
I call with this code:
catalog fragment2 = new catalog();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.my_content, fragment2, "fragment2");
fragmentTransaction.commit();
But my map isn't replaced with new fragment. Where I do wrong?
As #MikeM says in a comment to your question, the problem is that my_content.xml contains a static fragment specified directly in the XML. FragmentManager will not replace such a fragment dynamically at run time. Instead, you should remove the fragment tag and add the map fragment dynamically in onCreate(). Typically this is done with a FrameLayout but I think it will also work with a RelativeLayout.
For more information, see the Fragment Developer Guide.
I have a relative layout in which i have two child .one is drawer layout and the other is a frame layout in which i have used a fragment .The problem is when i keep drawer layout in the top and the frame layout in the bottom,drawer layout doesn't work and if do the opposite,frame layout doesn't
this is my relative layout
<RelativeLayout 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">
<include
android:id="#+id/toolbar_actionbar"
layout="#layout/mtoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar_actionbar">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.rohit.newmastervocab.FloatingFragment"
android:id="#+id/fragment2"
android:layout_alignParentStart="true"
tools:layout="#layout/fragmentfloating"
android:layout_alignTop="#+id/drawer"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
<android.support.v4.widget.DrawerLayout android:id="#+id/drawer1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_below="#+id/toolbar_actionbar"
android:layout_alignParentBottom="true">
<ListView
android:id="#+id/drawerlist1"
android:layout_width="240dp"
android:layout_height="match_parent"
android:entries="#array/navigation_items"
android:background="#F3F6C8"
android:layout_gravity="start">
</ListView>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Mike M. is absolutely right: you need to keep FrameLayout as one of the Children of DrawerLayout.
I've updated your XML a bit, take a look:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<FrameLayout
android:layout_below="#+id/toolbar_actionbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_gravity="center_horizontal|bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.rohit.newmastervocab.FloatingFragment"
android:id="#+id/fragment2"/>
</FrameLayout>
</RelativeLayout>
<ListView
android:id="#+id/drawerlist1"
android:layout_width="240dp"
android:layout_height="match_parent"
android:entries="#array/navigation_items"
android:background="#F3F6C8"
android:fitsSystemWindows="true"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
Now drawer works just as expected and Fragment is stick to the bottom|center of the screen.
I created a new Project in Android Studio - a Blank Activity with Fragment. It created the following files
MainActivty.java MainActivityFragment.java activity_main.xml
fragment_main.xxml
Now I wanted to add a Toolbar so I created a app_bar.xml as follows
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
I modified the MainActivity.java as below
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
this.setSupportActionBar(toolbar);
}
activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment"
android:name="com.lalapetstudios.udacityprojects.spotifystreamer.MainActivityFragment"
tools:layout="#layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment.xml
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivityFragment">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
When I run this. I get the following exception
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'java.lang.CharSequence
android.support.v7.widget.Toolbar.getTitle()' on a null object
reference
So I modified my activity_main.xml as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<fragment
android:id="#+id/fragment"
android:name="com.lalapetstudios.udacityprojects.spotifystreamer.MainActivityFragment"
tools:layout="#layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Also modified the app_bar.xml as follows (removed the android:id parameter)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
Now the App runs. Im able to see the Toolbar. However the Fragment is hidden behind the tool bar. The HelloWorld is behind the Toolbar. How do I fix it. Thank you.
Change the orientation to vertical in the activity_main.xml file. See below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<fragment
android:id="#+id/fragment"
android:name="com.lalapetstudios.udacityprojects.spotifystreamer.MainActivityFragment"
tools:layout="#layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>