0

I have a parent custom layout (child of LinearLayout) that I inflate programmatically.

In the constructor I add another layout to be a child of my parent layout like this:

public ExtendedContentLinearLayout(Context context) {
    super(context);
    this.context = context;

    this.setClickable(false);

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mTrigger = inflater.inflate(R.layout.nav_menu_board_expandable_children_outer, this, true);
    mTrigger.setOnClickListener(this);
    mTrigger.setBackgroundResource(R.color.da_blue);


}

The parent layout and the "trigger" layout (The child just added above) are working and shown fine.

Once the user touches this "trigger" layout, I want to add a new child to the parent - another layout with more content and thus I need to parent layout to expand and include the newly added layout but this is not working!

Clicking the trigger layout doesn't add a new layout (At least not shown on screen). This is the click call (the click event is working):

   @Override
    public void onClick(View v) {
        App.disableTouchEventForDefaultDuration();

    Log.d("CLICKED", "CLICKED");


    LinearLayout mContent = new LinearLayout(context);
    mContent.setOrientation(HORIZONTAL);
    mContent.setBackgroundResource(R.color.da_indicator_pink);
    mContent.setVisibility(VISIBLE);
    ExtendedContentLinearLayout.this.addView(mContent);


}
Bended
  • 494
  • 1
  • 7
  • 22

3 Answers3

1

I ended up creating the layout in XML and then inflating it using:

 View view = inflater.inflate(this.layout, null, false);

and then adding this view using addView(view)

this works just fine for me

Bended
  • 494
  • 1
  • 7
  • 22
0
mContent.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

Please try above code , try for WRAP_CONTENT as well as MATCH_PARENT

Anand Phadke
  • 531
  • 3
  • 28
0

In my case, layout.post worked

ExtendedContentLinearLayout.this.post( new Runnable() {
    @Override
    public void run() {
        ExtendedContentLinearLayout.this.addView(llhItem);
    }
});