I am trying to figure out what is the proper way to remove an ImageView from a RelativeLayout is? I have tried
relativeLayout.removeView(someImageView)
but this causes some issues on certain phones. See this other post here for stacktrace. Removing ImageView causes crash on certain phones
I really don't want to do the solution where you just make it appear gone because that is basically leaking that ImageView in memory right?
someImageView.setVisibility(View.GONE);
Any other ideas would be much appreciated.
If you remove a view from a relative layout, you must reset the relative layout parameters for all other views which are dependent on the view that you are removing. Otherwise, these other views will reference your removed view and throw a NPE.
Consider using another root layout design (like LinearLayout) if you want to achieve this without using View.GONE
Related
I have two layouts, manageLayout and mainLayout. They have constraints to the parent on all sides. I need in a one time have manageLayout on the top of mainLayout, and on the other time mainLayout on the top of manageLayout. Of course, there is sense in using visibility=gone on one of them, but i need one layout on the background of another. Problem: layout on the background handle events from top layout. How to make lower layout(and his elements) untouchable when another layout is risen?
Layout tree image:
LayoutTreeImg
Code sample, where i want to disable communications with lower layout: https://pastebin.com/PeL7u3YD (not only isSaveEnabled=false had no effect, also isEnabled=false had no effects too)
If you just need an explanation.
Once you've initialized both your views for mainLayout and manageLayout, you will need to set an empty onClickListener on both of them. Basically, layouts should get the click but do nothing. This way you can block the layout and widgets underneath the view on Front from getting clicked.
Now for for switching view to front maintain a boolean to know which view is on the front and on your button click set the other view bringToFront() (Or try some other ways mentioned here if you want) and don't forget to switch the boolean value.
Let me know if this works for you or you have any issues regarding this.
According to my perception, you can make lower layout setEnable(false). I hope it will work.
I want to set a blank relativeLayout then start adding some views from another relativeLayout.
I want to do this because at the moment I have many bitmaps in a single layout which causes memory errors. So i want to achieve the effect of adding and removing views as I see fit in the code.
At the moment I am using the setContentView() of a layout on the onCreate() which causes me to have memory erros since there are too many views to add at once.
Another way. Perhaps it is possible to setContentView() of the layout with too many views. Only making it not adding any views before I code it to add specific views.
setContentView(R.layout.start_up_screen_denmark);
// This will add all the views in the layout causing a memory error. Making everything below irrelavant.
// So perhaps there is a way to set the ContentView without adding all the views automaticly.
// Perhaps i can set a blank layout and add views to that layout from the other layout at will.
ImageView denmark = (ImageView) findViewById(R.id.GoDenmark);
ViewGroup parent = (ViewGroup) denmark.getParent();
parent.removeView(denmark);
Using a LinearLayout might be better suited for what you're trying to do since you can easily add and remove views from a LinearLayout with lin_lay.addView(View child, int index) and lin_lay.removeViewAt(int index).
However, it also sounds like you might want to look into a RecyclerView if you have a lot of bitmaps in one layout and it's causing memory issues.
I'm trying to programatically position a TextView above a custom View in a Relative Layout that has also been placed programatically (lets call this CustomView).
I've scoured these forums for similar problems, and I seem to have done all the steps, including setting ids, making sure there are no conflicting rules, etc. But yet it doesn't seem to work.
I am wondering if this has to do with the fact that my CustomView class overrides the onDraw() method (it needs to eventually be animated). In fact, testing the CustomView with it's own set of LayoutParams shows that it does not obey the rules laid out in it - in fact the way that I place this CustomView is by calling canvas.drawBitmap() with the relevant positions in the onDraw() method of CustomView.
Any idea how I can get around this problem? Is it possible to somehow place the the TextView above the CustomView?
I have a layout, but I need put more views (Buttons, EditText, etc), but how you know, the layout of the ADT is a bit small and I can't to set more views or know where is it, Somebody know How can I set more of these?
Thank you
Android isn't like iOS how you can just position elements with drag & drop.
RelativeLayout will position elements relative to each other and LinearLayout will lay out elements sequentially in a linear fashion, either vertically or horizontally. Both methods are better executed by actually writing the View XML yourself.
I suggest reading this: http://developer.android.com/guide/topics/ui/declaring-layout.html
If you want to actually see what you are adding to your layout without messing with the XML you could maybe change the device that the renderer is using to preview your layout.
I don't know what IDE you're using but in Eclipse and Android Studio you can change the device that your layout preview is rendered on. That way you can see what you're adding as you add it.
Then just make sure to put everything in a scroll view so users can access all the views and widgets you've put in that layout for your activity or fragment or dialog or whatever else it is.
I'm using Eclipse graphical layout, and i want to Drag and Drop components to the layout and i want them to stay at the exact position that i dragged them.
Relative Layout is the closet thing but it changes the location of the components after couple insertion.
thanks.
Rami.
i want them to stay at the exact position that i dragged them.
No, you don't. Android devices come with screens in many different sized and resolutions, so fixed positioning of components is a recipe for making the application unusable to users with a different screen that you.
The point of layouts is that they can adjust to different screen sizes. Learn to use them properly.
You're looking for AbsoluteLayout, but it is deprecated.
RelativeLayout is the way to go, just pay some attention to the hints drawed on-screen when you place your elements.
You should be careful also to avoid circular references otherwise your items will be scattered randomly over the layout. (Example: Textview A on right of TextView B; TextView B on left of TextView A)
Be careful also when you change your element's id because the IDE does not update automatically all the references and thus your layout gets scattered again. When you update an ID you must update all its references in the XML file by hand.
Actually absolute layout is the answer what u want ,but that is deprecated.Relative layout does what you want.But it arrange its child based on parent position.If you drag a text view ,then other view will depend on this text view.If you have inserted 2 view,then third view will depend on the parent(2 views).But in between if u disturb any of the view ,it may affect other views.So do things systematically
In Java what we can typically use is GroupLayout. You may see what the equivalent is for android.