getY() value doesnt change after changing the height of view - android

I have a frame layout that I add different height views to depending on different circumstances.
I would like to know the Y-axis position of the frame layout before and after the view has been added.
So before I add the view I call getY() on the frame layout which returns 1891 which is correct, once the new view has been added it still returns 1891 which is wrong, it should be less as the frame layout is higher up the screen.
I've checked the layout inspector for both circumstances and getY() gives the correct value so why doesn't my code?
I tried to call rootLayout.requestLayout() to redraw the layout after adding the view but that didn't work.
Log.d(TAG, "openActionBar: " + String.valueOf(actionFrameLayout.getY()));
LayoutInflater.from(getContext()).inflate(R.layout.main_menu_3_item, frameLayout, true);
questionRoot.requestLayout();
Log.d(TAG, "openActionBar: " + String.valueOf(actionFrameLayout.getY()));
Here is my 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/questionRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"
android:clickable="true"
android:focusable="true"
tools:context=".QuestionFragments.Question">
<com.englishquestionstogo.CustomViews.CustomChronometer
android:id="#+id/timer"
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"
/>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/actionFrameLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/questionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold"
tools:text="Title" />
<TextView
android:id="#+id/questionBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/questionTitle"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:paddingBottom="32dp"
android:textSize="16sp" />
</RelativeLayout>
</ScrollView>
<View
android:id="#+id/topBorder"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#D3D3D3"
app:layout_constraintBottom_toTopOf="#id/frameLayout"
/>
<View
android:id="#+id/bottomBorder"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#D3D3D3"
app:layout_constraintBottom_toBottomOf="parent"
/>
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/white"
app:layout_constraintBottom_toTopOf="#id/bottomBorder"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
<FrameLayout
android:id="#+id/actionFrameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="#id/frameLayout"
>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

You should use addOnGlobalLayoutListener
actionFrameLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
//Remove the listener before proceeding
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
actionFrameLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
actionFrameLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
// get the absolute coordinates of a view
Log.d(TAG, "openActionBar: " + String.valueOf(actionFrameLayout.getY()));
}
}
);

Related

Item height in the mvxcrecyclerview

I have a question about the items height in a recyclerview, here is my screen
Is it possible to have the same height for each rows like that for example:
So the first one has to have the same height of the second and the third the same as the 4th one.
Currently I used wrap-content that's why the height is changing regarding the content of the cardview but I would like to use smt dynamic if it possible and not put a fixed height.
Here is my code
<MvvmCross.DroidX.RecyclerView.MvxRecyclerView
android:id="#+id/recyclerViewSalary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
app:layout_constraintTop_toBottomOf="#id/lblSalary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:MvxItemTemplate="#layout/cell_tool_cardview"
app:MvxBind="ItemClick OpenTool;ItemsSource SalarisToolItems"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="2"/>
and the itemtemplate
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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="wrap_content"
android:layout_marginEnd="#dimen/margin2"
android:layout_marginStart="#dimen/margin2"
android:layout_marginBottom="#dimen/margin2"
android:layout_marginTop="#dimen/margin2"
app:cardCornerRadius="#dimen/margin1"
app:cardElevation="0dp"
app:MvxBind="Visibility Visibility(Available)">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/margin4"
android:background="#color/lightBlue">
<FrameLayout
android:id="#+id/frameLayoutCellTool"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<ImageView
android:id="#+id/imgTool"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_gravity="center"
android:scaleType="centerInside"
android:tint="#color/blue"
app:MvxBind="DrawableName ImageDrawable"/>
</FrameLayout>
<TextView
android:id="#+id/lblTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/margin3"
android:layout_marginEnd="#dimen/margin3"
android:textColor="#color/blue"
android:fontFamily="#font/poppins_semibold"
android:textSize="#dimen/t4"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/frameLayoutCellTool"
app:MvxBind="Text Title"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="#dimen/t2"
android:gravity="center"
android:fontFamily="#font/mulish_regular"
android:textColor="#color/blue"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/lblTitle"
app:MvxBind="Text SubTitle; Visibility InvertedVisibility(IsEmptySubTitle)"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Anyone has an idea ? Thanks

RecyclerView's last item is cut off when TextView Error Banner is visible. I'm using Constraint Layout with Barrier

I'm not sure how to ask this question. It seems easy to do but I'm not sure why it's not working. Anyways, here goes. I have this Error Banner that should be visible only if the total "Weight" for the Categories is less than or greater than 100. However my issue is that when the error banner is visible, my RecyclerView's last item is cut off, I think by the height of the error banner.
It will only be fixed if I edit it once again and the keyboard is shown, the recyclerview's height is refreshed (probably).
Here's my Layout XML:
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
tools:context=".presentation.screens.gradesetup.gradingcategories.GradingCategoriesFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/layoutParent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/layoutGradeCategoryList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constrainedHeight="true" >
<TextView
android:id="#+id/tvInvalidGradeWeightLayout"
style="#style/Default_Text_Error_Banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/default_margin_size_16dp"
android:text="#string/error_invalid_grade_category_weight"
android:visibility="gone" />
<!-- Constraint layout_constraintHeight_max dynamically to 24% -->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvGradingCategories"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/tvInfoNoData"
style="#style/Default_Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/text_no_data_display"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Barrier
android:id="#+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="layoutGradeCategoryList" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/layout_adjust_weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/default_margin_size_16dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="#+id/buttonSave"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/barrier"
app:layout_constraintVertical_bias="0.01">
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="#+id/switchViewAdjustWeight"
style="#style/Default_Text_Bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/screen_padding_size"
android:text="#string/text_adjust_weight"
android:theme="#style/Theme.LMS"
android:visibility="gone" />
</androidx.appcompat.widget.LinearLayoutCompat>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/default_margin_size_30dp"
android:src="#drawable/ic_plus"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:tint="#color/white" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/buttonSave"
style="#style/Primary_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/default_margin_size_30dp"
android:enabled="false"
android:text="#string/text_save"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
As you can see I also have this Constraint Barrier, so whenever the height of the RecyclerView is longer, the SwitchMaterial("switchViewAdjustWeight") is also adjusted. I set the android:layout_constraintHeight_max dynamically based on device height.
// Dynamically set constraintMaxHeight
var constraintMaxHeight = 0
constraintMaxHeight =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val windowMetrics = activity!!.windowManager.currentWindowMetrics
val insets: Insets = windowMetrics.windowInsets
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
val height = windowMetrics.bounds.height() - insets.top - insets.bottom
(height * 0.7).toInt()
} else {
val displayMetrics = DisplayMetrics()
activity!!.windowManager.defaultDisplay.getMetrics(displayMetrics)
(displayMetrics.heightPixels * 0.7).toInt()
}
val constraintSet = ConstraintSet()
constraintSet.clone(layoutParent)
constraintSet.constrainMaxHeight(R.id.layoutGradeCategoryList, constraintMaxHeight)
constraintSet.applyTo(layoutParent)
In my code (kotlin): the error banner is toggled to visible if sum != 100
val result = sum == 100
binding.apply {
buttonSave.isEnabled = result
tvInvalidGradeWeightLayout.isVisible = !result
}
Target UI:
I also did change LinearLayout to ConstraintLayout However it didn't work, the Banner overlaps with the RecyclerView list :
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/layoutParent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/layoutGradeCategoryList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constrainedHeight="true" >
<TextView
android:id="#+id/tvInvalidGradeWeightLayout"
style="#style/Default_Text_Error_Banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/default_margin_size_16dp"
android:text="#string/error_invalid_grade_category_weight"
android:visibility="gone"
app:layout_constraintTop_toTopOf="parent"/>
<!-- Constraint layout_constraintHeight_max dynamically to 24% -->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvGradingCategories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintTop_toBottomOf="#+id/tvInvalidGradeWeightLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/tvInfoNoData"
style="#style/Default_Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/text_no_data_display"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Barrier
android:id="#+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="layoutGradeCategoryList" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/layout_adjust_weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/default_margin_size_16dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="#+id/buttonSave"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/barrier"
app:layout_constraintVertical_bias="0.01">
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="#+id/switchViewAdjustWeight"
style="#style/Default_Text_Bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/screen_padding_size"
android:text="#string/text_adjust_weight"
android:theme="#style/Theme.LMS"
android:visibility="gone" />
</androidx.appcompat.widget.LinearLayoutCompat>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/default_margin_size_30dp"
android:src="#drawable/ic_plus"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:tint="#color/white" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/buttonSave"
style="#style/Primary_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/default_margin_size_30dp"
android:enabled="false"
android:text="#string/text_save"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
This worked for me, I added getViewTreeObserver().addOnGlobalLayoutListener for layoutGradeCategoryList and trigger the request layout there.
layoutGradeCategoryList.getViewTreeObserver().addOnGlobalLayoutListener(OnGlobalLayoutListener {
layoutGradeCategoryList.requestLayout();
})
This helped me understand more on requestLayout/forceLayout/invalidate view.
https://stackoverflow.com/a/42430695/3777452
Hope this helps someone else.

How to implement custom tabs with indicator below the custom tab

Current Implementation:
Requirement:
Below is the custom _tab xml which I am inflating into Tablayout and also I need the indicator to take the width of custom square box.I have added the custom_tab.xml layout file.please let me know how to implement one like requirement.
Added tab.xml too
Custom_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
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:layout_gravity="center"
android:layout_margin="10dp"
app:cardCornerRadius="8dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/round_corners_rectangle"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView_tabs"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginTop="-15dp"
android:contentDescription="#string/app_name"
android:src="#drawable/agent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView_tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="rooms"
android:textColor="#color/white"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView_tabs" />
<View
android:id="#+id/underLine"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_marginBottom="5dp"
android:background="#drawable/chat_indicator_line"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_tabs" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
Tab.xml:
<LinearLayout
android:id="#+id/linear_tab"
android:layout_width="match_parent"
android:layout_height="90dp"
android:alpha="0.6">
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/black"
android:baselineAligned="false"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
app:tabIndicatorColor="#color/white"
app:tabIndicatorHeight="3dp"
app:tabPaddingEnd="7dp"
app:tabPaddingStart="3dp" />
</LinearLayout>
Below is the code How I am adding customview programmatically:
private void createTabs() {
for (int i = 0; i < tabArray.length; i++) {
View viewCustomTab =
LayoutInflater.from(this).inflate(R.layout.custom_tab, null);//get custom view
viewCustomTab.setBackgroundColor(getResources().getColor(R.color.black));
ImageView imageViewTab = viewCustomTab.findViewById(R.id.imageView_tabs);
imageViewTab.setImageResource(tabIcons[i]);
TextView textViewTab =
viewCustomTab.findViewById(R.id.textView_tabs);
textViewTab.setText(tabArray[i]);
TabLayout.Tab tab = mTabLayout.getTabAt(i);//get tab via position
if (tab != null) {
tab.setCustomView(viewCustomTab);//set custom view
}
}
}

Recycler view does not show items list when layout_height = "match_constraint" in Constraint layout

I want to create a UI like: for which I am using a recycler view under constraint layout.
But the issue is when I set layout_height="match_constraint" of recyclerView then it does not show any list and when I set to layout_height="wrap_content" then the list goes out the bound and is not scrollable and when I set it to a fixed length then it does not fill the remaining screen vertically.
and it is shown like this:
Here is how it shows nothing when layout_height = "match_constraint":
Here is my xml code:
<?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=".searchResultListFragment">
<ImageView
android:id="#+id/thumbnail_img"
android:layout_width="0dp"
android:layout_height="150dp"
android:scaleType="fitXY"
android:src="#drawable/shopping_mart_transparent"
app:layout_constraintBottom_toTopOf="#id/search_result_recycler_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tv_search_result_msg"
style="#style/search_result_msg_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:text="Attraction Points in Naran"
app:layout_constraintBottom_toBottomOf="#id/thumbnail_img"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/tv_search_result_count"
style="#style/search_result_count_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="22dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="3 Results"
app:layout_constraintBottom_toBottomOf="#id/thumbnail_img"
app:layout_constraintEnd_toEndOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="#+id/search_result_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/thumbnail_img" />
</android.support.constraint.ConstraintLayout>
It is because at the very last line of your RecyclerView xml your added a wrong character
Change :
app:layout_constraintTop_toBottomOf="#+id/thumbnail_img"
to :
app:layout_constraintTop_toBottomOf="#id/thumbnail_img"
Same for other Views (ImageView and 2 TextViews), adding a + means that you want to add a reference not to link to that reference.
Plus you don't need this line in the ImageView :
app:layout_constraintBottom_toTopOf="#+id/search_result_recycler_view"
As by searching a lot I could not find any solution to this issue. So I came up with my own solution by replacing constraint layout with RelativeLayout. Although this is the ideal solution but this is how I resolved the Issue I was facing.
Here is the xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".searchResultListFragment"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_weight="0">
<ImageView
android:id="#+id/thumbnail_img"
android:layout_width="0dp"
android:layout_height="150dp"
android:scaleType="fitXY"
android:src="#drawable/shopping_mart_transparent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tv_search_result_msg"
style="#style/search_result_msg_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:text="Attraction Points in Naran"
app:layout_constraintBottom_toBottomOf="#id/thumbnail_img"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/tv_search_result_count"
style="#style/search_result_count_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="22dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="3 Results"
app:layout_constraintBottom_toBottomOf="#id/thumbnail_img"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/search_result_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

how to fix the alteration of visibility inside a recyclerview's viewholder layout

I have a recyclerview using the following xml as a list item for each of its views
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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"
app:cardElevation="2dp">
<android.support.constraint.ConstraintLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/item_question_name"
style="?android:attr/textAppearanceLarge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/app_margin"
app:layout_constraintBottom_toTopOf="#+id/item_question_question"
app:layout_constraintEnd_toStartOf="#+id/item_question_needs_sync"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="This is the name" />
<android.support.v7.widget.AppCompatImageView android:id="#+id/item_question_needs_sync"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="#+id/item_question_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/item_question_name"
app:srcCompat="#drawable/ic_sync" />
<TextView android:id="#+id/item_question_question"
style="?android:attr/textAppearanceMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/app_margin"
app:layout_constraintBottom_toTopOf="#+id/item_question_type"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/item_question_name"
app:layout_constraintTop_toBottomOf="#+id/item_question_name"
tools:text="this is the question" />
<TextView android:id="#+id/item_question_type"
style="?android:attr/textAppearanceMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/app_margin"
android:layout_marginTop="#dimen/app_margin"
app:layout_constraintBottom_toTopOf="#+id/item_question_separator"
app:layout_constraintEnd_toStartOf="#+id/item_question_info"
app:layout_constraintHorizontal_weight="5"
app:layout_constraintStart_toStartOf="#+id/item_question_question"
app:layout_constraintTop_toBottomOf="#+id/item_question_question"
tools:text="Four faces which are awesome" />
<TextView android:id="#+id/item_question_info"
style="?android:attr/textAppearanceSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/item_question_type"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="3"
app:layout_constraintStart_toEndOf="#+id/item_question_type"
app:layout_constraintTop_toTopOf="#+id/item_question_type"
tools:text="answered ALL the questions" />
<View android:id="#+id/item_question_separator"
android:layout_width="0dp"
android:layout_height="2dp"
android:background="#color/colorAccent"
app:layout_constraintBottom_toTopOf="#+id/item_question_buttons"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/item_question_info" />
<android.support.constraint.ConstraintLayout android:id="#+id/item_question_buttons"
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_toBottomOf="#+id/item_question_separator">
<Button android:id="#+id/item_question_delete"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/item_question_button_delete"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/item_question_edit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button android:id="#+id/item_question_edit"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/item_question_button_edit"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="#+id/item_question_delete"
app:layout_constraintEnd_toStartOf="#+id/item_question_view"
app:layout_constraintStart_toEndOf="#+id/item_question_delete"
app:layout_constraintTop_toTopOf="#+id/item_question_delete" />
<Button android:id="#+id/item_question_view"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/item_question_button_view"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="#+id/item_question_delete"
app:layout_constraintEnd_toStartOf="#+id/item_question_start"
app:layout_constraintStart_toEndOf="#+id/item_question_edit"
app:layout_constraintTop_toTopOf="#+id/item_question_delete" />
<Button android:id="#+id/item_question_start"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/item_question_button_start"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="#+id/item_question_delete"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/item_question_view"
app:layout_constraintTop_toTopOf="#+id/item_question_delete" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
my problem is that when the user clicks on the view I have the viewholder toggling whether the #+id/item_question_buttons will be shown or not like so:
void bind_view(Info info, boolean selected) {
//load the data from info
.....
// _buttons is a View object where I stored the constraint layout with the buttons
if (selected) {
_buttons.setVisibility(VISIBLE);
} else {
_buttons.setVisibility(GONE);
}
}
my problem is that every other time (i.e. the 2nd 4th 6th etc) the user toggles the visibility of the _buttons View the view expands to triple the normal size (which is the same as the max height a button can have).
I solved it by using android:maxLines="1" but I was wandering , why does this happen? and is there a better way to fix this?
thanks in advance for any help you can provide

Categories

Resources