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)
Related
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 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.
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.
well i want to inflate layout and set wanted values from Login Activity to a Drawer Header XML file, goal is to see this values in Main Activity too...
here`s my code:
XML: http://pastebin.com/MDpQh8S8
CODE: http://pastebin.com/436SNfCN
so what a heck is problem over here?
Found solution:
as im including header layout into a MainActivity.xml just finding parameters was enough, didn`t even needed to inflate layout xml
so, pasting whole inflater and changer code to MainAct and then:
TextView username = (TextView) findViewById(R.id.username);
TextView email = (TextView) findViewById(R.id.email);
just worked fine :) anyway thanks for trying to help me
I would like to ask if there is any methods to get a View from another View.
My case is that:
I have a custom ImageView call MyImageView.
and my layout is that:
FrameLayout:
MyImageView
LiearLayout:
LiearLayout:
TextView
LiearLayout:
TextView
I have some code in MyImageView and I would like to edit the text in TextView under the LinearLayout.
my code in MyImageView for select the TextView is:
TextView textView = (TextView) findViewById(id.TextView01);
However textView is always null and I can't set the text that I prefered.
More, if I code this:
TextView textView = (TextView) findViewById(R.id.TextView01);
Eclipse will give me a error and said can;t resolve the id.
So Is there any methods to edit TextView from a ImageView?
First of all... that's a horrible idea (I would be freaked out if I were you). And it's big signal that you have to rethink how to do your layout.
Anyway, using the getParent method could work:
// in your MyImageView
FrameLayout parent = (FrameLayout)getParent();
TextView textView = (TextView) parent.findViewById(R.id.TextView01);
It's not working the way you are doing it, since the TextViews are not inside your custom view.
I think you can always call getRootView() and from there you can search for a view by Id. As long as the ids are unique within the current view hierarchy you should be able to grab the textView no matter where it is in the hierarchy.
So similar to the other answer
TextView textView = (TextView) getRootView().findViewById(R.id.textview_01);
if it's on the same screen, it will be there always.