0

I have seen in the following answer: https://stackoverflow.com/a/24035591 of another post that to get the width of an element after the drawing phase I can do the following:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);
    View view1,view2;
    int width1,width2;´


        view1.post(() -> {
          width1=view1.getWidth();
                        }
}

What if I want to get the width of 2 elements (view1 and view2) instead of only one element as I am using both values in the same mathematical formula?

Should I do this? -->

view1.post(() -> {
              width1=view1.getWidth();
              width2=view2.getWidth();
                 formula(width1,width2);
                            }

Or should I do this(I don't know if it is okay to put a runnable inside another runnable) -->

view1.post(() -> {
              width1=view1.getWidth();
              view2.post(() -> {
               width2=view2.getWidth();
               formula(width1,width2);

               }
                            }

I can't do the following as I need both width's to be used in the same formula:

view1.post(() -> {
              width1=view1.getWidth();
                            }
view2.post(() -> {
              width2=view2.getWidth();
                            }

Both of the possibilities are working, but I would like to know which one is better or if there is another option to get both width's as I need them together as I am using them in one mathematical formula.

Note that formula(x,y) will use both width's to setMargins of another element:

TextView text1;
formula(x,y){
int z = x + y;
    ConstraintLayout.LayoutParams params = 
   (ConstraintLayout.LayoutParams) text1.getLayoutParams();
    params.setMargins(0,z,0,0);
    text1.setLayoutParams(params);
}

EDIT:

what I have in OnCreate:

protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);

 /*I set margins of view1 and view2 so that the width of both 
 is changing (this is why if I call getWidth of view1 and view2 in this 
 method, it will take the very first width (the width without the new 
 margins) */
 
    ConstraintLayout.LayoutParams params1 = 
   (ConstraintLayout.LayoutParams) view1.getLayoutParams();
    params1.setMargins(X,0,X,0);
    view1.setLayoutParams(params1);


    ConstraintLayout.LayoutParams params2 = 
   (ConstraintLayout.LayoutParams) view2.getLayoutParams();
    params2.setMargins(X,0,X,0);
    view2.setLayoutParams(params2);

    //X is a non-static value 

 }
sacacorchos
  • 163
  • 9

1 Answers1

0

Please see the activity lifecycle here: https://developer.android.com/guide/components/activities/activity-lifecycle.

The drawing is already over when the onStart() method is called. So you can directly call view1.getWidth() and view2.getWidth() inside this method, without using the post() call.

JollyRoger
  • 737
  • 1
  • 12
  • 38
  • Hi, I have updated the formula(x,y) function as I forgot to say that I am setting margin of a third element after doing a mathematical operation with both width's. I thought that setting some attributes of an element like the margins in this case should be done in the onCreate() method and not in the onStart() method. I have seen that in this post https://stackoverflow.com/questions/3591784/views-getwidth-and-getheight-returns-0/24035591#24035591 none of the answers say to use the onStart() method and I am surprised that it is as simple as that. – sacacorchos Jan 31 '23 at 10:37
  • Then, is it okay to use what you said in your answer although I need both width's to setMargins of another element? – sacacorchos Jan 31 '23 at 10:38
  • So you need to use the width of view1 and view2 to calculate the margin of let's say view3. And I am guessing widths of view1 and 2 are calculated and not static (let me know if this is wrong). `onStart()` is called after everything is rendered on the user's screen. Depending on how and where you set the width1 and width2, you can either do it in the `onCreate()` or in `onStart()`. If you did it using extended style sheets object or just the stylesheet object, then I think you should be able to call `getWidth` and `setWidth` without the `post` inside the `onCreate`, and this way is fine. – JollyRoger Feb 01 '23 at 12:23
  • Is everything alright now? – JollyRoger Feb 02 '23 at 13:43
  • Hi, I have edited the question to let you see how I am changing the width of view1 and view2 in the onCreate() method. If I get width in onCreate() method it returns 0, what it is working is using the post, as I need to get both values `after the new margins are applied`. And after obtaining both width's I need to setMargins of a third element, but as I have used setMargins() for view1 and view2 in onCreate(), if I obtain the width in the onStart() method, I would have to setMargins of third element in onStart() method instead of onCreate() as view1 and view2? is that right? – sacacorchos Feb 02 '23 at 20:19
  • Yes that's right. And I am guessing that will cause a change on the user's UI after the it is loaded, which is not that pretty. – JollyRoger Feb 03 '23 at 21:53