Search code examples
androidtextviewmoveandroid-constraintlayout

reposition textview in constraint layout dynamically


I'm building an android app that does 3 variations of a similar calculation. I have an imageview, three radiobuttons, and four textviews inside the imageview that display inputs/results. I have editext for actual input and a button to do the calculation. That is not my problem. I can load the appropriate image by checking appropriate radiobutton As each image is slightly different I'd like to reposition the the textview with each image so they line up with the image better. During layout design I loaded the appropriate, positioned the textviews where I want them and copied down the Horizontal and Vertical bias numbers. Can't figure out how to change the bias to move the textview when I change the image. I'm using constraint layout. Thanks Steve


Solution

  • The ConstraintLayout.LayoutParams class has two fields: horizontalBias and verticalBias. You can therefore update the bias values for any view that is a child of a ConstraintLayout like this:

    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) view.getLayoutParams();
    params.verticalBias = 0.3f;
    view.setLayoutParams(params);