188

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?

4 Answers 4

386
+50

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.

7
  • Must I remove the fragment, or will the fragment also be removed?
    – basickarl
    Jul 7, 2014 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, 2014 at 9:13
  • It is a pity your answer is too complex - in the comments - for novice Android programmers :-( Feb 4, 2015 at 21:07
  • 2
    @AntonioSesto what is too complex? Where did you get confused?
    – AdamMc331
    Sep 11, 2015 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 ? Apr 25, 2017 at 6:57
44

And if you do that very often, you could use a ViewSwitcher or a ViewFlipper to ease view substitution.

4
  • Yes, if there is question of alternate views, then view flipper/switcher is better to use
    – Zoombie
    Aug 4, 2011 at 15:10
  • 2
    @Shail Adi, did you try to goole an ViewSwitcher tutorial code ?
    – Snicolas
    Nov 21, 2012 at 14:03
  • I know this is old - but the caveat is that a ViewSwitcher initializes and holds both Views in itself (a FrameLayout). Apr 13, 2018 at 16:35
  • This should be the recommend answer. Why not just use a ready made well tested solution.
    – user9599745
    Sep 25, 2019 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);
    }
1
  • 7
    While 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, 2019 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);
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.