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);
Related
I have custom DrawerLayout with below codes.
I don't know why my layout (drawer_layout) take margin from top and the red close button that you can see below is being covered.
<?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"
tools:context=".DashboardActivity"
android:layout_gravity="left"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
[Some other elements here ...]
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginRight="-64dp"
android:fitsSystemWindows="true">
<include layout="#layout/drawer_layout" />
</android.support.design.widget.NavigationView>
and its my custom layout for NavigationView :
<?xml version="1.0" encoding="utf-8"?>
<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:background="#color/primary_color"
android:layout_height="match_parent">
<ImageView
[Properties]
/>
<TextView
[Properties]
/>
<TextView
[Properties]
/>
<ImageView
[Properties]
/>
<ImageButton
android:id="#+id/drawer_close_image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/radius_background_acent_color"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_close_black_24dp" />
</android.support.constraint.ConstraintLayout>
but in result I have a negative margin top that can be seen in below picture.
How can I remove that margin from top and show my NavigationView layout correctly?
Remove android:fitsSystemWindows="true". That line tells the layout engine to place elements using the whole screen, including the bit under the status bar.
I believe android:fitsSystemWindows="true" is causing your problem.
Try removing this line
I have a navigation menu with some items. The problem that I am facing is the Spinner items and other UI elements which is underneath the menu when it is open remains clickable even when the menu is on top of those, as explained in the figure.
Here is my activity layout files.(activity_main.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="match_parent">
<Spinner
android:id="#+id/delivery_area_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:background="#android:color/transparent"
android:gravity="center"
android:spinnerMode="dropdown" />
<include layout="#layout/layout_drawer_navigation" />
</RelativeLayout>
This is my navigation drawer layout.
<?xml version="1.0" encoding="utf-8"?>
<merge 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="wrap_content"
android:layout_height="wrap_content"
android:clickable="true">
<android.support.v4.widget.DrawerLayout
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="wrap_content"
android:layout_height="wrap_content" />
<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/layout_main_navigation_header"
app:menu="#menu/main_drawer_menu" />
</android.support.v4.widget.DrawerLayout>
I tried to look for solutions and found that for many people setting android:clickable="true" does the trick but in my case that is also not working.'
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'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");
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);