Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing/Adding constraint programmatically in ConstraintLayout

I want to remove and add constraint programmatically based on some condition. Here are the screenshots:

The button "create ad" has a top constraint

and I want to remove it like this but in code:

here is button with removed constraint on top

so the same effect in want to achieve programmatically

and here is the code that I tried:

    if (advertisements.size() > 0) { //my own condition         ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btnCreateAd.getLayoutParams();         layoutParams.topToBottom = R.id.imvEmpty; //the imageview that is in center of the view         btnCreateAd.setLayoutParams(layoutParams);         recyclerView.setVisibility(View.VISIBLE);         txvMyAdEmptyText.setVisibility(View.GONE);         imvEmpty.setVisibility(View.GONE);         adapter.setList(advertisements);         adapter.notifyDataSetChanged();     } else {         ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) btnCreateAd.getLayoutParams();         layoutParams.topToBottom = -1; //here i am trying to remove top constraint but it doesn't work         btnCreateAd.setLayoutParams(layoutParams);          recyclerView.setVisibility(View.GONE);         txvMyAdEmptyText.setVisibility(View.VISIBLE);         imvEmpty.setVisibility(View.VISIBLE);         adapter.setList(new ArrayList<Advertisement>());     }     mConstraintView.invalidate(); //this is my constraint view 

EDIT

I have tried using ConstraintSet also, but the result was even different somehow my RecyclerView (which is set to boundaries of parent view) was disappearing

 ConstraintSet set = new ConstraintSet();     set.clone(parentView);      if (advertisements.size() > 0) {          recyclerView.setVisibility(View.VISIBLE);         txvMyAdEmptyText.setVisibility(View.GONE);         imvEmpty.setVisibility(View.GONE);         adapter.setList(advertisements);         adapter.notifyDataSetChanged();      } else {          set.connect(btnCreateAd.getId(), ConstraintSet.TOP, imvEmpty.getId(), ConstraintSet.BOTTOM, 0);          recyclerView.setVisibility(View.GONE);         txvMyAdEmptyText.setVisibility(View.VISIBLE);         imvEmpty.setVisibility(View.VISIBLE);         adapter.setList(new ArrayList<Advertisement>());     }     set.connect(btnCreateAd.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 0);     set.connect(btnCreateAd.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 0);     set.connect(btnCreateAd.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);      set.applyTo(parentView); 
like image 595
Damir Mailybayev Avatar asked Jun 21 '17 20:06

Damir Mailybayev


People also ask

How do I get rid of ConstraintLayout?

Is it possible to get rid of this constraint layout? Go to the Text mode for the layout, select the ConstraintLayout root and wipe it.

Can we use LinearLayout in ConstraintLayout?

Most of what can be achieved in LinearLayout and RelativeLayout can be done in ConstraintLayout.

How do I change ConstraintLayout?

Open the layout file (activity_main. xml) in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click LinearLayout and then choose Convert layout to ConstraintLayout from the context menu.

Is ConstraintLayout a ViewGroup?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).


2 Answers

I have not worked through your code, but the following illustrates how to break and make the constraint using ConstraintSet.

ConstraintSet set = new ConstraintSet(); ConstraintLayout layout;  layout = (ConstraintLayout) findViewById(R.id.layout); set.clone(layout); // The following breaks the connection. set.clear(R.id.bottomText, ConstraintSet.TOP); // Comment out line above and uncomment line below to make the connection. // set.connect(R.id.bottomText, ConstraintSet.TOP, R.id.imageView, ConstraintSet.BOTTOM, 0); set.applyTo(layout); 
like image 59
Cheticamp Avatar answered Sep 17 '22 14:09

Cheticamp


Another way of doing this is with ConstraintLayout.LayoutParams.UNSET. This way is not using ConstraintSet.clear.

Assuming we want to remove the bottom constraint of our constraintLayout itself but can be any view:

val containerParams = cl_container.layoutParams as ConstraintLayout.LayoutParams containerParams.bottomToBottom = ConstraintLayout.LayoutParams.UNSET cl_container.layoutParams = containerParams 
like image 40
j2emanue Avatar answered Sep 17 '22 14:09

j2emanue