I have used navigation view in my app. Header in navigation view is repeated twice but i actually removed it (app:headerLayout="#layout/header") in xml. And i wanna implement action(like as button) from header of navigation view.
In code i have written like this
mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
headerView = mNavigationView.inflateHeaderView(R.layout.bp_header);
googele_plus = (ImageButton) headerView.findViewById(R.id.google_p);
facebook = (ImageButton) headerViewfindViewById(R.id.fb);
Header likes below
I got NullPointerException error if headerView removed from image button
Any suggestion or solution would be grateful
Update your support library to 23.1.1 or above.
After which you can do this -
Add the headerview in the app:headerLayout="#layout/header" inside NavigationView.
Then, you can access it by,
mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
View headerView = mNavigationView.getHeaderView(0)
googele_plus = (ImageButton) headerView.findViewById(R.id.google_p);
facebook = (ImageButton) headerView.findViewById(R.id.fb);
Ref : https://code.google.com/p/android/issues/detail?id=190226#c31
You may have header layout in xml and also you are adding header from java like
headerView = mNavigationView.inflateHeaderView(R.layout.bp_header);
So just remove above line from java and add it through the xml
You can add following in your xml
<include
android:id="#+id/header"
layout="#layout/navigation_header" />
and then in java create ref like below
googele_plus = (ImageButton) findViewById(R.id.google_p);
facebook = (ImageButton) findViewById(R.id.fb);
Clean and Build the project and then run
Seems like you create header 2 times. I had the same issue.
headerView = mNavigationView.inflateHeaderView(R.layout.bp_header);
this line ^ actually creates new View at runtime. And I believe you create the same header in XML. Try to remove that headerView from XML. That will solve your problem.
Well I also got NPE while working with NavigationView header where I was adding headers in layout app:headerLayout="#layout/header"
To get rid of this NPE I started adding header programatically like below
View header = LayoutInflater.from(this).inflate(R.layout.bp_header, null);
mNavigationView.addHeaderView(header);
googele_plus = (ImageButton) header.findViewById(R.id.google_p);
You may be called that navigation view header setting function twice. So it created two times.
If you written that setting navigation header view in onStart() method remove and write in onCreate() method.
onStart will call many times if you go to some other activity and returning back.
So it will create header as much times.
Or check in your code you are calling that code more than once.
In KOTLIN I have achieved by using below code.
step 1. remove app:headerLayout="#layout/your_header_layout" from NavigationView.
step 2. add below code in your activity
var nav = nav_view.inflateHeaderView(R.layout.nav_header_home)
nav?.nav_header_title?.text = "Your text "
Note: if you will not remove the app:headerLayout from NavigationView then header will be repeat twice.
Related
i dont know how to use it can anyone explain me how this can be done i already use some solutions but didnt work it does not show me the name on textview but i displayed in toast it is displaying and i inflate the view but still cant used then i programmatically hardcode string my app is crashing now please help me to find a suitable solution for this problem
Try This
NavigationView navigationView = (NavigationView) findViewById(R.id.your_navigation_view_id);//navigation view id
View hView = navigationView.getHeaderView(0);
TextView tv= (TextView) hView.findViewById(R.id.text);
Then use
tv.setText("");
inside the onCompleted() method.
you should write this code in your main activity onCreate:
View headerLayout = navigationView.getHeaderView(0);
TextView signuplogin = headerLayout.findViewById(R.id.signuplogin);
signuplogin.setText(R.string.signup_login);
It seems like you have another TextView with the name of "tv" but defined as a field. (Most probably at the top of your class)
Remove the field, and give it a try.
You need to make 2 changes
1 - set your Textview below View header
2 - also make header.findViewById(R.id.text) in Textview
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View header = navigationView.getHeaderView(0);
TextView tv = (TextView) header.findViewById(R.id.text)
How can I inflate FABRevealMenu? (the project's wiki page is here)
Since I'm newbie, I can't understand what layout to inflate which is mentioned in these rows:
You can inflate custom view by two ways
app:menuCustomView="#layoutRes" // you can get custom view object by calling getCustomView() on fabRevealMenu instance
setCustomView(View)
At the moment, I can't see any FABRevealMenu in my app and didn't get any error.
I added this to app_bar_main.xml:
<com.hlab.fabrevealmenu.view.FABRevealMenu
android:id="#+id/fabMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
app:animateItems="true"
app:menuBackgroundColor="#color/blue_semi_transparent"
app:menuDirection="left"
app:menuRes="#menu/main"
app:menuSize="normal"
app:menuTitleDisabledTextColor="#color/white"
app:menuTitleFontFamily="#font/italiana"
app:menuTitleTextColor="#color/white"
app:overlayBackground="#color/colorBlack"
app:showOverlay="true"
app:showTitle="true" />
Thanks in advance!
you will have to create a fab button first and then assign this feb menu on this button
final FloatingActionButton fab = view.findViewById(R.id.fab);
final FABRevealMenu fabMenu = view.findViewById(R.id.fabMenu);
//attach menu to fab
fabMenu.bindAnchorView(fab);
//set menu selection listener
fabMenu.setOnFABMenuSelectedListener(this);
Dont forget to implement OnFABMenuSelectedListener in your activity/fragment
if you have any problem ask freely!!
I have trouble deciding where to set the image of an ImageView contained in the Android NavigationDrawer.
My Android app has a NavigationDrawer that contains an ImageView nested into a couple of other elements inside the NavigationDrawer header layout (app:headerLayout="#layout/navdrawer_header), and my navdrawer_header.xml has the following structure:
<LinearLayout android:id="#+id/navdrawer_header">
...
<ImageView android:id=#+id/image_view
android:layout_width="match_parent"
android:layout_height="match_parent"/>
...
</LinearLayout>
I am setting the image (img.jpg in my drawable folder) via
ImageView imageView = (ImageView) view.findViewById(R.id.image_view);
imageView.setImageResource(R.drawable.img);
If called from OnCreate inside my MainActivity, the imageView will be null and the app crashes. The same if I move the above lines to OnPostCreate or even OnStart.
My understanding is that the view is not initialized yet at that point in time and therefore findViewById returns null. As a workaround, I have overwritten the OnDrawerOpened method and moved my code from above to that method.
This works just fine, but I am pretty sure it is not the best way to go since the method is called every time the drawer is opened. Instead, I would need a method that is only called once upon the creation of the drawer.
What is the best practice here, i.e. where should I set the resource of an ImageView that is contained in the NavigationDrawer?
I have tried omitting view. and experimented with different values of view, for example by assigning to it the NavigationView or DrawerLayout associated with my drawer, but it does not solve the issue.
Try this:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View view = navigationView.getHeaderView(0);
ImageView imageView = (ImageView) view.findViewById(R.id.image_view);
imageView.setImageResource(R.drawable.img);
I have a NavigationView in a Drawer Layout which contains a HeaderView followed by a Menu (as given in AndroidStudio's Navigation Drawer Activity's template). My Header contains an Image and couple of TextViews in a LinearLayout. I want to edit of one of the TextViews in the header from SharedPreferences. I use following function to do so
public void navUpdate()
{
navigationView=(NavigationView)findViewById(R.id.nav_view);
linearLayout=(LinearLayout) LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
textView_nname=(TextView)linearLayout.findViewById(R.id.textView_nname);
String name=sharedPreferences.getString("name","Your Name");
textView_nname.setText(name);
navigationView.removeHeaderView(linearLayout);
navigationView.addHeaderView(linearLayout);
}
The problem is , though I've added the line
navigationView.removeHeaderView(linearLayout);
my previous headerview is still visible above the new one. And previous one is not updated. I read about the method navigationview.getHeaderAt() in many answers but believe me, that method is not available.
Here's how it looks
Is there any solution for this problem, if not is there any other way to do so?
in order for you to get the Header from the NavigationView you'll have to call getHeaderView(int index) for instance:
View headerView = navigationView.getHeaderView(0);
textView_nname = (TextView)headerView.findViewById(R.id.textView_nname);
// set whatever you like on the textView.
Refer to the doc: http://android-developers.blogspot.com/2009/02/android-layout-tricks-2-reusing-layouts.html
I have a button inside the included layout, how can I access the button? I don't know the id! How can I define the OnClickListener...?
Please help...
The id you have with the include tag is assigned to the root View of the included layout.
First get a reference to that View using findViewByid. Then you can call findViewById on that specific View to get a reference to a View inside the layout. So:
View myLayout = findViewById( R.id.cell1 ); // root View id from that link
View myView = myLayout.findViewById( R.id.someinnerview ); // id of a view contained in the included file
All you need is the id of the parent view:
In this example, the LinearLayout is the parent of the TextView I want from the included View.
I can now do this:
View view = findViewById(R.id.include_title_layout);
TextView titleTV = (TextView) view.findViewById(R.id.newsTitleTextView);
titleTV.setText("Whatever");
You do know the id of the include. You can get that complete element, and then get one of its children starting from there.
Actually include tags include all the elements in your root layout. So you can always access them using findViewById on your Activity.
Suppose you have a alayout.xml in which there is a include layout. Now in one of your activity A,inside onCreate you declared setContentView(R.layout.alayout) Now inside your include layout you might have a button with id myBtn. You can access that inside onCreate in the same way you could access it if it were in main layout: findViewById(R.id.myBtn)
i think your mean is NavigationView. you can access to inner layout by this way:
NavigationView ngv= (NavigationView) findViewById(R.id.navigation_id);
View innerview = ngv.getHeaderView(0);
TextView user_view= (TextView)innerview.findViewById(R.id.nav_name); //any you need
user_view.setText(user);
works for me in java
<include
android:id="#+id/layout_infoProfile"
layout="#layout/user_info_doctor_profile_layout"
/>
preferences = mContext.getSharedPreferences(MyConstant.MY_SHARED_PREF,Context.MODE_PRIVATE);
View view = mView.findViewById(R.id.layout_infoProfile);
((TextView)view.findViewById(R.id.tv_name)).setText(preferences.getString(MyConstant.ROLE,MyConstant.NO_VALUE));