I have two LinearLayout in which there are several textView, the first field has a certain size. Is it possible to fill the first field by filling in the textView in the second field?

UPD: Based on the proposed option, implemented this method:

public void changeParent(TextView tv){ FrameLayout flCoverBook = findViewById(R.id.flCoverBook); LinearLayout llTextView1 = findViewById(R.id.llTextView1); if (llTextView1.getHeight() <= flCoverBook.getHeight()) { ((LinearLayout) tv.getParent()).removeView(tv); ((LinearLayout) findViewById(R.id.llTextView1)).addView(tv); contentFrameLayout.invalidate(); } } 

But when used, it didn’t work so well, since after adding a textView to the field (layout), the field size remained the same, tried to update it with different methods:

 llTextView1.removeAllViews(); llTextView1.requestLayout(); llTextView1.invalidate(); contentFrameLayout.invalidate()l; lTextView1.refreshDrawableState(); 

Even resorted to quite specific:

 lTextView1.setVisibility(View.GONE); lTextView1.setVisibility(View.VISIBLE); lTextView1.removeAllViews(); lTextView1.refreshDrawableState(); 

etc.

One real solution found the option of getting the size of the field, then with each added textView add its size (height). But this approach does not take into account the number of lines occupied by the textView and this is basically a crutch than a real implementation.

The fields are filled by callback and the screen is updated by the method:

 contentFrameLayout.invalidate(); 

After all these manipulations, I got the impression that you can work with elements on the screen only after its filling and visualization, and not at the time of filling.

  • the first field of what? Explain what you mean. - Jarvis_J
  • count the number of characters and, when a certain number is reached, carry out manipulations - Pollux
  • one
    You need this lib: github.com/google/flexbox-layout - JuriySPb
  • @Jarvis_J, under the word field, I meant LinearLayout. I have a layout on it is a picture, next to it is a LinearLayout which contains a textView. These textViews can be deleted, depending on the availability of this or that information. The amount of information in the textView "conditionally" may not be limited, respectively, when they occupy a height more than the picture, it turns out not beautifully. So I want to add below another LinearLayout, in which to transfer the textView. And it makes sense to implement other fields only if I am completely template. - Valeriy

1 answer 1

If I understand your question correctly: write a function that will be called every time you change the text in TextView :

 void changeParent(TextView tv){ if (tv.getText().length()>limit) { //или if (tv.getHeight()>img.getHeight()) { ((LinearLayout) tv.getParent()).removeView(tv); ((LinearLayout) findViewById(R.id.secondLL)).addView(tv); } } 

And transfer your TextView .

((LinearLayout) tv.getParent()) and ((LinearLayout) findViewById(R.id.secondLL)) can be defined in variables in advance.

Or, write custom TextView , in which on setText(...) this function will be called through the interface .

  • Hello! I used your solution, slightly changing it to fit my conditions, where it turned out: changeParent (TextView tv) {FrameLayout flCoverBook = findViewById (R.id.flCoverBook); LinearLayout llTextView1 = findViewById (R.id.llTextView1); if (llTextView1.getHeight () <= flCoverBook.getHeight ()) {((LinearLayout) tv.getParent ()). removeView (tv); ((LinearLayout) findViewById (R.id.llTextView1)). AddView (tv); contentFrameLayout.invalidate (); }} and another problem got out, the size of the fields is always the same. How can you solve? - Valeriy
  • size is height? do and look in the logs, it is not 0? - Jarvis_J
  • no, it is not zero, the size (height) of the image (field containing) is 270px, the height of the field with textView 171px, when new textView is added to it, its size is not changed. The height of this field is set to "wrap_content". I tried after each addition to update the "root" field, the field containing the textView and them together, using different methods: llTextView1.removeAllViews (); llTextView1.requestLayout (); llTextView1.invalidate (); contentFrameLayout.invalidate () l; lTextView1.refreshDrawableState () to no avail. It is necessary to clarify that the fields are filled in by CallBack and the screen is updated: contFrameLayout.invalidate (); - Valeriy
  • The only working solution that at the moment I managed to get is the height of the field, and each time I add a textView, I add the size of the textView to the size of the field. But this method looks more like crutches and there is another problem, with such an implementation, the height of the textView with its final contents is not taken into account. - Valeriy
  • If you have not received an answer to your question, it is better to add the code to the question with a label, for example, "upd:". Firstly, it is difficult to read it here, secondly, someone else will be able to understand what the problem is and give a solution, thirdly, if someone has a similar question, it will be easier for him to find the answer. - Jarvis_J 1:16 pm