How To Programmatically Add Constraint Layouts To A Scrollview?
I have the following in mind: Create a new Activity which contains somewhere on the layout a scrollview Create a ConstraintLayout (width is on match-parent) with one edit-field an
Solution 1:
Please try below code, i have added two button
, you can replace button
with textview
or edittext
or any other controls.
Suggestion :- If you use any ui controls inside ConstraintLayout programmatically then please define its id.
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ConstraintLayoutconstraintLayout= findViewById(R.id.constraintlayout);
// Create btn_contact_us1Buttonbtn_contact_us1=newButton(this);
// Generate an Id and assign it to programmatically created Button
btn_contact_us1.setId(View.generateViewId());
btn_contact_us1.setText("Contact Us 1");
btn_contact_us1.setLayoutParams(newConstraintLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// Add programmatically created Button to ConstraintLayout
constraintLayout.addView(btn_contact_us1);
// Create btn_contact_us2Buttonbtn_contact_us2=newButton(this);
// Generate an Id and assign it to programmatically created Button
btn_contact_us2.setId(View.generateViewId());
btn_contact_us2.setText("Contact Us 2");
btn_contact_us2.setLayoutParams(newConstraintLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// Add programmatically created Button to ConstraintLayout
constraintLayout.addView(btn_contact_us2);
// Create ConstraintSetConstraintSetconstraintSet=newConstraintSet();
// Make sure all previous Constraints from ConstraintLayout are not lost
constraintSet.clone(constraintLayout);
// Create Rule that states that the START of btn_contact_us1 will be positioned at the END of btn_contact_us2
constraintSet.connect(btn_contact_us2.getId(), ConstraintSet.START, btn_contact_us1.getId(), ConstraintSet.END);
constraintSet.applyTo(constraintLayout);
}
and the layout code is as below
<?xml version="1.0" encoding="utf-8"?><ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:fillViewport="true"><android.support.constraint.ConstraintLayoutandroid:id="@+id/constraintlayout"android:layout_width="match_parent"android:layout_height="match_parent"/></ScrollView>
Output:-check link image
Solution 2:
<?xml version="1.0" encoding="utf-8"?><ScrollViewandroid:layout_height="match_parent"android:layout_width="match_parent"xmlns:android="http://schemas.android.com/apk/res/android"><android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><EditTextandroid:layout_width="match_parent"android:layout_height="match_parent" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"
/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/B1"/></android.support.constraint.ConstraintLayout></ScrollView>
Post a Comment for "How To Programmatically Add Constraint Layouts To A Scrollview?"