How to create components dynamically, being created in relation to other already existing components?

0

In a project in android studio, when clicking on one of the 2 buttons in activity (01 and 101) a TextView displays a response corresponding to the choice.

I would like to know how to create a new button just below this second TextView:

Iknowthefollowingreferencescanbemade(positionbeingthepositioningRelativeLayout):

Topleft=>position.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

Topright=>position.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

Leftfooter=>position.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.ALIGN_PARENT_LEFT);

Rightfooter=>position.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.ALIGN_PARENT_RIGHT);

Vertical/horizontalcenter=>position.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.CENTER_HORIZONTAL);

BEYONDOTHER...

IlearnedthisthankstoAckLay'sanswertothisquestion#

But I did not find anything related to the reference to another component present in the activity.

MainActivity:

package genesysgeneration.chloko;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn01, btn101;
    private TextView tv02;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv02=(TextView)findViewById(R.id.tv02);

        btn01=(Button)findViewById(R.id.btn01);
        btn101=(Button)findViewById(R.id.btn101);

        btn01.setOnClickListener(this);
        btn101.setOnClickListener(this);

    }

    public void onClick(View v){

        switch (v.getId()){

            case R.id.btn01:

                RelativeLayout relativeLayout01 = (RelativeLayout)findViewById(R.id.relativeLayout);
                RelativeLayout.LayoutParams layoutParams01 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

                Button btn02 = new Button(this);
                btn02.setText("02");
                relativeLayout01.addView(btn02, layoutParams01);
                RelativeLayout.LayoutParams position01 = (RelativeLayout.LayoutParams) btn02.getLayoutParams();
                //abaixo do TEXTVIEW (tv02)

                break;

            case R.id.btn101:

                RelativeLayout relativeLayout02 = (RelativeLayout)findViewById(R.id.relativeLayout);
                RelativeLayout.LayoutParams layoutParams02 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

                Button btn102 = new Button(this);
                btn102.setText("102");
                relativeLayout02.addView(btn102, layoutParams02);
                RelativeLayout.LayoutParams position02 = (RelativeLayout.LayoutParams) btn102.getLayoutParams();
                //abaixo do TEXTVIEW (tv02)

                break;

        }

    }

}

One thing that caught my eye was that in the statement of RelativeLayout.LayoutParams layoutParams01 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); I was not able to put the actual size of the height of my relativelayout which in this case is 3000dp , I had to put WRAP_CONTENT .

I would like to know if there might be a problem if I have to create one more of these buttons and it is much lower in the activity.

    
asked by anonymous 29.03.2017 / 01:43

1 answer

0

The solution I found was as follows:

In the image below you can see that there is a button 50x50 (dp) at the top and left in the activity layout. Below it is another button with the same dimensions:

Ifyouputthecodebtn01.setVisibility(View.INVISIBLE);itwillnolongerappear,butthespaceitoccupieswillcontinuetobedemarcated.Itisasifhewerephysicallythere,butcouldnotbeseenorsufferedanykindofinteraction.

Nowifyouputthecodebtn01.setVisibility(View.GONE);thesamewilldisappearandthespacethatoccupiedwhileactivewillbecompensated.Allofthecomponentsintheactivitythathavesomepositioningrelationshipwillbereallocated,intheexamplecase,btn02willgoup50dpvertically.

Belowisanimagethatexplainshowtorelocatethepositionsofthecomponentswhentheyhavesomereferencetotheobjectthatwilldisappear.

Well for what I needed, it served perfectly. At the beginning of the activity I would move all components that I did not want to appear with View.GONE and then make them reappear with View.VISIBLE .

With the instructions in this answer, you will not exactly create a component after a given action, but make it appear and become interactive. It's exactly the same ( at least for my case ), because it will not be taking up any space in the activity and will not be seen by the user.

    
08.04.2017 / 23:57