0

The fragment.xml contains linearlayout and a button inside a RelativeLayout. I need to resize the height of the linearlayout on button click but it doesn't work (nothing happens). However, setting the text of the textview is working. Any ideas? I'm not sure if this is even possible.

getView() function :

@Override
public View getView(int _position, View _view, ViewGroup _parent) {

View view = _view;

ViewHolder m_viewHolder ;

 if (view == null) {

    LayoutInflater inflater = (LayoutInflater) m_ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     view = inflater.inflate(R.layout.fragment_user, _parent, false);

     m_viewHolder = new ViewHolder();

     m_viewHolder.btn_test = (ImageButton)view.findViewById(R.id.btn_details);

     m_viewHolder.btn_test.setOnClickListener(changeLLHeight);

     view.setTag(m_viewHolder);

} else {
     m_viewHolder = (ViewHolder) view.getTag();
}

m_viewHolder.btn_test.setTag(_position);


return view;
}

OnClickListener():

private View.OnClickListener changeLLHeight = new View.OnClickListener() {
  @Override
  public void onClick(View v) {

    RelativeLayout rl = (RelativeLayout)v.getParent();

    LinearLayout ll = (LinearLayout)rl.getChildAt(1);

    TextView tv = (TextView)rl.getChildAt(2);

    ll.getLayoutParams().height = 20;
    ll.requestLayout();

    tv.setText("Oi!!");

    Toast.makeText(getContext(), "", Toast.LENGTH_SHORT).show();


  }
};
EMP
  • 87
  • 2
  • 11

3 Answers3

1

U can use

LinearLayout ll = (LinearLayout)rl.getChildAt(1);
LayoutParams params = ll.getLayoutParams();

// Changes the height and width to the specified *pixels*
params.height = 100;
params.width = 100;
ll.setLayoutParams(params);
0

Here's what you need:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)
                            ll.getLayoutParams();
params.height = 20;
ll.setLayoutParams(params);
RoyalGriffin
  • 1,987
  • 1
  • 12
  • 25
0

You need to apply the parameter with setLayoutParams

Android: How to Programmatically set the size of a Layout

LayoutParams params = ll.getLayoutParams();
params.height = 20;
ll.setLayoutParams(params);