ConstraintLayout setting constraintHeight_default programmatically - java

I have a ConstraintLayout that contains a ViewStub with the app:constraintHeight_default property set to spread in XML. I need to be able to update this property to have a value of wrap at runtime, but so far nothing has worked for me. Here is the layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
>
<ViewStub
android:id="#+id/view_stub"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottom_view"
app:layout_constraintTop_toBottomOf="#+id/top_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="spread"
/>
<com.example.TopView
android:id="#+id/top_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/view_stub"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="wrap"
/>
<com.example.BottomView
android:id="#+id/bottom_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/fields_stub"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_default="wrap"
/>
So far, I've tried using a ConstraintSet to set constraintHeight_default programmatically, but the layout inflated from the ViewStub ends up having 0 width and 0 height, so it isn't rendered in the layout.
ConstraintLayout constraintLayout = root.findViewById(R.id.constraint_layout);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.constrainHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);
constraintSet.applyTo(constraintLayout);
ViewStub viewStub = root.findViewById(R.id.view_stub);
viewStub.setLayoutResource(R.layout.custom_layout);
viewStub.inflate();
How can I set the height of the ViewStub to wrap its height programmatically?

Try changing the following line:
constraintSet.constrainHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);
to constrainDefaultHeight
constraintSet.constrainDefaultHeight(R.id.view_stub, ConstraintSet.MATCH_CONSTRAINT_WRAP);
See the ConstraintSet documentation.
constrainDefaultHeight
void constrainDefaultHeight (int viewId,
int height)
Sets how the height is calculated ether MATCH_CONSTRAINT_WRAP or MATCH_CONSTRAINT_SPREAD. Default is spread.

Related

Padding is not working on child views of popup window android

LayoutInflater layoutinflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.example,null);
final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Here's the child views that I'm trying to set the padding in.
Imageview img = popupView.findViewById(R.id.image);
I tried to set the padding in XML by 10dp in all corners but the image is still the same, I event put it in the java file. img.setPadding(10, 10,10,10); yet still the same imageview.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns: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:id="#+id/rotaset"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/customborder">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/layoutagain"
android:layout_width="145dp"
android:layout_height="163dp"
android:layout_margin="1dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="14dp">
<ImageView
android:id="#+id/example"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:background="#drawable/zonesstyle"
android:scaleType="fitXY"
android:padding="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Simulate" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

How To Fit Number of ImageViews Inside RecyclerView?

I have a RecyclerView with a fixed Dimension-Ratio
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
...>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintDimensionRatio="35:16"
... />
</androidx.constraintlayout.widget.ConstraintLayout>
I Need to show list of ImageView inside the RecyclerView, but with a fixed number of images shown on the screen, and the rest of the images need to scroll to be shown, similar to the image
I Create this layout with ImageView inside it:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/image2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The problem here
I try a lot, but the image doesn't show without setting the layout_width and layout_hieght with a fixed number.
and What i want is to set ImageView hight-width dynamicly with RecyclerView Hight and Screen Width.
The solution is super easy:
pass DisplayMetrics In The Adapter Constructor for getting screen width.
DisplayMetrics displayMetrics;
public SearchSliderOneAdapter(DisplayMetrics displayMetrics) {
this.displayMetrics = displayMetrics;
}
and calculate the width of ImageView = ScreenWidth\numberOfImage inside onCreateViewHolder function of the adapter.
public SearchSliderOneViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_image_slider, parent, false);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
displayMetrics.widthPixels / numberOfImage,
GridLayout.LayoutParams.MATCH_PARENT, 1);
inflate.setLayoutParams(params);
return new SearchSliderOneViewHolder(inflate, listener);
}

Set max height on constraint layout

I have a listview inside constraint layout. When it has more lists, want to restrict the parent layout height. How can I set max layout height programmatically for bottom_layout? Display a maximum of 80% of device height.
This is the layout:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/try_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/primary_button"
android:minWidth="180dp"
android:text="#string/error_try_again"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ListView
android:id="#+id/instruction_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/try_again"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/instruction_subtitle" />
<TextView
android:id="#+id/instruction_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/instruction_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/instruction_title"
app:layout_constraintBottom_toTopOf="#+id/instruction_subtitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Tried this solution on calling onCreate but didn't work:
private void BottomCardHeight(){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float density = metrics.density;
float device_height = metrics.heightPixels / density;
bottom_layout = findViewById(R.id.bottom_layout);
ConstraintSet set = new ConstraintSet();
set.clone(bottom_layout);
set.constrainMaxHeight(R.id.bottom_layout, (int) (device_height * 0.8));
// set.constrainHeight(R.id.bottom_layout, (int) (device_height * 0.8)); both not working
set.applyTo(bottom_layout);
//ConstraintLayout mConstrainLayout = (ConstraintLayout) findViewById(R.id.bottom_layout);
//ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) mConstrainLayout.getLayoutParams();
//lp.matchConstraintMaxHeight = (int) (device_height * 0.8);
//mConstrainLayout.setLayoutParams(lp);
}
You can you do this on layout by wrapping the ConstraintLayout in another one that has a height that matches the screen height.
Then add the below constraints to the inner ConstraintLayout to get a max height of 80% of the total screen height. This requires to have a height that match constraints
android:layout_height="0dp"
app:layout_constraintHeight_max="wrap"
app:layout_constraintHeight_percent="0.8"
So, the outer layouts would be:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="wrap"
app:layout_constraintHeight_percent="0.8"
app:layout_constraintStart_toStartOf="parent">
<!-- other views -->
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

How to correctly resize a View so its bottom is aligned with other View's top in ConstraintLayout programatically?

I have a ScrollView and a FrameLayout inside a ConstraintLayout. When I click a button, I want the ScrollView layout to resize so the bottom of the ScrollView is aligned with the top of the FrameLayout. However, the constraints won't change when the button is clicked.
I've tried to put the ScrollView inside a LinearLayout instead, but that didn't work either.
When button is clicked:
private void fixTableConstraint() {
ConstraintLayout constraintLayout = findViewById(R.id.home_constraint_layout);
View scrollView = findViewById(R.id.homeScreenScroll);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(R.id.homeScreenScroll, ConstraintSet.BOTTOM, R.id.timeMenuOverlay, ConstraintSet.TOP, 0);
constraintSet.applyTo(constraintLayout);
ConstraintLayout.LayoutParams cl = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
scrollView.setLayoutParams(cl);
}
XML layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns: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:id="#+id/home_constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/homeScreenScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.2"
tools:layout_editor_absoluteX="0dp">
<TableLayout
android:id="#+id/homescreenTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</ScrollView>
<FrameLayout
android:id="#+id/timeMenuOverlay"
android:layout_width="match_parent"
android:layout_height="152dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<fragment
android:id="#+id/timeMenuFragment"
android:name="multicus.com.scheduleplannerv2.TimeMenuFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Normal Layout:
https://imgur.com/VsfSgVa
What happens in my case when button is clicked:
https://imgur.com/pXT8v6W
What I actually want:
https://imgur.com/6OML007
Your constraints changes will not work because you have
ScrollView layout_height = "match_parent"
You can do it like this
<ScrollView
android:id="#+id/homeScreenScroll"
android:background="#color/green_dark"
android:layout_width="match_parent"
android:layout_height="0dp" // this is important
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/timeMenuOverlay" // and this
tools:layout_editor_absoluteX="0dp">
<TableLayout
...
/>
</ScrollView>
<FrameLayout
android:id="#+id/timeMenuOverlay"
android:visibility="gone" // !!
android:layout_width="match_parent"
android:layout_height="152dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
Instead of changing your layout constraints, change only the visibility.
timeMenuOverlay visibility => GONE, expand your ScrollView to the bottom
timeMenuOverlay visibility => VISIBLE => expected result

Set PercentRelativeLayout percentage in Java

I want to change the width percentage of items inside a PercentRelativeLayoutdynamically in Java based on some events, I found this, but I cannot find where to put the percentages:
mLinerLayout.setLayoutParams(new PercentRelativeLayout.LayoutParams());
My xml looks something like this:
<android.support.percent.PercentRelativeLayout
android:id="#+id/ad_prl_tags"
android:layout_width="match_parent"
android:layout_height="48dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/ad_rv_tags"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
app:layout_widthPercent="70%" />
<!--Add Tags Button-->
<LinearLayout
android:id="#+id/ad_ll_add_tags_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/ad_rv_tags">
<ImageView
android:id="#+id/itd_animatable_plus_iv"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<EditText
android:id="#+id/ad_et_add_tags_and_info"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.percent.PercentRelativeLayout>
I want to set the width percentage of the LinearLayout to 70% and the width of the RecyclerView to 30% in Java.
You can use it pragmatically with this method :
public static void setWidthPercent(View view, float width) {
PercentLayoutHelper.PercentLayoutParams params =(PercentLayoutHelper.PercentLayoutParams) view.getLayoutParams();
PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
info.widthPercent = width;
}

Categories