Change the layout of buttons dynamically

3

I have two buttons on the screen, these are visible or not depending on the situation. There is a third situation, which should make the two visible, but it does not fit the screen. So I would like them to stay side by side, how to do this, in code, dynamically?

Follow 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>

Following 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);
    }
    
asked by anonymous 23.01.2017 / 20:40

2 answers

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 appreciate the answers that led me to this conclusion.

    
24.01.2017 / 13:03
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 the LinearLayout. So they are declared with android:layout_width="match_parent" .

When you want both to be visible they overlap, due to match_parent .

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

This is possible using the setLayoutParams() method:

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

It is, however, necessary to preserve margin values.
Given that it is the same on both buttons just save 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_centerHorizontal have no effect on a LinearLayout.

    
23.01.2017 / 23:01