I am trying to align an image view to centre of constraint layout. I am writing below code but its not working.
Code
int size = 150;
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(size, size);
layoutParams.bottomToBottom = ConstraintSet.PARENT_ID;
layoutParams.endToEnd = ConstraintSet.PARENT_ID;
layoutParams.startToStart = ConstraintSet.PARENT_ID;
layoutParams.topToTop = ConstraintSet.PARENT_ID;
circleImageView.setLayoutParams(layoutParams);
ConstraintLayout cLayout = findViewById(R.id.main_layout);
TextView ev = new TextView(getApplicationContext());
ev.setText(getString(R.string.not_found_name));
ev.setTextSize(50);
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams( ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT);
layoutParams.bottomToBottom = ConstraintSet.PARENT_ID;
layoutParams.endToEnd = ConstraintSet.PARENT_ID;
layoutParams.startToStart = ConstraintSet.PARENT_ID;
layoutParams.topToTop = ConstraintSet.PARENT_ID;
ev.setLayoutParams(layoutParams);
cLayout.addView(ev);
Try this, with the use of ConstraintSet you can easily render view programatically into center in ConstraintLayout.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
imageView.setId(R.id.imageview);
ConstraintLayout myLayout = new ConstraintLayout(this);
myLayout.setBackgroundColor(Color.BLUE);
myLayout.addView(imageView);
setContentView(myLayout);
ConstraintSet set = new ConstraintSet();
set.constrainHeight(imageView.getId(),
ConstraintSet.WRAP_CONTENT);
set.constrainWidth(imageView.getId(),
ConstraintSet.WRAP_CONTENT);
set.connect(imageView.getId(), ConstraintSet.LEFT,
ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);
set.connect(imageView.getId(), ConstraintSet.RIGHT,
ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
set.connect(imageView.getId(), ConstraintSet.TOP,
ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
set.connect(imageView.getId(), ConstraintSet.BOTTOM,
ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
set.applyTo(myLayout);
}
in your code:
ConstraintSet.PARENT_ID
is not the real ID of parent constraint layout. You need to change it to:
R.id.myLayout
in which myLayout is what you type in the *.xml file:
android:id="#+id/myLayout"
this should work:
int size = 150;
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(size, size);
layoutParams.topToTop = R.id.myLayout;
layoutParams.bottomToBottom = R.id.myLayout;
layoutParams.leftToLeft = R.id.myLayout;
layoutParams.rightToRight = R.id.myLayout;
layoutParams.verticalBias = (float) 0.95;
layoutParams.horizontalBias = (float) 0.5;
circleImageView.setLayoutParams(layoutParams);
Related
Now,here is my question.
how can I use ConstraintSet's connect to set attr like 'app:layout_constraintBottom_toBottomOf="parent"'
Try this
TextView textView = new TextView(this);
textView.setId(R.id.text_id);
ConstraintSet set = new ConstraintSet();
set.clone(constraintLayout);
set.connect(textView.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
constraintLayout.addView(textView);
set.applyTo(constraintLayout);
I'm creating TextViews programmatically and adding them to my constraint layout. They are by default placed in the top left corner. I want them to go below each other, the first one being below an EditText.
This is my code:
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.cLayout);
ConstraintSet set = new ConstraintSet();
TextView tv = new TextView(this);
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT, // Width of TextView
ConstraintLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
set.clone(layout);
set.connect(tv.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 8);
set.connect(tv.getId(), ConstraintSet.TOP, editText.getId(), ConstraintSet.BOTTOM, 8);
tv.setId(View.generateViewId());
set.applyTo(layout);
layout.addView(tv,0);
As you see, I've already set a constraint from TOP of View to BOTTOM of editText. Still, it shows in the top left corner. Why is that?
Your code order isn't proper, the following code should work.
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.cLayout);
TextView tv = new TextView(this);
tv.setText("Something");
tv.setId(View.generateViewId());
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT, // Width of TextView
ConstraintLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
layout.addView(tv,0);
ConstraintSet set = new ConstraintSet();
set.clone(layout);
set.connect(tv.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 8);
set.connect(tv.getId(), ConstraintSet.TOP, editText.getId(), ConstraintSet.BOTTOM, 8);
set.applyTo(layout);
P.S - I agree with Syed Ahmed Jamil that you should preferably use a RecyclerView or ListView, but it doesn't hurt to know how to set ConstraintLayout constraints properly.
I have a custom ConstraintLayout, and there's a TextView inside of it. I'm doing everything programmatically WITHOUT XML at all, here's the code:
public class CustomView extends ConstraintLayout {
private TextView textView;
public CustomView(Context context) {
super(context);
textView = new TextView(context);
textView.setId(View.generateViewId());
textView.setText("...");
// Adding border to the view in order to visualize the frame
GradientDrawable border = new GradientDrawable();
border.setColor(Color.TRANSPARENT);
border.setStroke(1, Color.BLACK);
textView.setBackground(border);
addView(textView);
// Apply constraints to textView. Left = right = top = bottom = 120
ConstraintSet set = new ConstraintSet();
set.clone(this);
set.connect(textView.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 120);
set.connect(textView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 120);
set.connect(textView.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 120);
set.connect(textView.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 120);
set.applyTo(this);
}
}
When I run the above code, I'm getting some wrong results:
If the text in textView is short:
If the text in textView is long:
If I understand correct that your wrong result - you don't see the margins?
I think this because defaults layout_width layout_height of your textview - wrap_content. Defaults must be 0dp.
Try like:
LayoutParams lp = new LayoutParams(0, 0);
textView.setLayoutParams(lp);
I've added 2 textviews in ConstrainLayout in code, but i can't see them.
The screen is empty.
Please can anybody help me to figure out this?
Thanks a lot!
public class ActivitySixth extends AppCompatActivity {
private TextView tv1;
private TextView tv3;
private ConstraintLayout mConstraintLayout;
private static final int ID_CONS_LAY = 160;
private static final int ID_TV1 = 160;
private static final int ID_TV3 = 160;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sixth);
mConstraintLayout = findViewById(R.id.parent_Constraint);
ConstraintSet set = new ConstraintSet();
set.clone(mConstraintLayout);
tv1 = new TextView(this);
tv3 = new TextView(this);
tv1.setText("TextView number ONE");
tv3.setText("TextView number THREE");
tv1.setId(ID_TV1);
tv3.setId(ID_TV3);
mConstraintLayout.addView(tv1);
mConstraintLayout.addView(tv3);
set.connect(tv1.getId(), ConstraintSet.LEFT, R.id.parent_Constraint, ConstraintSet.LEFT, 0);
set.connect(tv3.getId(), ConstraintSet.RIGHT, R.id.parent_Constraint, ConstraintSet.RIGHT, 0);
set.applyTo(mConstraintLayout);
}
}
First, the IDs should be different, second, you need to add constraints to the TextViews' width/height:
set.connect(tv1.getId(), ConstraintSet.LEFT, R.id.parent_Constraint, ConstraintSet.LEFT, 0);
set.constrainWidth(tv1.getId(), WRAP_CONTENT);
set.constrainHeight(tv1.getId(), WRAP_CONTENT);
set.connect(tv3.getId(), ConstraintSet.RIGHT, R.id.parent_Constraint, ConstraintSet.RIGHT, 0);
set.constrainWidth(tv3.getId(), WRAP_CONTENT);
set.constrainHeight(tv3.getId(), WRAP_CONTENT);
set.applyTo(mConstraintLayout);
By the way, to avoid conflicts, you can define ids using resources xml.
create ids.xml inside your res\values folder
define ids:
<resources>
<item type="id" name="textView1" />
</resources>
use the defined id:
R.id.textView1
Hello Please follow below code
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.mainConstraint);
ConstraintSet set = new ConstraintSet();
ImageView view = new ImageView(this);
layout.addView(view, 0);
set.clone(layout);
set.connect(view.getId(), ConstraintSet.TOP, layout.getId(), ConstraintSet.TOP, 60);
set.applyTo(layout);
TextView contactUs = new TextView(this);
contactUs.setText(R.string.contactus);
contactUs.setTextSize(25);
contactUs.setId(R.id.contactUs);
ConstraintLayout.LayoutParams clpcontactUs = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
contactUs.setLayoutParams(clpcontactUs);
constraintLayout.addView(contactUs);
ConstraintSet set = new ConstraintSet();
set.clone(mConstraintLayout);
set.connect(tv1.getId(), ConstraintSet.LEFT, R.id.parent_Constraint, ConstraintSet.LEFT, 0);
set.connect(tv3.getId(), ConstraintSet.RIGHT, R.id.parent_Constraint, ConstraintSet.RIGHT, 0);
set.applyTo(mConstraintLayout);
try something like this..
ref. http://www.zoftino.com/adding-views-&-constraints-to-android-constraint-layout-programmatically
In an android application I am attempting to programmatically add customized ConstraintLayout views to a vertically oriented LinearLayout.
I set the LayoutParams to MATCH_PARENT for width and WRAP_CONTENT for height in the ConstraintLayouts. However, when I run the application the ConstraintView is all scrunched up and the content is overlapping. Below are some relevant snippets and a screenshot of my application. How do I go about correcting this issue?
public class ItemView extends ConstraintLayout {
LinearLayout linearButtons;
LinearLayout linearText;
public ItemView(Context context, String name, String price, ArrayList<String> guests,
ArrayList<String> checked, int id) {
...
addView(linearText);
addView(linearButtons);
set.clone(this);
set.connect(linearText.getId(), ConstraintSet.LEFT, this.getId(),
ConstraintSet.LEFT, 8);
set.connect(linearText.getId(), ConstraintSet.TOP, this.getId(),
ConstraintSet.TOP, 8);
set.connect(linearButtons.getId(), ConstraintSet.RIGHT, this.getId(),
ConstraintSet.RIGHT, 8);
set.connect(linearButtons.getId(), ConstraintSet.TOP, this.getId(),
ConstraintSet.TOP, 8);
}
elsewhere:
for (Item it:r.getItems()) {
ItemView itemView = new ItemView(this, it.getName(), nf.format(it.getPrice()), dinerlist, it.getGuests(), i);
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT);
itemView.setLayoutParams(params);
vg.addV[enter image description here][1]iew(itemView);
Log.d("ItemView Children: ", itemView.getWidth()+" "+itemView.getHeight());
In ConstraintLayout in xml when you want a "MATCH_PARENT" width, you have to set the width to 0dp and then set the layout_constraintWidth_default attribute to "spread"
Programmatically you can do the same:
a) set a 0 dp width and height
b) set defaultWidth and defaultHeight constraints
//add the view with 0dp width and height
val layoutParams = ConstraintLayout.LayoutParams(0, 0)
val view = View(context)
view.layoutParams = layoutParams
view.id = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) View.generateViewId() else R.id.yourLayoutId
parent.addView(view)
//apply the default width and height constraints in code
val constraints = ConstraintSet()
constraints.clone(parent)
constraints.constrainDefaultHeight(view.id, ConstraintSet.MATCH_CONSTRAINT_SPREAD)
constraints.constrainDefaultWidth(view.id, ConstraintSet.MATCH_CONSTRAINT_SPREAD)
constraints.applyTo(parent)
If you need Java:
//add the view with 0dp width and height
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(0, 0);
View view = View(context);
view.setLayoutParams(layoutParams);
view.setId(R.id.yourLayoutId);
parent.addView(view);
//apply the default width and height constraints in code
ConstraintSet constraints = new ConstraintSet();
constraints.clone(parent);
constraints.constrainDefaultHeight(view.getId(), ConstraintSet.MATCH_CONSTRAINT_SPREAD);
constraints.constrainDefaultWidth(view.getId(), ConstraintSet.MATCH_CONSTRAINT_SPREAD);
constraints.applyTo(parent);