How to remove a layout that was added using addContentView()? - android

I am adding a layout using addContentView().
How can i remove this layout on a Button click ?

Assuming contentView is the view that was added via window.addContentView()
((ViewGroup) contentView.getParent()).removeView(contentView);

try it
View youAddedView;
ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
for (int i = 0; i < rootView.getChildCount(); i++) {
if(rootView.getChildAt(i) == yourAddedView) {
// do anything here
}
}

If you already have the reference to the view you can simply just do :
ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
rootView.removeView(viewToRemove);
Instead of looping through the ViewGroup.

Unfortunately there's no way of removing a content view that was added with addContentView(). The best you can you do is to call setVisibility(View.GONE) on it, to hide it.
That is why the activity's onContentChanged() only gets called when the content view is set or added to an activity.

you can do two things over here you can set the visibility to gone on the click event of the button.
OR
you can set the layout parameter to layout width and height to 0dp
It will hide your layout display

Related

Dynamically Add a overlay view on top of a fragment android

I have this requirement where I have a fragment Instance and I want to dynamically add a view on the top of the fragment irrespective of the root viewGroup of the Fragment.
Something like this
:
So is it possible to get the root view of the fragment from its instance? I know for activity I can do this: activity.findViewById(android.R.id.content);
Also since a fragment can have any type of view group as its rootview like LinearLayout, RecyclerView, RelativeLayout, FrameLayout, Is there a generic way to add an overlay view on the top of the fragment?
If it is not possible then should I just add a dummy view on top of the fragment in xml layout and then use that view to add the overlay?
The getView() method returns a View that contains the Fragment's root layout. From there you could either cast it to ViewGroup and leave it at that to add a simple view, use an if-else block with instanceof to determine its type:
First:
ViewGroup viewGroup = (ViewGroup) fragment.getView();
Second:
View view = fragment.getView();
if (view instanceof FrameLayout) {
FrameLayout root = (FrameLayout) view;
} else if (view instanceof RelativeLayout) {
}
//etc...
Third:
Edit: Removed this option. It actually wont work.
You could use a Child Fragment but if you are only interested in the Fragments root view, just retain the View you are returning in the fragments onCreateView() method

Re-arrange the view order on Framelayout android

I've add five views on frameLayout.
how to re arrange the childIndex of framelayout.
i use below code:
fromindex = 3;
toindex = 4;
View tempFrom = frameLayout.getChildAt(fromindex);
View tempTo = frameLayout.getChildAt(toindex);
frameLayout.removeViewAt(fromindex)
frameLayout.removeViewAt(toindex)
frameLayout.addView(tempFrom, toindex)
frameLayout.addView(tempTo,fromindex)
But its throws the below error.
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
How to re arrange the childindex of framelayout ?
how to re arrange the childIndex of framelayout.
The FrameLayout doesn't have the ability to re arrange its children directly but you can do it by removing those children and re adding them in the right positions. Your code doesn't work because you remove the views in an incorrect order resulting in views still being attached to the parent:
fromindex = 3;
toindex = 4;
View tempFrom = frameLayout.getChildAt(fromindex);
View tempTo = frameLayout.getChildAt(toindex);
// first remove the view which is above in the parent's stack
// otherwise, if you remove the other child you'll call the `removeViewAt`
// method with the wrong index and the view which was supposed to be detached
// from the parent is still attached to it
frameLayout.removeViewAt(toindex);
frameLayout.removeViewAt(fromindex);
// first add the child which is lower in the hierarchy so you add the views
// in the correct order
frameLayout.addView(tempTo,fromindex)
frameLayout.addView(tempFrom, toindex)
Try this code
frameLayout.removeView(tempFrom) //to remove views
frameLayout.removeView(tempTo)
You can't swap views I think. You need to define new view group and inflate to his parent and remove old one.
View C = findViewById(R.id.C);
ViewGroup parent = (ViewGroup) C.getParent();
int index = parent.indexOfChild(C);
parent.removeView(C);
C = getLayoutInflater().inflate(optionId, parent, false);
parent.addView(C, index);

Get layout from view

I am inflating interface from XML using
View add_phone = getLayoutInflater().inflate(R.layout.phone_info, null);
Now how can i access RelativeLayout from add_phone view? is there any methos like getChildCount() ?
yes , getChildCount(), works on a ViewGroup like LinearLayout, RelativeLayout etc..
ViewGroup add_phone = (ViewGroup) getLayoutInflater().inflate(R.layout.phone_info, null);
int childCount = add_phone.getChildCount();
you must make sure the inflated layout has viewGroup as parent view, otherwise you will get class cast exception. viewGroup can be anything like LinearLayout, RelativeLayout etc..
You can find child views of a view through
View.findViewById(int id)
In your case, that translates to
RelativeLayout child = (RelativeLayout)add_phone.findViewById(R.layout.phone_info)
As long as you have unique id's for the child elements in add_phone, this should return the correct element.

How to get Position of an view added dynamically on LinearLayout

I need to get an dynamically added view position in LinearLayout with vertical orientation.
For example i have 4 TextViews added dynamically on LinearLayout, then i need to change position of text colour at 3rd position will be in different color.How can i achieve it by getting position of added views.
You can do it just like that
ViewGroup parent;
int position;
for(int i = 0; i < parent.getChildCount(); ++i) {
int currentViewId = parent.getChildAt(i).getId();
if(currentViewId == wantendViewId) {
position = i;
}
}
That's (in my opinion) the simplest way
If you always know the number of TextViews in your LinearLayout, you can just use the function getChildAt( int position ). This returns a View which you can then cast to a TextView to be able to perform the desired operations.
If you do not know the number of elements you could set the id of each TextView (in order to be able to identify a particular one) and then run through them like this:
for( View view : myLinearLayout )
if( view instanceof TextView && view.getId().equals( idToSearchFor ) )
//Do what needs to be done.
I see following options:
Declare some id's in resources in form of <item type="id">first</item> and assign them to
views in adding to layout, after that use normal findViewById() mechanism
Assign some tags to views you're adding to a layout via setTag method and after that use findViewWithTag mechanism
Remeber position of your views and use them vie getChildAt method
I got simple option.
suppose you add
View v;//any view
linearlayout.addview(v);//add in layout
While u want to modify view.
simpaly remove old view.
linearlayout.removeView(v);
add new update view object
v-updated new view
linearlayout.addview(v);

How to inflate one view with a layout

I have a layout defined in XML. It contains also:
<RelativeLayout
android:id="#+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
I would like to inflate this RelativeView with other XML layout file. I may use different layouts depending on a situation. How should I do it? I was trying different variations of
RelativeLayout item = (RelativeLayout) findViewById(R.id.item);
item.inflate(...)
But none of them worked fine.
I'm not sure I have followed your question- are you trying to attach a child view to the RelativeLayout? If so you want to do something along the lines of:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
You inflate an XML resource. See the LayoutInflater doc .
If your layout is in a mylayout.xml, you would do something like:
View view;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
RelativeLayout item = (RelativeLayout) view.findViewById(R.id.item);
Though late answer,
but would like to add that one way to get this
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.mylayout, item );
where item is the parent layout where you want to add a child layout.
It's helpful to add to this, even though it's an old post, that if the child view that is being inflated from xml is to be added to a viewgroup layout, you need to call inflate with a clue of what type of viewgroup it is going to be added to. Like:
View child = getLayoutInflater().inflate(R.layout.child, item, false);
The inflate method is quite overloaded and describes this part of the usage in the docs. I had a problem where a single view inflated from xml wasn't aligning in the parent properly until I made this type of change.
Even simpler way is to use
View child = View.inflate(context, R.layout.child, null)
item.addChild(child) //attach to your item
Try this code :
If you just want to inflate your layout :
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,null); // Code for inflating xml layout
RelativeLayout item = view.findViewById(R.id.item);
If you want to inflate your layout in container(parent layout) :
LinearLayout parent = findViewById(R.id.container); //parent layout.
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,parent,false);
RelativeLayout item = view.findViewById(R.id.item); //initialize layout & By this you can also perform any event.
parent.addView(view); //adding your inflated layout in parent layout.
layout inflation
View view = null;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
main.addView(view);
If you're not in an activity you can use the static from() method from the LayoutInflater class to get a LayoutInflater, or request the service from the context method getSystemService() too :
LayoutInflater i;
Context x; //Assuming here that x is a valid context, not null
i = (LayoutInflater) x.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//OR
i = LayoutInflater.from(x);
(I know it's almost 4 years ago but still worth mentioning)
AttachToRoot Set to True
Just think we specified a button in an XML layout file with its layout width and layout height set to match_parent.
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/custom_button">
</Button>
On This Buttons Click Event We Can Set Following Code to Inflate Layout on This Activity.
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.yourlayoutname, this);
Hope this solution works for you.!
If you want to add a single view multiple time then you have to use
layoutInflaterForButton = getActivity().getLayoutInflater();
for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);
btnContainer.addView(btnView);
}
If you do like
layoutInflaterForButton = getActivity().getLayoutInflater();
FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);
and
for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
btnContainer.addView(btnView);
}
then it will throw exception of all ready added view.
If you are you trying to attach a child view to the RelativeLayout? you can do by following
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, item, true);
I had the hardest time with this error, because of my unique circumstances, but finally found a solution.
My situation: I am using a separate view (XML) which holds a WebView, then opens in an AlertDialog when I click a button in my main activity view. But somehow or another the WebView belonged to the main activity view (probably because I pull the resource from here), so right before I assigned it to my AlertDialog (as a view), I had to get the parent of my WebView, put it into a ViewGroup, then remove all the views on that ViewGroup. This worked, and my error went away.
// set up Alert Dialog box
AlertDialog.Builder alert = new AlertDialog.Builder(this);
// inflate other xml where WebView is
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.your_webview_layout, null);
final WebView webView = (WebView) v.findViewById(R.id.your_webview_id);
// more code...
.... later on after I loaded my WebView ....
// first, remove the parent of WebView from it's old parent so can be assigned a new one.
ViewGroup vg = (ViewGroup) webView.getParent();
vg.removeAllViews();
// put WebView in Dialog box
alert.setView(webView);
alert.show();
With Kotlin, you can use:
val content = LayoutInflater.from(context).inflate(R.layout.[custom_layout_name], null)
[your_main_layout].apply {
//..
addView(content)
}
When add a layout to an Activity in Kotlin, see these steps:
Add just in Activity - One layout as a parent
Xml file of new
Layout Give the value of R.
val parent: LinearLayout =findViewById(R.id.stars)
val view =
LayoutInflater.from(applicationContext).inflate(R.layout.another, parent,false)
Where parent is not necessary, can be null But warning message will be appear
view.findViewById<ImageView>(R.id.ivTimer).setImageResource(R.drawable.t2)
Any view must be set value in this way, finally add
parent.apply { addView(view)}
I had used below snippet of code for this and it worked for me.
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
linearLayout.addView(child);

Categories

Resources