Question

I am using view stub in my application. I am inflating the view stub to the layout and it is working fine. Now if you see the dummy_layout.xml there I have given the android:layout_height=20dp. What I need to do is to change that view stub height from 20 dp to Wrap_Content on a button click. How can I achieve this?

My ultimate goal is this. I have a button in it. when i click on it for first time I need to show the full layout of stub. and again when I click on it for the second time I need to animate the stub to slide down and show only a part of it.

dummy_layout.xml

    <RelativeLayout
        android:id="@+id/topLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/ic_watch_pic" />

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:background="@color/dark_grey"
                android:text="Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:text="Button" />

    </RelativeLayout>

    <ViewStub
        android:id="@+id/viewStub1"
        android:layout="@layout/stub_layout"
        android:layout_width="wrap_content"
        **android:layout_height="20dp"**
        android:layout_alignParentBottom="true"/>


</RelativeLayout>

And here is my class:

public class DummyFragment extends Fragment {

    private View mRootView;
    private Context mContext;
    private Button mButton;
    private boolean isVisible = true;
    private RelativeLayout mParentLayout;

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        mRootView = inflater.inflate(R.layout.dummy_layout,
                container, false);

        mContext = getActivity();

        mButton = (Button) mTopLayout.findViewById(R.id.button1);

        final ViewStub stub = (ViewStub) mRootView.findViewById(R.id.viewStub1);

            **final View inflated = stub.inflate();**

        inflated.setVisibility(View.VISIBLE);

        final View viewstublayout = mRootView.findViewById(R.id.viewStub1);

        mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if(isVisible)
                {
                    isVisible=false;    
                    inflated.setVisibility(View.VISIBLE);
                }
                else
                {
                    isVisible=true;                     
                    inflated.setVisibility(View.INVISIBLE);
                }
            }
        });

        return mRootView;
    }

}
Was it helpful?

Solution

You can change the width/height of a view by editing its LayoutParams. Example:

View stub = findViewById(R.id.viewStub1);
ViewGroup.LayoutParams lp = stub.getLayoutParams();
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
stub.setLayoutParams(lp);

The better question is why you want to do this? Maybe there's a better way of achieving your goal.

OTHER TIPS

Try this

    final ViewStub stub = (ViewStub) mRootView.findViewById(R.id.viewStub1);
    View view = stub .inflate();

inside button click

        LayoutParams params = (LayoutParams) view.getLayoutParams();
        params.width = LayoutParams.WRAP_CONTENT;
        view.setLayoutParams(params);

Try this and let me know if works well.

LayoutParams lp = (LayoutParams) (YourView).getLayoutParams();

 lp.height = newheight;
 YourView.setLayoutParams(lp);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top