setContentView() deletes elements which have been added with addView() - java

I've 2 layouts on my project. Both have a button to swap between each other using setContentView method. Whenever I swap between these 2 layouts, every UI element I've added using addView() is lost. However static XML elements remains.

That's because the layout is inflated anew, with all the views specified in the xml when you call setContentView(R.layout.xml), that's happening behind the scenes, and all the dynamically added views will be gone.
Optional solutions:
Add the views again after you call setContentView().
The 2 layouts can live on top of each other and you can toggle their visibility. use GONE to hide the layout, not INVISIBLE If the layouts have clickable elements on them.

Related

RecyclerView is not returning all items

I'm making my first java android app and I'm having some issues with the RecyclerView.
I have a RecyclerView with a custom layout for each element.
I want to make that when I press the toolbar button, the Image Buttons inside EACH layout element inside the RecyclerView turn visible or invisible
This is the toolbar edit button code:
ArrayList<View> dest=new ArrayList<View>();
recView= findViewById(R.id.newListView);
recView.setItemViewCacheSize(0);
recView.findViewsWithText(dest,getString(R.string.onesingleuserlayout),View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
for (View oneUserLayout:dest)
{
oneUserLayout.findViewById(R.id.btnDelete).setVisibility(newVisibility);
oneUserLayout.findViewById(R.id.btnEdit).setVisibility(newVisibility);
}
It hides almost all the elements, but not every one of them. The ones shown behave correctly, and SOMETIMES the ones not showing don't.
I think that it is related to the cached items.
Remove this line, in order to permit caching:
recView.setItemViewCacheSize(0);
Here it's explained how it works (which may also explain, why caching is required).
Try to change your approach and instead of calling all those findView methods from outside the RecyclerView, implement a method on the adapter to change a boolean variable representing the visibility of the buttons and then call notifyDataSetChanged to force the layout manager to redraw all the visible itens and since all items reference the boolean variable, when new items are redrawn they will have the new visibility.

ListView with horizontally scrollable views

I'm currently developing an app in android.
I have a really basic listview that has 3 elements in his adapter:
1 imageview and 2 textviews.
These 3 items are inside a constraint layout.
All i ask is if i can make this layout (imgview + 2 tview) scrollable horizontally.
I've tried adding the layout to a horizontalscrollview, it works but i can't take the list onclicklistener.
I assume you want to be able to click on a list item? You could try adding a click listener to the scrollView. You probably also need to set clickable = true on the scrollview (not sure which is the default) and also just in case set clickable = false to all 3 child elements so they don't "eat" the click instead of the scrollView.
Use RecyclerView instead, you can add the onClickListener inside the adapter itself, or if you want the clickListener to be more interactive between your adapter and your activity/fragment, you can try using interface inside the adapter.

Can we get id of child linear layout by setting listener on parent linear layout?

I have one linear layout and inside it there are many child layouts. I don't want to put click listener on each and every child layout. I had set ids of child layout such that i can call respective activity using Class.forName().
So basically i am looking for a more productive way than putting click listener on each linear layout. Is there any way to meet this issue?

Fragment Z-order changes when orientation changes

My main xml file has a FrameLayout, with one fragment defined in the XML. At runtime, I am adding another fragment on top of this, and then a third fragment on top of this. When I add the runtime fragments, I don't give containerIds to attach them to. This is all fine until the orientation changes. After the orientation change, the third fragment has gone beneath the second fragment. But obviously I want the z-order to stay how it was before the orientation change, i.e. the last fragment added should be on top and visible.
I've tried using containerIds when adding the fragments, and adding them to the FrameLayout defined in XML, but when I put in containerIds, the runtime fragments simply don't show up at all.
So I'm wondering why the fragments added at runtime don't show when I use a containerId. Is it because they are being added to a container which already contains a fragment (the one defined in XML)? How can I get around this? I've tried adding additional containers to the XML to hold the runtime fragments, but this doesn't seem to work. The only way to get the fragments to show is to remove the containerId from the call to FragmentTransaction.add().
Also, is it possible to change the z-order of fragments manually? I've searched Google quite a lot to find this out and haven't found any answers. I've tried using ViewGroup.bringChildToFront() and using the view returned by the fragment's getView() but this hasn't worked. I've also found out that you can nest fragments on Android 4.2+ with getChildFragmentManager(), but I'm supporting older APIs, and this method doesn't seem to be in the support library.
Any help greatly appreciated!
The way I got around this was to manually recreate the third fragment after the orientation change, and remove the one created by the system.

Switching between layouts and keeping content displayed

In my main layout (mainlayout) I am displaying some text and images which are set dynamically based on the actions of the user. For one particular button click I need to display the contents of another layout (secondlayout). I do this using:
setContentView(R.layout.secondlayout);
On the second layout I have another button that I use to return to the main layout, once again using:
setContentView(R.layout.mainlayout);
The problem is on displaying the mainlayout again all the text and images I was displaying have now disappeared.
How can I return to the mainlayout and still display the contents I was displaying?
don't do it that way. setContentView() is meant to be called once in your onCreate() method. however, couple of reasonable ways to do it,
encapsulate each layout in a fragment, then show / hide each fragment as needed.
bundle both layouts into a single layout, and show / hide each section of the layout by calling setVisibility() on the layout's outermost container.

Categories

Resources