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

Asked

Viewed 164 times

0

In an android studio project, when clicking on one of the 2 existing Textview on Activity (01 and 101) a Textview displays a corresponding answer to the choice.

I would like to know how to make a new button to be created just below that second Textview (answer):

inserir a descrição da imagem aqui

I know that the following references can be made (being position o Relativepositioning layout):

Top left => position.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

Top right => position.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

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

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

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

OTHER THAN OTHERS...

I learned that thanks to Ack Lay’s answer to that question How to create a textView by code in a specific Activity location?

However I did not find anything related to the reference to another company present in 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 struck me was that in the statement of RelativeLayout.LayoutParams layoutParams01 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); I could not put the real dimension of the height of my relativelayout which in the case is 3000dp, I had to put in WRAP_CONTENT.

inserir a descrição da imagem aqui

I wonder if there may be any problem if I have to create one more of these Buttons and the same is much further down on Activity.

1 answer

0


The solution I found was the following:

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

inserir a descrição da imagem aqui

If you put the code btn01.setVisibility(View.INVISIBLE); the same will no longer appear, but the space it occupies will continue to be demarcated. It’s like he’s physically there, but he can’t be seen or experienced any kind of interaction.

inserir a descrição da imagem aqui

Now if you put the code btn01.setVisibility(View.GONE); The same will disappear and the space that occupied while active will be compensated. All components present in Activity that have some positioning relation it will be reallocated, in the case of the example, the btn02 will rise 50dp vertically.

inserir a descrição da imagem aqui

Below an image that explains how well the reallocation of the positions of the components happens when they have some reference to the object that will disappear.

inserir a descrição da imagem aqui

Well for what I needed, it served perfectly. At the beginning of Activity I set all the components I didn’t want to appear with the View.GONE and then made them reappear with View.VISIBLE.

With the instructions of this answer, you won’t exactly create a component after a certain action, but make it appear and become interactive. It’s exactly the same (at least for my case), because it will not be occupying any space in Activity and will not be being seen by the user.

Browser other questions tagged

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