2

I tried to research, but I can't make heads or tails because I'm so new to this.

I'm using Constrained layout. I have a button, button1. button1 has a bottom margin of 10.

Is there an easy way to make it so when I click my button, it changes bottom margin to 20?

This is my simple code:

button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (butt1){
                button1.setBackgroundResource(buttonpress2);
                butt1 = false;
            }else{
                button1.setBackgroundResource(buttonpress);
                butt1 = true;
            }
        }
    });

Also:

    <Button
    android:id="@+id/button1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/buttonpress"
    android:text="@string/button1Text"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="38dp" />
IrishWolf
  • 43
  • 4

2 Answers2

0

Try to use ConstraintLayout.LayoutParams as mentioned here in developer guide https://developer.android.com/reference/android/support/constraint/ConstraintLayout.LayoutParams.html

You can do something like this:

ConstraintLayout.LayoutParams params = new  ConstraintLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
button1.setLayoutParams(params);
Kartik Shandilya
  • 3,796
  • 5
  • 24
  • 42
  • When I tried this, it changed the shape of my button, moved it to the top right and I don't see how I can adjust it to make it work.... – IrishWolf Jul 14 '17 at 19:28
  • Thanks for helping out! I really appreciate it, even if I can't understand it. – IrishWolf Jul 14 '17 at 20:01
0

Rather then creating a new layout param object, instantiate it with the one that the view already has... That way it will retain all the properties you have set in xml and will update the once you change here.

Do this....

int marginInPixels = dpToPx(10);

public int dpToPx(int dp) {
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));     
}
ConstraintLayout.LayoutParams params = 
(ConstraintLayout.LayoutParams)button1.getLayoutParams();

params.topMargin = marginInPixels; 
button1.setLayoutParams(params);
Ravinder Bhandari
  • 2,546
  • 21
  • 25
  • This seems to be something I can figure out... is there a way to only change one Margin...? this way seems to put them all to 0. – IrishWolf Jul 14 '17 at 19:52
  • yes that can be done, if you want to change top margin use this: params.topMargin = 10 or for left margin use this params.leftMargin = 12. – Ravinder Bhandari Jul 14 '17 at 19:55
  • THANKS! Not sure if this is going to be an elegant solution, but it's all I can handle right now! THANK YOU so much for helping out! – IrishWolf Jul 14 '17 at 20:01
  • Glad this helped you, a small suggestion, you should not be setting the margin values in pixel, as it will not fit properly on different screen sizes, rather use this link "https://stackoverflow.com/questions/8309354/formula-px-to-dp-dp-to-px-android" to know how to convert dp value to pixel and then apply it. – Ravinder Bhandari Jul 14 '17 at 20:05
  • One last question if you're still around... I've got it so it changes... but it seems that my original margin was set to 10dp, and the new '20' doesn't seem to be in dp.... thoughts? Answered before I asked! – IrishWolf Jul 14 '17 at 20:06
  • Please accept the answer and upvote so that others can be benefited from this. – Ravinder Bhandari Jul 14 '17 at 20:06
  • I tried to up it, but it said I can't yet. This is my first post. =( – IrishWolf Jul 14 '17 at 20:07
  • I don't understand why or anything.. but I get problems when using the code for dpToPx..... DisplayMetrics imports (alt-enter), but then getContext() turns red, .getResources turns red.... and it all falls apart. – IrishWolf Jul 14 '17 at 20:21
  • If writing this code inside fragment use getActivity() instead of getContext(), if writing it inside an activity use MyActivity.this.. like this getActivity().getResources(). – Ravinder Bhandari Jul 14 '17 at 20:24
  • Hey, I think you can still accept the answer even if your repo is not above 15. Please accept this answer so that others can be benefited. Thanks – Ravinder Bhandari Jul 17 '17 at 08:07