Add a view programmatically in ConstraintLayout - android

I try to add a View at the same place of an other View in a ConstraintLayout but the added View don't get the LayoutParams of the other View.
The added View take place on the top|left of the container.
This is my code :
TextView cloneView = new TextView(getContext());
cloneView.setLayoutParams(otherView.getLayoutParams());
mainContainer.addView(cloneView);

To add views to a ConstraintLayout you have to add the constraints using a ConstraintSet.
View v = findViewById(...);
ConstraintLayout cl = (ConstraintLayout) findViewById(...);
ConstraintSet c = new ConstraintSet();
cl.addView(v);
int id = v.getId();
c.clone(cl);
c.connect(id, ConstraintSet.Top, otherViewIdAboveV, ConstraintSet.BOTTOM, 0);
...
other constraints
...
c.applyTo(cl);

Related

ConstraintLayout and changing contraints programmatically

I am trying to change the constraints programmatically as follows:
final ConstraintSet set = new ConstraintSet();
set.clone(this);
if(text == null) {
view.setVisibility(GONE);
set.clear(div.getId(), ConstraintSet.TOP);
set.connect(div.getId(), ConstraintSet.TOP, otherView.getId(), ConstraintSet.BOTTOM);
}
set.applyTo(this);
But the rule is not applied. The div goes to the top of the layout like there is no top constraint.
What am I doing wrong?
Update
If I change the following:
set.clone(this);
...
set.apply(this);
to:
View root = LayoutInflater.from(getContext()).inflate(R.layout.my_layout, this);
ConstraintLayout layout = root.findViewById(R.id.constraint_layout);
....
set.clone(layout);
....
set.apply(layout);
The layout is correct! But I don't understand why.
this is a ConstraintLayout since I am extending a ConstraintLayout.

Use SnapHelper to Scroll to a Child view in a recyclerview item

In my recyclerview onCreateViewholder method I create a dynamic which would have unspecified number of text views inside a linear layout
if (sessionManager.getBookScrolOrientation() == AppConstents.HORIZONTAL) {
LinearLayout linearLayout = new LinearLayout(context);
ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(layoutParams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
Spannable spannable = getVerseSpanableHorizontal(pidreference.get(position) , cidreference.get(position));
Pagination pagination = new Pagination(spannable,
pageWidth,
pageHight,
textPaint,
lineSpacingMultiplier,
lineSpacingExtra,
isTextPaddingIncluded);
List<CharSequence> list = pagination.getPages();
for (CharSequence charSequence : list){
TextView tv = new TextView(linearLayout.getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Tools.getScreenWidth() , ViewGroup.LayoutParams.MATCH_PARENT);
tv.setLayoutParams(params);
tv.setText(charSequence);
linearLayout.addView(tv);
}
MainViewHolder viewHolder = new MainViewHolder(linearLayout);
return viewHolder;
}
When i use SnapHelper with this recyclerview it scrolls to the center of linear layout not the next textview. How can i make it scroll to the next textview inside the linearlayout?
If I understand correctly, the problem is that SnapHelper snaps the center of the current view to the center of the recycler. As the docs say:
The implementation will snap the center of the target child view to the center of the attached RecyclerView.
If you want the the current view to snap to the start of the recycler use a libraray such as this one. Import it with gradle and activate it like this:
GravitySnapHelper(Gravity.START).attachToRecyclerView(recyclerview);

Adding constraints programmatically

What is the translation to java code of the following XML instructions used in a layout definition with a constraint layout?
app:layout_constraintBottom_toBottomOf="#+id/Button1"
app:layout_constraintTop_toTopOf="#+id/Button1"
app:layout_constraintLeft_toLeftOf="#+id/Button2"
app:layout_constraintRight_toRightOf="Button2"
Here is an example of adding constraints programatically,
ConstraintLayout mConstraintLayout = (ConstraintLayout)fndViewById(R.id.mainConstraint);
ConstraintSet set = new ConstraintSet();
ImageView view = new ImageView(this);
mConstraintLayout.addView(view,0);
set.clone(mConstraintLayout);
set.connect(view.getId(), ConstraintSet.TOP, mConstraintLayout.getId(), ConstraintSet.TOP, 60);
set.applyTo(mConstraintLayout);
To know more details you can refer Constraint layout

How to access parent in ConstraintLayout

I am using ConstraintLayout to create a view programmatically and I want to set my button view topTotop of parent and leftToleft of the parent. How can I do that?
My code is below:
Button button = new Button(MainActivity.this);
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(widthInt, heightInt);
params.topToTop = // I want to set it parent
params.leftToLeft = // I want to set it parent
params.setMargins(marginStartint , marginTopInt , 0 , 0);
button.setLayoutParams(params);
You can use PARENT_ID
so your code becomes
params.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
params.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;
You can use ConstraintSet's Connect method to perform such operations. There PARENT_ID to refer parent id.
ConstraintLayout layout = (ConstraintLayout)fndViewById(R.id.mainConstraint);
ConstraintSet set = new ConstraintSet();
set.clone(layout);
set.connect(view.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
//view refers to the view's constraint to be changed
set.applyTo(layout);
connect
void connect (int startID,
int startSide,
int endID,
int endSide,
int margin)
Create a constraint between two widgets.

Android : How to programmatically set layout_constraintRight_toRightOf "parent"

I have a view in ConstrainLayout as follows.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="260dp"
android:textColor="#FFF"
android:textSize="16sp"
app:layout_constraintLeft_toLeftOf="#+id/parent"
app:layout_constraintTop_toBottomOf="#id/message_date"
android:id="#+id/text_main"
/>
I would like to change the view to either app:layout_constraintLeft_toLeftOf="#+id/parent" or layout_constraintLeft_toRightOf="#+id/parent" programmatically in recycleViewHolder based on some conditions.
Here is an example of setting a button to the bottom of parent view using java code:
ConstraintLayout constraintLayout;
ConstraintSet constraintSet;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
constraintLayout = (ConstraintLayout) findViewById(R.id.activity_main_constraint_layout);
Button button = new Button(this);
button.setText("Hello");
constraintLayout.addView(button);
constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(button.getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.RIGHT, 0);
constraintSet.constrainDefaultHeight(button.getId(), 200);
constraintSet.applyTo(constraintLayout);
}
to achieve something like this,
app:layout_constraintLeft_toLeftOf="#+id/parent"
your java code should look like this
set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0);
and to achieve something like this,
layout_constraintLeft_toRightOf="#+id/parent"
your java code should look like this,
set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);
Here, I'm assuming android:id="#+id/parent" is the id of your parent ConstraintLayout .
constraintSet = new ConstraintSet(); and so on
Did not work for me anyhow.
Solved by
LayoutParams layoutParams = (LayoutParams) viewToChange.getLayoutParams();
layoutParams.leftToLeft = anotherViewId;
layoutParams.rightToRight =anotherViewId;
layoutParams.topToTop = anotherViewId;
layoutParams.bottomToBottom = anotherViewId;
layoutParams.startToStart =anotherViewId;
layoutParams.endToEnd = anotherViewId;
viewToChange.setLayoutParams(layoutParams);
Use id
class MyLayout(context: Context) : ConstraintLayout(context) {
fun show() {
val view = ImageView(context)
addView(view)
val params = view.layoutParams as ConstraintLayout.LayoutParams
params.height = 100
params.width = 50
params.rightToRight = id
view.requestLayout()
}
}
I had same situation when I needed to move buttons down once a new EditText appears, In XML file they had constraints like this:
app:layout_constraintTop_toBottomOf="#+id/first_layout"
and what I needed to do is changing to sth like programmatically:
app:layout_constraintTop_toBottomOf="#+id/second_layout"
Using ConstraintLayout.LayoutParams the task is pretty straightforward e.g.
Kotlin:
val currentLayout = btn.layoutParams as ConstraintLayout.LayoutParams // btn is a View here
currentLayout.topToBottom = R.id.second_layout // resource ID of new parent field
btn.layoutParams = currentLayout
and that's it!
Haven't seen better solution than this:
textMain.updateLayoutParams<ConstraintLayout.LayoutParams> {
startToEnd = anyOtherView.id
topToTop = anyOtherView.id
endToStart = anyOtherView.id
bottomToBottom = anyOtherView.id
}
In Android, you can programmatically set the layout_constraintRight_toRightOf attribute to "parent" using the ConstraintLayout.LayoutParams class. Here's an example:
ConstraintLayout layout = findViewById(R.id.constraint_layout);
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) view.getLayoutParams();
params.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID;
view.setLayoutParams(params);
In the above example, "constraint_layout" is the id of the ConstraintLayout, "view" is the view that you want to set the constraint on and the "params.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID" is used to set the layout_constraintRight_toRightOf attribute to "parent".
This will set the right side of the view to the right side of its parent, aligning it to the right edge of the screen.
You can also use setRightToRight(int rightToRight) method of ConstraintLayout.LayoutParams.
It's also important to note that you should call requestLayout() on the view after modifying its layout parameters, so that the changes will be reflected on the layout.

Categories

Resources