I have a xml
-layout file main with two textviews A/B and a view C.
I have two other xml
-layout files option1
and option2
.
Is it possible to load either option1
or option2
in run time via Java into C? If so, what function do I have to use?
Asked
Active
Viewed 1.3e+01k times
188

Subhanshuja
- 390
- 1
- 3
- 20

Christian
- 25,249
- 40
- 134
- 225
4 Answers
386
You could replace any view at any time.
int optionId = someExpression ? R.layout.option1 : R.layout.option2;
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);
If you don't want to replace already existing View, but choose between option1/option2 at initialization time, then you could do this easier: set android:id
for parent layout and then:
ViewGroup parent = (ViewGroup) findViewById(R.id.parent);
View C = getLayoutInflater().inflate(optionId, parent, false);
parent.addView(C, index);
You will have to set "index" to proper value depending on views structure. You could also use a ViewStub: add your C view as ViewStub and then:
ViewStub C = (ViewStub) findViewById(R.id.C);
C.setLayoutResource(optionId);
C.inflate();
That way you won't have to worry about above "index" value if you will want to restructure your XML layout.

ישו אוהב אותך
- 28,609
- 11
- 78
- 96

broot
- 21,588
- 3
- 30
- 35
-
Must I remove the fragment, or will the fragment also be removed? – basickarl Jul 07 '14 at 23:59
-
4@KarlMorrison This question is almost 4 years old and has nothing to do with fragments. I suggest to file a new question and/or take a look at `android.app.FragmentTransaction` and it's `replace()` method. – lupz Jul 11 '14 at 09:13
-
It is a pity your answer is too complex - in the comments - for novice Android programmers :-( – Antonio Sesto Feb 04 '15 at 21:07
-
2@AntonioSesto what is too complex? Where did you get confused? – AdamMc331 Sep 11 '15 at 13:57
-
@broot I want to add Images daynamically to my linearlayout that i have done. later I remove some of images from layout. then I want to restore it at its own position from where I have removed. means I want to add images without removing child's parent can you please hlep me ? – PriyankaChauhan Apr 25 '17 at 06:57
-
After I changed orientation, it went back to the old view. Any way to workaround this bug, aside from implementing viewflipper/switcher? – pete Jul 01 '17 at 10:03
-
@pete it's not a bug. After rotation the activity is destroyed and recreated - so your xml is once again used to recreate the layout - so once again you have to manipulate the layout to your liking. I suggest you write a method that manipulates the Views and call that method after every onCreate() – Someone Somewhere Sep 09 '17 at 13:26
44
And if you do that very often, you could use a ViewSwitcher or a ViewFlipper to ease view substitution.
-
Yes, if there is question of alternate views, then view flipper/switcher is better to use – Zoombie Aug 04 '11 at 15:10
-
2
-
I know this is old - but the caveat is that a ViewSwitcher initializes and holds both Views in itself (a FrameLayout). – Vikram Bodicherla Apr 13 '18 at 16:35
-
This should be the recommend answer. Why not just use a ready made well tested solution. – Sep 25 '19 at 17:03
4
private void replaceView(View oldV,View newV){
ViewGroup par = (ViewGroup)oldV.getParent();
if(par == null){return;}
int i1 = par.indexOfChild(oldV);
par.removeViewAt(i1);
par.addView(newV,i1);
}

Kaiwalya Rangle
- 43
- 3
-
7While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value – frobinsonj Jun 27 '19 at 15:26
0
it work in my case, oldSensor and newSnsor - oldView and newView:
private void replaceSensors(View oldSensor, View newSensor) {
ViewGroup parent = (ViewGroup) oldSensor.getParent();
if (parent == null) {
return;
}
int indexOldSensor = parent.indexOfChild(oldSensor);
int indexNewSensor = parent.indexOfChild(newSensor);
parent.removeView(oldSensor);
parent.addView(oldSensor, indexNewSensor);
parent.removeView(newSensor);
parent.addView(newSensor, indexOldSensor);
}

ch13mob
- 261
- 6
- 11