Search code examples
javaandroidandroid-layoutlayouthide

removeview() doesn't remove android


I have a layout (main) that when i click on a button it shows a RelativeLayout (newLayout) with a textview and a edittext. But when i want to leave that RelativeLayout (clicking on some button), it doesn´t dissappear. My code is this:

When I click on the button, this is the code:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_hide:
            RelativeLayout main = (RelativeLayout) findViewById(R.id.atraccion_layout);
            RelativeLayout newLayout= (RelativeLayout) View.inflate(this, R.layout.newLayout, null);     
            main.addView(newLayout);
        default: 
            return super.onOptionsItemSelected(item);
    }           

}

Then when I click over another button, my code is:

public void close(View v){
    RelativeLayout main = (RelativeLayout) findViewById(R.id.atraccion_layout);
    RelativeLayout newLayout = (RelativeLayout) View.inflate(this, R.layout.newLayout, null);
    main.removeView(comentarLayout);
    main.forceLayout();
}

But the newLayout is still there.

Tried also:

((RelativeLayout)v.getParent()).removeView(v);

main.removeView((View)v.getParent());

newLayout.setVisibility(v.INVISIBLE);

newLayout.setVisibility(v.GONE);

((RelativeLayout)newLayout.getParent()).forceLayout();

((RelativeLayout)newLayout.getParent()).removeView(comentarLayout);

Without success.

Can someone help me why isnt it removing the layout?


Solution

  • I resolved it by defining class atributes whith those layers

    public class ActivityClass extends Activity{
        private RelativeLayout main;
        private RelativeLayout newLayout;
    
        protected void onCreate(Bundle b){...}
        ...
    
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.action_hide:
                main = (RelativeLayout) findViewById(R.id.atraccion_layout);
                newLayout= (RelativeLayout) View.inflate(this, R.layout.newLayout, null);     
                main.addView(newLayout);
                return true;
            }
            default:
                return super.onOptionsItemSelected(item);
        }
    
        public void close(View v){
            main.removeView(newLayout);
            main.forceLayout();
        }  
    }