vandus
vandus

Reputation: 3288

Switching between multiple layouts in one LinearLayout dynamically

I have a spinner and a LinearLayout in my application. When the item in the spinner is changed, the LinearLayout is supposed to contain a different layout (a different xml file which is a LinearLayout, too). I´ve tried using View.inflate method but the layout never changed from the first one. What should I do to solve this problem?

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parentView,
                View selectedItemView, int position, long id) {
            changeForm(id);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            ;
        }
    });

}

private void changeForm(long id) {
    LinearLayout formLayout=(LinearLayout)findViewById(R.id.type_layout);
    if(id==0){View.inflate(this, R.layout.text, formLayout);}
    else if(id==3){View.inflate(this, R.layout.phonecall, formLayout);}     
}

Upvotes: 0

Views: 952

Answers (2)

blessanm86
blessanm86

Reputation: 31779

Another way to handle this would be to use the android ViewFlipper. That allows you to add mulitple child under it. And the viewflipper will only show 1 child at a time using a line like

viewflipper.setDisplayedChild(1);

It also allows for in and out animations while switching between views.

These are tutorials for it

Tutorial 1, Tutorial 2

Upvotes: 0

H9kDroid
H9kDroid

Reputation: 1824

You can also put all of your LinearLayouts in a FrameLayout, then just change the visibility on them.

Upvotes: 1

Related Questions