I'm trying to add a header in my navigation drawer.
I'm a beginner on android studio, so any tips would help me a lot
The problem is this header is over my navigation drawer itens, so I can't see some of my navigation drawer itens.
how can I add this header properly? also, my textview content is not showing, why?
<?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"
tools:context="com.sk.mf.UserAreaActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:id="#+id/drawerLayout">
<FrameLayout
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:id="#+id/content_frame">
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#layout/navigation_menu"
android:layout_gravity="start">
<RelativeLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/colorPrimary">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/nh_User"
android:textSize="24sp"
android:textColor="#ffffff" />
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Create the Header in header tag of NavigationView:
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/white"
app:headerLayout="#layout/nav_header" //layout of Header
app:itemIconTint="#color/colorPrimary"
app:menu="#menu/menu_navigation" />
in your layout/nav_header.xml: add the header contents
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:scaleType="fitXY"
android:src="#drawable/drawer_header" />
</LinearLayout>
To access the Header layout contents use:
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
View header = navigationView.getHeaderView(0); //get the header view
TextView text = (TextView) header.findViewById(R.id.nh_User);
text.setText("Hello");
Related
I am making a Navigation Drawer in my Android app. The Navigation Drawer has an Header which includes a Account Icon and a Text View. But when I run the App, I can no longer see the Icon or the TextView.
This is what I have been getting,
Here is my code for my navigation drawer activity_admin.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.vishistvarugeese.ongc_app.AdminActivity">
<android.support.design.widget.NavigationView
app:headerLayout="#layout/admin_nav_header"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/white"
app:itemIconTint="#color/black"
app:itemTextColor="#color/colorAccent"
app:menu="#menu/admin_menu"
android:layout_gravity="start"
app:theme="#style/menu_style">
</android.support.design.widget.NavigationView>
This is the code for my navigation header, admin_nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="#drawable/headernav"
android:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/ic_account_circle_white_48dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/navUserName"
android:text="#string/name"
android:textStyle="bold"
android:textColor="#color/white"
android:layout_marginTop="10dp"/>
</LinearLayout>
This is what I was trying to do
Can someone please help me with the issue?
Remove theme and other property from NavigationView.
<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/homeDrawerColor"
android:fitsSystemWindows="true"
app:headerLayout="#layout/admin_nav_header"
app:menu="#menu/admin_menu" />
If not work then give a direct string to check any problem in string.xml or not...
I want to integrate a button over relative layout over navigation drawer. Could someone help me please?
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true">
<!-- The navigation drawer -->
<RelativeLayout
android:id="#+id/rela"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginRight="-20dp"
android:background="#android:color/black"></RelativeLayout>
<RelativeLayout
android:id="#+id/relad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#android:color/black">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/radiobutton_off_background"
app:fabSize="normal"
android:layout_marginLeft="-20dp"
/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
My floatin button appear inside. But cannot able to overlay it
I want to change NavigationView header background programmatically but got something that not I want.
this is MainActivity I attempt to change Navigation header background in this way:
View header = navigationView.getHeaderView(0);
tv_nav_header = (TextView)header.findViewById(R.id.text_nav_header);
tv_nav_header.setText(username);
tv_nav_header.setBackground(drawable);
this is my nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="160dp">
<cn.xyz.util.CircleImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="40dp"
android:layout_marginStart="10dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/text_nav_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_marginStart="70dp"
android:text="tempLocalUser"
android:textColor="#FFFFFF"
android:background="#android:color/transparent"/>
</RelativeLayout>
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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="100" />
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_drawer" />
</android.support.v4.widget.DrawerLayout>
the actual show like this,this isn't my want
enter image description here
and below is my want,this is imple by nav_header.xml RelativeLayout attr
android:background="#color/colorPrimary_blue"
enter image description here
You are setting the background color to the TextView. Instead try setting the color on the RelativeLayout. Try the following -
View header = navigationView.getHeaderView(0);
header.setBackground(drawable);
tv_nav_header = (TextView)header.findViewById(R.id.text_nav_header);
tv_nav_header.setText(username);
tv_nav_header.setBackground(drawable);
I have simple question! I am stuck on it. I have seen plenty of answers and tried everything that I can.
Here is my DrawerLayout:
<?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:context=".BaseNavDrawerActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<include
android:id="#+id/toolbar"
layout="#layout/layout_toolbar" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_drawer_header_layout"
app:itemBackground="#color/white"
app:itemTextColor="#color/nav_text_color_selector"
app:itemIconTint="#color/nav_text_color_selector"
app:menu="#menu/nav_drawer" />
</android.support.v4.widget.DrawerLayout>
And this is my nav_drawer_header_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="bottom"
android:background="#drawable/drawer_cover">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/google_logo"
android:id="#+id/image_account"
android:scaleType="centerCrop"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="17dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner_accounts_list"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:spinnerMode="dropdown"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp"
android:layout_marginEnd="22dp"
android:layout_marginRight="22dp" />
</RelativeLayout>
When I scroll NavigationDrawer, only NavigationView get scrolled but headerlayout(nav_drawer_header_layout) remains still and doesn't scroll at all.
I know DrawerLayout scrolls completely but I am stuck here.
What I am doing wrong?
EDIT-1
Java code snippet:
mNavViewHeaderLayout = LayoutInflater.from(EmailActivity.this).inflate(R.layout.nav_drawer_header_layout, mNavigationView);
mSpinnerAccounts = (Spinner)mNavViewHeaderLayout.findViewById(R.id.spinner_accounts_list);
You need to do two things.
remove app:headerLayout="#layout/nav_drawer_header_layout" from your NavigationView.
Inflate header in your navigation view at run time.
View headerLayout = navigationView.inflateHeaderView(R.layout.nav_header_home);
TextView txtContact = (TextView) headerLayout.findViewById(R.id.txtContact);
TextView txtHeaderLogoName = (TextView) headerLayout.findViewById(R.id.txtHeaderLogoName);
I can't get the Floating Action Button (FAB) to appear in the correct position. I want it to appear between the header and the first item in my nav drawer.
Currently, I've got it to appear in the bottom right corner of the header and NOT on top of the line between the 1st and 2nd elements (1st element = header & 2nd element = first item in recyclerview).
My app is using the following appcompat items:
appcompat-v7:23.0.0
recyclerview-v7:23.0.0
design:23.0.0
I'm using a nav drawer but I can't use the NavigationView because I need to customize the item entries and not load a simple menu.
As you know, the drawer is really not 2 different controls. The header is actually the '0' element in the RecyclerView. I don't know if this makes a difference.
Here is my current xml for the header/"0 view in RecyclerView":
<?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"
android:layout_width="match_parent"
android:layout_height="#dimen/navdrawer_image_height">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/navDrawerHeaderView"
android:layout_width="match_parent"
android:layout_height="#dimen/navdrawer_image_height">
<ImageView
android:id="#+id/navdrawer_image"
android:layout_width="wrap_content"
android:layout_height="#dimen/navdrawer_image_height"
android:contentDescription="#string/cd_navdrawer_image"
android:scaleType="centerCrop"
android:src="#drawable/bg_material_design" />
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/app_image"
android:layout_width="#dimen/navdrawer_user_picture_size"
android:layout_height="#dimen/navdrawer_user_picture_size"
android:src="#drawable/ic_launcher"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
app:border_width="2dp"
app:border_color="#FF000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/appNameTextView"
android:text="App Name"
android:textStyle="bold"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:layout_alignParentBottom="true"
android:textColor="#android:color/white"/>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="#drawable/button_account"
app:layout_anchor="#id/navDrawerHeaderView"
app:layout_anchorGravity="bottom|right|end"
app:elevation="4dp"/>
</android.support.design.widget.CoordinatorLayout>
I think I might have the FAB in the wrong location/file. Here is the xml for 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"
android:id="#+id/drawerLayout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true">
<!-- Content layout -->
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar"/>
<FrameLayout
android:id="#+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/init_background">
</FrameLayout>
</LinearLayout>
<!-- Pages -->
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ffffff"
android:scrollbars="vertical"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
HELP!!!!!
example drawer fragment layout containing your existing RecyclerView:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="200dp">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00"
android:id="#+id/header"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:scrollbars="vertical"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="#id/appbar"
app:layout_anchorGravity="bottom|right|end"
android:layout_margin="5dp"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>