Dynamically change the layout of Buttons

Asked

Viewed 483 times

3

I have two Buttons on the screen, these are visible or not visible depending on the situation. There is a third situation, which should leave both visible, but does not fit on the screen. So I’d like them to be next to each other, how to do this, in code, dynamically?

Follows xml:

<RelativeLayout
    android:id="@+id/relative_first"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/request_photo"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"
        android:src="@drawable/avatar5"
        android:transitionName="infoboxImage" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/request_photo"
        android:layout_weight="1">

        <ImageView
            android:id="@+id/img_favorite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:src="@drawable/ic_star_border_black_off_48px" />

        <TextView
            android:id="@+id/request_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/img_favorite"
            android:padding="5dp"
            android:text="Abraao Barros Lacerda"
            android:textSize="25sp"
            android:transitionName="infoboxName" />

        <TextView
            android:id="@+id/request_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/request_name"
            android:layout_marginBottom="7dp"
            android:padding="5dp"
            android:text="[email protected]" />

        <ImageView
            android:id="@+id/img_points"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_alignRight="@+id/request_name" />

        <TextView
            android:id="@+id/txt_Points"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/request_name"
            android:text="50.000 pontos" />

        <HorizontalScrollView
            android:id="@+id/horizontal_scroll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/request_email"
            android:layout_marginBottom="5dp">

            <LinearLayout
                android:id="@+id/linear_interests"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/txt_Points"
                android:orientation="horizontal"></LinearLayout>

        </HorizontalScrollView>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/horizontal_scroll">

            <android.support.v7.widget.CardView
                android:id="@+id/card_1"
                android:layout_width="match_parent"
                android:layout_height="105dp"
                android:layout_marginTop="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/textView_first"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.03"
                        android:padding="10dp"
                        android:text="Segunda-feira"
                        android:textSize="14sp" />

                    <CheckBox
                        android:id="@+id/button0"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.03"
                        android:text="Ida - 7:00" />

                    <CheckBox
                        android:id="@+id/button1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.03"
                        android:text="Volta -18:00" />

                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:id="@+id/card_2"
                android:layout_width="match_parent"
                android:layout_height="105dp"
                android:layout_below="@+id/card_1"
                android:layout_marginTop="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/textView_second"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="10dp"
                        android:text="Quarta-Feira" />

                    <CheckBox
                        android:id="@+id/button2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Ida - 7:00" />

                    <CheckBox
                        android:id="@+id/button3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Volta -18:00" />


                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:id="@+id/card_3"
                android:layout_width="match_parent"
                android:layout_height="105dp"
                android:layout_below="@id/card_2"
                android:layout_marginTop="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:id="@+id/check">

                    <TextView
                        android:id="@+id/textView_third"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:padding="10dp"
                        android:text="Quinta-Feira" />

                    <CheckBox
                        android:id="@+id/button4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Ida - 7:00" />

                    <CheckBox
                        android:id="@+id/button5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Volta -18:00" />


                </LinearLayout>
            </android.support.v7.widget.CardView>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_below="@+id/card_3"
                android:id="@+id/button">

                <Button
                    android:id="@+id/send_offer_request"
                    style="@style/appButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_below="@+id/card_3"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:paddingTop="10dp"
                    android:text="Oferecer" />

                <Button
                    android:id="@+id/send_ask_request"
                    style="@style/appButtonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_below="@id/card_3"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:paddingTop="10dp"
                    android:text="Pedir" />
            </LinearLayout>

        </RelativeLayout>


    </RelativeLayout>


</RelativeLayout>

Follow code, the Else, should make both appear on the screen.

    if (user.getProfile() == 0) {
        send_offer_request.setVisibility(View.VISIBLE);
        send_ask_request.setVisibility(View.INVISIBLE);
    }else if(user.getProfile() == 1){
        send_offer_request.setVisibility(View.VISIBLE);
        send_ask_request.setVisibility(View.INVISIBLE);
    }else{
        send_offer_request.setVisibility(View.VISIBLE);
        send_ask_request.setVisibility(View.VISIBLE);
    }
  • They are within a Linearlayout with orientation="horizontal", so they should appear side by side. Try changing , in both, android:layout_width="match_parent" for android:layout_width="wrap_content"

  • @ramaral That’s what I want to do, dynamically. For they must be match_parent, unless they are side by side.

  • If you switch to wrap_content they fit on the screen?

  • @Fit ramaral, also need to space them. but fit

2 answers

2

If I understand correctly what you want is that when only one of the buttons is visible it should occupy the full width of Linearlayout.
So they are declared with android:layout_width="match_parent".

When it is intended that both become visible they overlap, due to the match_parent.

So, to put them side by side, you must change the layout_width of match_parent to wrap_content.

This is possible using the method setLayoutParams():

bt.setLayoutParams(new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

It is nevertheless necessary to preserve the margin values.
Considering he’s the same in both buttons just keep one:

ViewGroup.MarginLayoutParams marginOld = (ViewGroup.MarginLayoutParams)send_offer_request.getLayoutParams();

send_offer_request.setLayoutParams(new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT));
send_ask_request.setLayoutParams(new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT));

ViewGroup.MarginLayoutParams marginNew_offer = (ViewGroup.MarginLayoutParams)send_offer_request.getLayoutParams();
marginNew_offer.topMargin = marginOld.topMargin;

ViewGroup.MarginLayoutParams marginNew_ask = (ViewGroup.MarginLayoutParams)send_ask_request.getLayoutParams();
marginNew_ask.topMargin = marginOld.topMargin;

Note: the attributes android:layout_alignParentBottom, android:layout_below and android:layout_centerHorizontalhave no effect on Linearlayout.

  • had tried earlier that way. and this error appears to me: android.widget.Linearlayout$Layoutparams cannot be cast to android.widget.Relativelayout$Layoutparams

  • even if it is explicit send_ask_request.setLayoutParams(new LinearLayout.LayoutParams(&#xA; LinearLayout.LayoutParams.WRAP_CONTENT,&#xA; LinearLayout.LayoutParams.WRAP_CONTENT)); the error happens

  • I see no reason for this error, the buttons are inside a Linearlayout, unless the xml that posted is not what you are using.

  • This is the XML, I still added another linear layout, over all relatives and put this linear, with the buttons, being the child of this other linear and the error remains.

  • follows my reply, how I managed to solve, still not understanding the error I put in the comment

1


Could only be resolved as follows:

     else {
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.BELOW,R.id.card_3);
                RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                params2.addRule(RelativeLayout.BELOW,R.id.card_3);
                params2.addRule(RelativeLayout.RIGHT_OF,R.id.send_ask_request);

                send_offer_request.setLayoutParams(params2);
                send_ask_request.setLayoutParams(params);
                send_offer_request.setVisibility(View.VISIBLE);
                send_ask_request.setVisibility(View.VISIBLE);
}

I am grateful for the answers that led me to this conclusion.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.