Question

I want to change Inflated view of main_inflater by click each button.

here is my source code;

I initiated My two buttons and setOnListener to two buttons.

But inflater just show the views that is inflated first time.

public class MainActivity extends Activity implements OnClickListener{

//optionmenu
int titleHeight;
int mainHeight;
int menuHeight;

//objects
TextView tv_status;
Tabbar tb_status;
LinearLayout titleLayout;
LinearLayout mainLayout;
LinearLayout menuLayout;
Button btn1;
Button btn2;

LayoutInflater main_inflater;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    layout_init();
    btn_init();


}   

public void layout_init() {

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int deviceWidth = displayMetrics.widthPixels;
    int deviceHeight = displayMetrics.heightPixels;
    titleHeight = 2*deviceHeight/26;
    mainHeight = 21*deviceHeight/26;
    menuHeight = 3*deviceHeight/26;

    main_inflater = (LayoutInflater) this.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    //initiate Layouts
    titleLayout = (LinearLayout) findViewById(R.id.title_layout);
    mainLayout = (LinearLayout) findViewById(R.id.main_layout);
    menuLayout = (LinearLayout) findViewById(R.id.menu_layout);

    titleLayout.getLayoutParams().height = titleHeight;
    mainLayout.getLayoutParams().height = mainHeight;
    menuLayout.getLayoutParams().height = menuHeight;

    main_inflater.inflate(R.layout.viewpager_layout, mainLayout, true);


}

public void btn_init() {
    btn1 = (Button) findViewById(R.id.button1);
    btn2 = (Button) findViewById(R.id.button2);
    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
}


@Override

public void onClick(View v) {
    if(v.getId() == R.id.button1)
    {
        main_inflater.inflate(R.layout.viewpager_layout, mainLayout, false);
    }
    if(v.getId() == R.id.button2)
    {
        main_inflater.inflate(R.layout.person_info, mainLayout, false);}

    Toast.makeText(getBaseContext() , "id : "+v.getId() ,Toast.LENGTH_SHORT ).show();


}

}

toast message is working. But inflater didn't change view. plz tell me why bbbb

Was it helpful?

Solution

In your onClick method, you are passing false as last parameter to inflate. This prevents the inflated layout from being attached to the root. Try passing true as last parameter, as shown below:

public void onClick(View v) {
    if(v.getId() == R.id.button1)
    {
        mainLayout.removeAllViews();
        main_inflater.inflate(R.layout.viewpager_layout, mainLayout, true);
    }
    if(v.getId() == R.id.button2)
    {
        mainLayout.removeAllViews();
        main_inflater.inflate(R.layout.person_info, mainLayout, true);
    }
    Toast.makeText(getBaseContext() , "id : "+v.getId() ,Toast.LENGTH_SHORT ).show();    
}

OTHER TIPS

Inflater does nothing but inflate the view, that is, to convert the xml of viewpager_layout.xml to an actual object, which is a View or a subclass of View.

If you are trying to set the view of the entire Activity...

Replace

main_inflater.inflater(R.layout.viewpager_layout, mainLayout, true);

with the following:

setContentView(R.layout.viewpager_layout);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top