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);
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)
I have a frame-layout defined in xml a follows:
//pseudo code
<FrameLayout
android:height = "match_parent" // same for width
android:background="#android:color/white"
padding = "20dp"
id = "polygraph"
layout_gravity="center"/>
I access this xml view via code as such:
LayoutInflater inflater = this.getLayoutInflater();
frameInflate = (FrameLayout) inflater.inflate(R.layout.mframe,null);
frar = (FrameLayout)frameInflate.findViewById(R.id.polygraph);
I create the following image-view dynamically but somehow it doesn't show inside this frame-layout which is one of many views inside another frame-layout that holds all the views of my User-Interface
view1 = new ImageView(this);
view1.setAdjustViewBounds(false);
matrix = new Matrix();
view1.setScaleType(ImageView.ScaleType.Matrix);//I have tried: ScaleType.Center it did not work at all
view1.setImageMatrix(matrix);
view1.setBackgroundColor(Color.GRAY);
params11= new FrameLayout.LayoutParams(200,300,Gravity.CENTER);
view1.setLayoutParams(params11);
DummyDraw drawD = new DummyDraw(this);
view1.setImageDrawable(drawD);
frar.addView(view1);
//somewhere down on onWindowsFocusChange()
frar.invalidate(); //try to invalidate to see if it will show the imageview
No image-view shows at all. I have tried different ways, such as:
frar.addView(view1,params11); //did not work
//instead of view1.setImageDrawable
view1.setBackground(drawD); //nothing
view1.setImageResource(R.drawable.somerandomdrawable);//nothing
I have no idea why this view isn't showing. Could it be the drawable "drawD"? If it is this drawable, then why isn't showing at least the background color "gray"
which I set in the code? I have tried to get rid of the ScaleType.Matrix to something else, such as ScaleType.Center, but nothing seems to work.
Any advice or suggestions will be appreciated
thanks
I had ran into the same problem a few months ago. But I did forget how to handle it. LayoutInflater wasn't needed to solve the problem. Seems like I can't do this call inside onCreate() "(FrameLayout)findViewById(R.id.polygraph)" and then try to add my programmatically created image-view to my xml layout element. It gave me a null pointer exception all the time.
Moving both calls way after the layout pass, such as on onStart() or onWindowsFocusChanged() works well. The image-view is added to the xml frame-layout, which in the end it works well for my app, since I had the intentions of adding the image-view well beyond the layout pass.
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.
I have a problem, I would like to put in my Layout the Drawer Layout and the FlyOutContainer but in the error Log it shows an error:that DrawerLayout cannot be cast to FlyOutContainer what can I do to fix this problem. I need the Drawer Layout to go to the MainActivity.
Hope you can help me and sorry for my bad english.
The code in that demo inflates a layout and casts the root view of the Layout to FlyOutContainer. If you change your layout xml so that the root is now DrawerLayout, then that code no longer runs correctly and you are probably getting ClassCastException. You should do it this way instead (in onCreate()):
setContentView(R.layout.your_activity_layout);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout_id);
FlyOutContainer flyOutContainner = (FlyOutContainer) findViewById(R.id.fly_out_container_id);
Of course, if you don't actually need a reference to these views/layouts, you can just stop after setContentView(...)