Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a maximum width for a view in an android constraintLayout?

I have a layout that looks like this:

<?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:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.plarke.proto1.QuizActivity">  <Button     android:id="@+id/compare_settings"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="Button"     app:layout_constraintTop_toTopOf="parent"     android:layout_marginTop="16dp"     android:layout_marginRight="16dp"     app:layout_constraintRight_toRightOf="parent"     android:layout_marginEnd="16dp" />  <TextView     android:id="@+id/compare_score"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="TextView"     app:layout_constraintTop_toTopOf="parent"     android:layout_marginTop="16dp"     android:layout_marginLeft="16dp"     app:layout_constraintLeft_toLeftOf="parent"     android:layout_marginStart="16dp" />  <SeekBar     android:id="@+id/compare_pitchbar"     android:layout_width="0dp"     android:layout_height="23dp"     android:layout_marginBottom="50dp"     android:layout_marginEnd="16dp"     android:layout_marginLeft="16dp"     android:layout_marginRight="16dp"     android:layout_marginStart="16dp"     android:progressBackgroundTint="@color/colorAccent"     android:progressBackgroundTintMode="src_over"     app:layout_constraintBottom_toTopOf="@+id/textView"     app:layout_constraintHorizontal_bias="0.0"     app:layout_constraintLeft_toLeftOf="parent"     app:layout_constraintRight_toRightOf="parent" />  <Button     android:id="@+id/button2"     android:layout_width="0dp"     android:layout_height="wrap_content"     android:layout_marginBottom="16dp"     android:layout_marginLeft="16dp"     android:layout_marginRight="16dp"     android:text="Button"     app:layout_constraintBottom_toBottomOf="parent"     app:layout_constraintLeft_toLeftOf="parent"     app:layout_constraintRight_toRightOf="parent"     android:layout_marginStart="16dp"     android:layout_marginEnd="16dp" />  <TextView     android:id="@+id/textView"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_marginBottom="50dp"     app:layout_constraintBottom_toTopOf="@+id/button2"     tools:text="The seekbar is all the way to the left."     app:layout_constraintRight_toRightOf="parent"     app:layout_constraintLeft_toLeftOf="parent"     android:layout_marginRight="0dp"     android:layout_marginLeft="0dp" /> 

And I want to change it so that the seekBar defaults to a certain width, but if the screen isn't wide enough, it just fills the screen(with margins). I've tried setting layout_width, but then it just goes off the screen if too wide. Using max_width does nothing at all. Is what I want possible in a ConstrainstLayout? If not what should I use?

like image 999
pjc Avatar asked Jul 10 '17 18:07

pjc


People also ask

Is ConstraintLayout a view?

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).

What is the difference between ConstraintLayout and LinearLayout?

Following are the differences/advantages: Constraint Layout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout).

Which is better RelativeLayout or ConstraintLayout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI.

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.


1 Answers

I think what you're looking for is matchConstraintMaxWidth attribute.

If you add it to your SeekBar with a value of, say, 200dp, keeping left and right constraints to the parent and a width of 0dp, it will stretch the view's width up to 200dp, but if there is more room it will stay at 200dp.

so your xml should look like this:

<SeekBar    ...     android:layout_width="0dp"     app:layout_constraintStart_toStartOf="parent"     app:layout_constraintEnd_toEndOf="parent"     app:layout_constraintWidth_max="200dp" /> 

you should also be able to set it programmatically with

ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(0, 0); layoutParams.matchConstraintMaxWidth = ... 
like image 132
lelloman Avatar answered Sep 22 '22 11:09

lelloman