I have an activity and a fragment. Activity has a trigger event which adds an Imageview to fragment every time event is triggered.
Now I wish to programmatically add image view on every event trigger and refresh the fragment UI.
How can I implement this?
The fragment is just a container for your layout. Basically, you'd have to call addView on the ViewGroup layout that is hosted in the fragment. The ViewGroup could be a LinearLayout for instance. Link to documentation: https://developer.android.com/reference/android/view/ViewManager.html#addView(android.view.View,%20android.view.ViewGroup.LayoutParams)
But as mentioned, you could use a RecyclerView or a ListView and an adapter to accomplish this easier, as it would also save you from messing with LayoutParams, plus it would give you some performance improvements with the help of recycling.
why don't you use a recyclerview then?
Just use a recyclerview and whenever you get the trigger you can add that image dynamically.. or you can inflate image view directly on the parent view.. like: LayoutInflater.from(context).inflate(R.layout.ivLay,'your main layout',true);
ivLay.xml
Related
I created a custom layout which can have an arbitrary amount of child views.
I figured out that the child views are not available when I try to access them in the constructor of my layout view.
So what I currently do is to access them in onMeassure, but it seems to be a bad idea, since this gets called several times.
What is the best place in my layout to init child views? I wish there was a method like onChildViewsAttached(). Any ideas?
Child views are attached to the parent once layout pass is finished, i.e layout() of the view group is finished.
You can also register OnGlobalLayoutListener or OnPredrawListeneron the ViewTreeObserver of your custom layout.
I have a little problem with my DialogFragment., a custom one I wrote for my app.
In this fragment I have one button which must start showing custom view which are inflated to root frame layout of my app.
Could you please tell me how can I do this?
If I am getting you correctly, you want to display your view only when the button is pressed.
So, you need to use the android:visibility attribute here and use either GONE or INVISIBLE values.
Then when the button is pressed, retrieve an instance of the custom view, and then set the value to VISIBLE using the setVisibility() method.
First of all I will explain my scenario.
I have an activity with a linear layout and I am inflating that linear layout with custom views. In the custom view I have a remove button to remove the particular child.
I know to remove the view I have to add an onClick Listener to the button in the custom view. But what I am not sure about is that how can I remove the view from its parent view from that onClick Listener.
I am populating the linear layout from items stored in shared preferences. So I thought first to update the shared preference by removing the string of the particular custom view I am clicking. But I don't know how I cn manage to re-populate the linear layout, since the function is in the parent activity.
I am a complete newbie in android. Thanks in advance
update
I'll make the scenario a little more simple.
I have a custom view with two imagebuttons - update & remove.
If i click in the remove button, then the custom view should be removed from the linearlayout.
And if I click in the update imagebutton, a function of the Activity which hosts the Linearlayout should be called with the text in the customview as a parameter.
How can i do this. I tried many ways, but failed :-(
I found the answer finally :
For the first requirement, ie., to remove the view, I used:
LinearLayout parentLyt = (LinearLayout) RecentSearch.this.getParent();
parentLyt.removeView(RecentSearch.this);
Thanks Very much to Piyush Gupta (#piyush)
For the second option, to access the hosting activity, I used getContext() and casted it to my activity class, then called the method.
Thanks every one who replied.
I have a container, usually a common LinearLayout, where I want to add views. After the user clicks on a view, the view should be removed from the container. Obviously, I can implement the functionality in an OnClickListener, and attach it to the view.
But, if another programmer forgets, to remove the view, after doing his stuff in his OnClickListener, the view will remain in the container.
Is there a way, that the container can enforce the removal? I haven't seen a View#getOnClickListener.
Why don't you create your own container and when addView is called you can modify the view's onClickListener.
EDIT:
This is not possible since there is no getOnClickListener and also the variable that holds the listener has the {#hide}.
I guess the only way out if extending each View and overriding the setOnClickListener method.
I have built my interface by using ViewStubs, which I inflate during onCreate.
But later in my app, I want to change the View completely, by loading different View into the same place. How do I achieve that?
Remove the old View via removeView(). Then inflate and add the replacement via addView().
Though if you're going to be bouncing back and forth a lot, consider using a FrameLayout or ViewFlipper or something to have both views loaded at once, only making one visible.