Extending AlertDialog creates big padding on the bottom of the dialog - android

I am trying to create a dialog which uses a custom view which also has a number of different behaviours attached, which is why I extended AlertDialog instead of using a builder.
The code that creates the view looks like this looks like this
class MyTaskCreateDialog extends AlertDialog implements DialogInterface.OnCancelListener {
public MyTaskCreateDialog(Context ctx, TaskItem item, List<Person> people, IOnFinishListener listener) {
super(ctx) {
View view = LayoutInflater.from(ctx).inflate(Resource.Layout.dialog_my_task_create, null, false);
setView(view);
setCancelable(true);
setOnCancelListener(this);
setCanceledOnTouchOutside(true);
assign_views(view); //this assigns the views to the local variables
attach_listeners();
refresh_view();//this updates the view with data
}
the above uses this layout file
<?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="wrap_content"
android:padding="#dimen/app_padding">
<android.support.design.widget.TextInputLayout
android:id="#+id/my_task_create_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/app_margin"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/my_task_create_hint_title" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/my_task_create_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="#+id/my_task_create_title"
app:layout_constraintStart_toStartOf="#+id/my_task_create_title"
app:layout_constraintTop_toBottomOf="#+id/my_task_create_title">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/my_task_create_hint_description" />
</android.support.design.widget.TextInputLayout>
<TextView
android:id="#+id/my_task_create_assignee_caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/my_task_create_caption_assignee"
app:layout_constraintBottom_toBottomOf="#+id/my_task_create_assignee"
app:layout_constraintStart_toStartOf="#+id/my_task_create_description"
app:layout_constraintTop_toTopOf="#+id/my_task_create_assignee" />
<Spinner
android:id="#+id/my_task_create_assignee"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/app_margin"
android:checked="true"
android:padding="#dimen/app_padding"
android:textOff="#string/my_task_create_toggle_manually"
android:textOn="#string/my_task_create_toggle_automatically"
app:layout_constraintEnd_toEndOf="#+id/my_task_create_description"
app:layout_constraintStart_toEndOf="#+id/my_task_create_assignee_caption"
app:layout_constraintTop_toBottomOf="#+id/my_task_create_description"
tools:listitem="#android:layout/simple_spinner_item" />
<Button
android:id="#+id/my_task_create_create_task"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/app_margin"
android:text="#string/my_task_create_button_create"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/my_task_create_cancel"
app:layout_constraintTop_toBottomOf="#+id/my_task_create_assignee" />
<Button
android:id="#+id/my_task_create_cancel"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/app_margin"
android:text="#string/my_task_create_button_cancel"
app:layout_constraintEnd_toStartOf="#+id/my_task_create_create_task"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/my_task_create_assignee" />
</android.support.constraint.ConstraintLayout>
which creates a dialog with big padding at the bottom.
in fact is I change the background of the ConstraintLayout to be greyish, then the bottom half of the dialog seems to be the default color.
the result of the above looks like this:
now I know that some space is saved for the buttons (I do not use those, I use custom ones from the layout file), but how can I make the rest of the space disappear and make my dialog wrap its content?

I was having the same problem, and the only solution that worked for me is to add the following code into all Views inside of ConstraintLayout:
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
Those sentences replace this:
android:layout_height="wrap_content"

Related

Enable button after user scrolled to the bottom of ScrollView Android

I wanna achieve the following be behavior for one screen of my app.
I have a fragment's layout with ConstraintLayout as it's parent. Inside ConstraintLayout I have a ScrollView with nested ConstraintLayout (nested ConstraintLayout contains ImageView and TextView) and simple Button below the ScrollView.
I wanna enable button as soon as user reaches to the bottom of ScrollView and disable when user scrolls up.
Layout is below.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
>
<ScrollView
android:id="#+id/scrollableView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintBottom_toTopOf="#id/elevationShadow"
app:layout_constraintTop_toBottomOf="#id/appbar">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_marginStart="#dimen/spacing_large"
android:layout_marginEnd="#dimen/spacing_large"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/user_image"
android:layout_width="144dp"
android:layout_height="144dp"
android:layout_gravity="center"
android:layout_marginTop="#dimen/spacing_large"
android:src="#drawable/user_image"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="#+id/heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/text_size_xxlarge"
android:textStyle="bold"
android:textAlignment="center"
android:textColor="#color/black_color"
android:layout_marginTop="#dimen/spacing_large"
tools:text="Tools text"
android:textAppearance="?tvptTextAppearanceBody"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/user_image"
/>
<TextView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_large"
android:textAlignment="center"
android:textSize="#dimen/user_info_content_text_size"
android:textAppearance="?tvptTextAppearanceBody"
android:textColor="#color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/heading"
tools:text="Tools test content"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
<View
android:id="#+id/elevationShadow"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#drawable/shadow_elevation"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginBottom="#dimen/user_info_activity_confirm_button_margin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="#id/button_confirm"/>
<com.travelportdigital.android.compasswidget.button.PercentageBasedStateButton
android:id="#+id/button_confirm"
style="#style/PrimaryButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_marginBottom="#dimen/user_info_activity_confirm_button_margin"
android:text="#string/user_info_continueButton_title"
android:textAllCaps="true"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The problem is that content for TextView which is inside ScrollView can both long and short. That's why I had to add ScrollView if the content is long.
By the small piece of code I was able to achieve the behavior I needed with one small remark.
fun addScrollChangeListener() {
scrollView.viewTreeObserver
.addOnScrollChangedListener {
enableContinueButton(scrollView.getChildAt(0).bottom <= scrollView.height + scrollView.scrollY)
}
}
And the code above works fine for the scenario when content is long (when user arrives to this screen Continue button is disabled and when user scrolls to the bottom of the scroll view it become enabled if user scrolls up it becomes disabled again.
I wanna update this logic to enable button when user arrives to this screen and content of TextView inside ScrollView is short (no need scrolling for this scenario).
I made some researches in Google and could not find the solution which would work for me.
In onViewCreated() method I added logic to disable or enable button when user arrives to this screen.
enableContinueButton(!isScrollingRequired())
I tried this implementation
private fun isScrollingRequired(): Boolean {
val view = scrollView.getChildAt(scrollView.childCount - 1) as View
val diff = view.bottom - (scrollView.height + scrollView.scrollY)
return diff != 0
}
and this
return if (child != null) {
val childHeight = child.height
scrollView.height <= childHeight + scrollView.paddingTop + scrollView.paddingBottom;
} else {
false
}
but it did not work, because ScrollView height and it's child height is always 0
Looking forward your advices.
Regards,
Alex
I dont know is this what you want to do but it should be one of the solution.
I think you can just simply add the button in the "ScrollView" so when user scrolls at the bottom, user will see the button and when user scrolls up, user cannot press the button as well.
Below layout .XML works for me, Using ScrollView with ConstraintLayout:
(you may need extra dependencies)
<ScrollView
android:id="#+id/msg_scroll"
android:layout_width="0dp"
android:layout_height="200dp"
android:fillViewport="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/infoSumm">
<!--Display the <ScrollView> under <TextView>"#+id/infoSumm" -->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/inside_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/infoDetail"
android:text=""
android:layout_marginTop="12dp"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="240dp"
app:layout_constraintTop_toTopOf="#id/inside_scroll"
app:layout_constraintStart_toStartOf="parent"
tools:text="Info Detail"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_infoClose"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/default_field_spacing"
android:backgroundTint="#color/colorPrimary"
android:textAppearance="#style/TextAppearance.MaterialComponents.Button"
android:textAlignment="center"
android:textStyle="bold"
android:textAllCaps="false"
android:textSize="20sp"
android:textColor="#color/colorWhite"
android:text="#string/btn_Close"
android:paddingTop="10dp"
android:paddingBottom="10dp"
app:cornerRadius="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/infoDetail"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

Button with other components inside

I would like to create custom component in Android Studio. This should be a button but with other components like ImageView or ChceckBox inside. How to do this?
I'm already tried create a Compound Control (LinearView with components inside) but I can't set onClickListener to catch click event of this and it's not so elegant solution.
I would like to avoid creating my own component nearly from scratch (extend View and override onDraw method) but I don't know how I can do this in other way.
You can use ConstraintLayout to achieve view over/inside another view, something 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"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
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"
android:scaleType="fitXY"
tools:srcCompat="#tools:sample/avatars[10]" />
<CheckBox
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="check"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="#+id/imageView"
app:layout_constraintStart_toStartOf="#+id/imageView"
app:layout_constraintTop_toTopOf="#+id/imageView" />
</android.support.constraint.ConstraintLayout>
This will look like this:
And about your click listeners - if you want to do the same thing when any of your views are clicked just add a click listener to your parent layout.
If you want to do different things for different view clicks with ConstraintLayout you can simply attach click listener for every view that you have.

Improper text appearance in Custom AlertDialog

In my code, I'm presenting a custom alert dialog by inflating a layout and set it as the dialog's view.
The problem I'm having is that the text looks OK in the Android Studio's design tool, but at run-time, the dialog becomes smaller and the text takes more lines.
I tried different approaches to fix it, but didn't acquire the wanted result.
For the layout, I'm using constraint layout and the text is "wrap content".
Here's my code:
//inflate alert layout
LayoutInflater inflater = LayoutInflater.from(this);
final View view = inflater.inflate(R.layout.connectivity_issue_counter, null);
//set builder
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setView(view);
dialog.setCancelable(false);
final AlertDialog alert = dialog.create();
//define dialog buttons and counter......//
alert.show();
XML:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/alertBackground">
<TextView
android:id="#+id/connectivity_issue_title"
style="#style/customAlertTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="50dp"
android:text="#string/connectivity_issue_title"
app:layout_constraintEnd_toStartOf="#+id/connectivity_issue_wait"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/connectivity_issue_wait"
style="#style/customAlertTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="#string/connectivity_issue_wait"
app:layout_constraintEnd_toStartOf="#+id/connectivity_issue_counter_text"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/connectivity_issue_counter_text"
style="#style/customAlertTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/connectivity_issue_main_text"
style="#style/customAlertMainText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="#string/connectivity_issue_main_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/connectivity_issue_title" />
<Button
android:id="#+id/connectivity_isuue_button"
style="#style/customAlertButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="#string/stop"
app:layout_constraintEnd_toStartOf="#+id/guideline21"
app:layout_constraintStart_toStartOf="#+id/guideline22"
app:layout_constraintTop_toBottomOf="#+id/connectivity_issue_main_text" />
<android.support.constraint.Guideline
android:id="#+id/guideline20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.82" />
<android.support.constraint.Guideline
android:id="#+id/guideline21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.75" />
<android.support.constraint.Guideline
android:id="#+id/guideline22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
</android.support.constraint.ConstraintLayout>
*Another interesting thing - on a different tablet (both the same model) the dialog looks bigger and wider.
I'll appreciate any help.
As Davidaz suggested, I've changed the text size from sp to dp and it helped to solve the problem.
¿Maybe the text is bigger on that phone? I can't see the customAlertTitle style, but I suppose the text is on "SP", and these are "DP" but text-size dependent (if the phone is set to show the text on a big font from it's settings tab, it will increase it's size).
Maybe a solution could be to set the sizes to "DP", but you won't let the people change it, so it has counter effects.
Another idea that comes to my mind is that the size of the screen you are previewing on the preview is larger than your phone's screen.
The last thing I would try is to change the 'android.R.id.message' TextView's size by hand after creating the dialog. Once you show it you can access the message's textview by calling for it's id,
TextView messageTv = (TextView) alert.findViewById(android.R.id.message);
messageTv.setTextSize(X);
Edit: Don't mind that last idea, as you are inflating your own layout, maybe you can try to access your id and change it the same way, but with R.id.connectivity_issue_wait

moving items inside constraint layout cardview

This is my layout I am using for a activity.
<?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.example.phocast.AboutMeActivity">
<android.support.v7.widget.CardView
android:id="#+id/cv_abtme"
android:layout_width="369dp"
android:layout_height="260dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="101dp"
android:background="#color/colorPrimaryLight"
app:cardBackgroundColor="#color/bgcol_abtme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.883">
<ImageView
android:id="#+id/imageView2"
android:layout_width="120sp"
android:layout_height="120sp"
app:srcCompat="#drawable/ic_face_black_24dp" />
<ImageView
android:id="#+id/iv1_abtme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_abtme"
app:srcCompat="#drawable/about_icon_facebook" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:fontFamily="serif-monospace"
android:lineSpacingExtra="12sp"
android:text="#string/facebook_id"
android:textSize="18sp" />
<TextView
android:id="#+id/tv_abtme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="cursive"
android:text="#string/user_name"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textSize="36sp" />
</android.support.v7.widget.CardView>
....
</android.support.constraint.ConstraintLayout>
The output looks like:
The problem is I can't move the items inside the cardview either programatically or using drag in design view of android studio.
Like, for say, I want to have
Imageview iv1_abtme(the facebook icon) below the tv_abtme
textview4 horizentally aligned to iv1_abtme
etc.
Can anyone kindly help me on what is going wrong here(why I cant simply drag items)
CardView is based on a FrameLayout and the documentation there clearly says:
Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
So, to solve your problem, your CardView needs to have a single child that is a view group for your views, such as LinearLayout, RelativeLayout, or ConstraintLayout.

Nested Constraint Layout not showing if using Match_Constraints

I'm trying to create a nested ConstraintLayout in Android.
The objective is to have an Image to the left, and another constraint layout to the right, inside a constraint layout, as the following image:
It correctly shows on the preview, but inside the application, it bugs and doesn't show at all
Layout File:
<?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/box_npcs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/npcImage"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="#+id/layoutInformation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="#+id/layoutInformation"
app:srcCompat="#color/colorBlueishGreen" />
<android.support.constraint.ConstraintLayout
android:id="#+id/layoutInformation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:maxWidth="250dp"
app:layout_constraintHorizontal_bias="0.021"
app:layout_constraintLeft_toRightOf="#+id/npcImage"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/top_priority"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ellipsize="start"
android:text="DummyTextIsDummy"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/common_google_signin_btn_text_light_default"
android:textSize="24sp"
android:textStyle="bold"
android:typeface="normal"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
The layout is being added to another ConstraintLayout using the code
parent.addChild(inflater.inflate(R.layout.box_npc);
How should I fix this? And even, what is the issue?
The situation faced was that OP was trying to add the created ConstraintLayout to another ConstraintLayout programatically. That was being made using
inflater.inflate(R.layout.box, null)
It was an incorrect approach, as this ignores the layout parameters from the box. What was made to fix it was
inflater.inflate(R.layout.box, PARENT_LAYOUT/* One that box was being added to*/)
This fixes some issues due to the fact that the layout parameters were now being respected by the parent layout.
More information: Understaing Android's Layout Inflater.inflate()
For child views of each inner ConstraintLayout, use the id of parent layout instead of "parent".
For example instead of:
app:layout_constraintTop_toTopOf="parent"
Use
app:layout_constraintTop_toTopOf="#+id/parent_id"

Categories

Resources