I've been through several solutions over the stack overflow but could not achieve the desired result. I have a firebase recyclerview for a comment section. The firebase recyclerview exist with in the scroll view as am scroll some other part of the layout as well with the normal firebase recyclerview. I've set RecyclerView.setNestedScrollingEnabled(false), so the scrollview can scroll smoothly. right now I'm able to successfully scroll through out the layout but my problem only arises when I enter the activity the layout is shown from the top which is something, I don't want. I want the firebase recyclerview or the scroll view to be shown from the last item which is in the bottom. Also I want to the RecyclerView to automatically scroll to the last item position whenever the user enters a new comment
Any Help would be appreciated thank you
Below is my code
the code inside the oncreate for initialization
replyInDetailCommentsRecyclerView = findViewById(R.id.replyInDetailCommentsRecyclerView);
linearLayoutManager1 = new LinearLayoutManager(this);
replyInDetailCommentsRecyclerView.setLayoutManager(linearLayoutManager1);
replyInDetailCommentsRecyclerView.setNestedScrollingEnabled(false);
The code inside the firebase recyclerview, I've only added the relevant part to the problem. hope it helps
adapter1.startListening();
adapter1.notifyDataSetChanged();
adapter1.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int friendlyMessageCount = adapter1.getItemCount();
int lastVisiblePosition =
linearLayoutManager1.findLastCompletelyVisibleItemPosition();
if (lastVisiblePosition == -1 ||
(positionStart >= (friendlyMessageCount - 1) &&
lastVisiblePosition == (positionStart - 1))) {
linearLayoutManager1.scrollToPosition(positionStart);
} else {
replyInDetailCommentsRecyclerView.scrollToPosition(adapter1.getItemCount() - 1);
}
}
});
replyInDetailCommentsRecyclerView.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
My XML Code
<ScrollView
android:layout_below="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:fillViewport="true"
android:fitsSystemWindows="true"
android:layout_marginBottom="50dp"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/RelativeLayout5"
android:layout_width="match_parent"
android:layout_below="#id/RelativeLayout1"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/RelativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/RelativeLayout4">
<android.support.v7.widget.RecyclerView
android:id="#+id/replyInDetailCommentsRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/imageInDetailComments"
android:layout_width="30dp"
android:src="#drawable/profile"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_height="30dp"/>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/nameInDetailComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="bold"
android:layout_alignTop="#+id/imageInDetailComments"
tools:text="Saqib"
android:layout_toRightOf="#+id/imageInDetailComments"
android:layout_marginLeft="5dp"
android:textColor="#096697"
android:layout_toEndOf="#+id/imageInDetailComments"
android:layout_marginStart="5dp" />
<android.support.v7.widget.AppCompatRatingBar
android:id="#+id/ratingInDetailComments"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:max="5"
android:numStars="5"
android:progressBackgroundTint="#color/colorAccent"
android:progressTint="#color/colorAccent"
android:rating="0.0"
android:secondaryProgressTint="#color/colorAccent"
android:stepSize="0.0"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imageInDetailComments"
android:layout_marginLeft="5dp"
android:layout_below="#+id/nameInDetailComments"
android:layout_marginStart="5dp"
android:layout_toEndOf="#+id/imageInDetailComments" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/userCommentInDetailComments"
android:layout_width="0dp"
android:layout_below="#+id/ratingInDetailComments"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/imageInDetailComments"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:background="#efeeee"
android:padding="5dp"
android:text="i can do it mate, my bidding price includes gotaskie fee"
/>
<View
android:layout_below="#+id/userCommentInDetailComments"
android:layout_marginTop="8dp"
android:id="#+id/lineAboveCommentsRecyclerView"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#dad8d8"
/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
You can use NestedScrollView instead of ScrollView
Related
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>
I have create an android app with Kotlin.This app contains an interface, which contains a recyclerview to get all the list of product.
To fill this list of product, i create an adapter, this adapter is a cardview : Imageview and two textview.
In this interface, i added two buttons, one when i click on all the product display with the list and the other one display with grid disposition.The default value for grid disposition.
I wsant to change the width and the height of a cardview, to display all the product in the List.
The following code is anadapter item:
<?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:id="#+id/cardProductItem"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_marginTop="#dimen/spacing_medium"
android:layout_marginBottom="#dimen/spacing_medium"
android:layout_marginRight="#dimen/spacing_middle"
android:layout_marginLeft="#dimen/spacing_middle"
android:background="#color/grey">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/productImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_centerInParent="true"
android:layout_gravity="center"/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="#dimen/spacing_middle">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/productName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="#dimen/spacing_medium"
android:fontFamily="#font/lato_regular"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/productPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="#dimen/spacing_medium"
android:textStyle="bold"
android:fontFamily="#font/lato_regular"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView
android:id="#+id/arrowDetailProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_margin="8dp"
android:visibility="gone"
android:layout_gravity="center|center_horizontal"
android:src="#drawable/ic_right_arrow"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
the following code is an extract from an xml file which cotains a recyclerview :
<ImageView
android:id="#+id/gridProducts"
android:layout_width="#dimen/spacing_mlarge"
android:layout_height="#dimen/spacing_mlarge"
android:scaleType="fitXY"
android:layout_toLeftOf="#+id/productNumber"
android:layout_margin="8dp"
android:src="#drawable/ic_divided_squares"/>
<ImageView
android:id="#+id/listProducts"
android:layout_width="#dimen/spacing_mlarge"
android:layout_height="#dimen/spacing_mlarge"
android:scaleType="fitXY"
android:layout_margin="8dp"
android:src="#drawable/ic_rounded_black_square_shape"/>
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewProductByCategory"
android:layout_below="#+id/recyclerViewCategories"
android:layout_marginTop="66dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/spacing_middle"
android:layout_marginEnd="#dimen/spacing_middle"
android:orientation="horizontal"/>
the following code contains the onclick acion into the two buttom(Imageview) :
gridProducts.setOnClickListener {
recyclerViewProductByCategory.layoutManager =
StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
recyclerViewProductByCategory.adapter = products?.let { it -> ProductAdapter(it) }
gridProducts.visibility = View.INVISIBLE
listProducts.visibility = View.VISIBLE
}
listProducts.setOnClickListener {
recyclerViewProductByCategory.layoutManager =
LinearLayoutManager(this#CategoryByProduct, RecyclerView.VERTICAL, false)
recyclerViewProductByCategory.adapter = products?.let { it -> ProductAdapter(it) }
val layoutParams = LayoutParams(
LayoutParams.MATCH_PARENT, // CardView width
LayoutParams.WRAP_CONTENT // CardView height
)
cardProductItem.layoutParams = layoutParams
listProducts.visibility = View.INVISIBLE
gridProducts.visibility = View.VISIBLE
}
after running my app an exception appear as the following :
java.lang.IllegalStateException: cardProductItem must not be null
could you please tell me where's the error and how can i correct it
Based on the code available, you are setting the value of cardProductItem outside of setOnClickListener. The error is stating that cardProductItem can not be set when a listProduct is clicked. Make sure that you are setting cardProductItem to a value before the UI is presented to the user.
For a more in-depth answer, I would need to look at the other places where cardProductItem is used.
<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.partyspottr.appdir.ui.mainfragments.eventchildfragments.alle_eventer_fragment">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_layout_events"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="#dimen/_8sdp"
android:layout_marginStart="#dimen/_8sdp"
android:layout_marginTop="#dimen/_5sdp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/search_alle_eventer"
app:layout_constraintVertical_bias="0.0">
<ListView
android:id="#+id/lvalle_eventer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/transparent"
android:dividerHeight="#dimen/_20sdp"
android:scrollbarSize="#dimen/_3sdp"
android:scrollbarThumbVertical="#color/lightred"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<EditText
android:id="#+id/search_alle_eventer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="#dimen/_8sdp"
android:layout_marginStart="#dimen/_8sdp"
android:layout_marginTop="#dimen/_90sdp"
android:background="#drawable/border_rounded"
android:hint="Search.."
android:inputType="text"
android:paddingEnd="#dimen/_10sdp"
android:paddingStart="#dimen/_10sdp"
android:textColor="#android:color/black"
android:textSize="#dimen/_15ssp"
app:layout_constraintEnd_toEndOf="#+id/swipe_layout_events"
app:layout_constraintStart_toStartOf="#+id/swipe_layout_events"
app:layout_constraintTop_toTopOf="parent" />
This is my layout XML, containing a parent (ConstraintLayout), a SwipeRefreshLayout with a ListView as child, and an EditText which i use to search items inside the ListView.
This layout is used as a fragment, so i have a button outside of this layout, when i press this button, the EditText either shows or doesn't, i do this with this code:
search_alle_eventer.setVisibility(search_alle_eventer.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
ViewGroup.LayoutParams params = search_alle_eventer.getLayoutParams();
if(search_alle_eventer.getVisibility() == View.INVISIBLE)
params.height = 0;
else {
params.height = WRAP_CONTENT;
search_alle_eventer.setLayoutParams(params);
Now, the problem im having is when the EditText is showing (WRAP_CONTENT), when i scroll to the bottom of the ListView, the last item is cut off. How can i fix this?
you can set margin from bottom to ListView. android:marginBottom = "50dp"
Or try with relative layout like this
< RelativeLayout
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.partyspottr.appdir.ui.mainfragments.eventchildfragments.alle_eventer_fragment">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_layout_events"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="#dimen/_8sdp"
android:layout_marginStart="#dimen/_8sdp"
android:layout_marginTop="#dimen/_5sdp"
>
<ListView
android:id="#+id/lvalle_eventer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/transparent"
android:dividerHeight="#dimen/_20sdp"
android:scrollbarSize="#dimen/_3sdp"
android:scrollbarThumbVertical="#color/lightred"
/>
</android.support.v4.widget.SwipeRefreshLayout>
<EditText
android:id="#+id/search_alle_eventer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="#dimen/_8sdp"
android:layout_marginStart="#dimen/_8sdp"
android:layout_marginTop="#dimen/_90sdp"
android:background="#drawable/border_rounded"
android:hint="Search.."
android:inputType="text"
android:paddingEnd="#dimen/_10sdp"
android:paddingStart="#dimen/_10sdp"
android:textColor="#android:color/black"
android:textSize="#dimen/_15ssp"
/>
</RelativeLayout>
Alright, thanks for you help, but my case is a special needs case, whatever im doing, im sure is not the correct way, but i'll let it be.
Fixed it by adding some more code inside the OnClickListener of the button i mentioned:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(search_alle_eventer.getVisibility() == View.INVISIBLE)
listView.setPadding(0, 0, 0, 0);
else
listView.setPadding(0, 0, 0, search_alle_eventer.getHeight());
}
}, 100);
Adding some padding on the ListView whenever the EditText is visible did the trick for me.
EDIT: Thank you Mark Keen for pointing out the container for the fragments, I was being retarded not thinking straight. I fixed it by resizing the container.
Tbh, sorry for this question.
I'm encountering some really strange behaviors with RecyclerView and it's about anchoring and aligning views inside one row of RecyclerView. So here is how the result from row should look:
And here are two pictures showing final result:
This is not happening always. Sometimes views inside are messed up like on attached images above and sometimes are not. Anyway here is my xml file showing how row layout looks:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout android:id="#+id/background"
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">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_margin="24dp"
android:id="#+id/iv_user_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_zelena_bez_lopte"
app:civ_border="true"
app:civ_border_color="#color/colorAccent"
app:civ_border_width="1dp"
app:civ_shadow="true"
app:civ_shadow_color="#android:color/black"
app:civ_shadow_radius="10"/>
<LinearLayout
android:id="#+id/username_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/iv_user_icon"
android:layout_alignTop="#+id/iv_user_icon"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_toEndOf="#+id/iv_user_icon"
android:layout_toRightOf="#+id/iv_user_icon"
android:gravity="center|start"
android:orientation="vertical">
<TextView
android:id="#+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mr.Bean"
android:textColor="#android:color/white"
android:textSize="12sp"
android:typeface="sans"/>
<TextView
android:id="#+id/tv_city_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#color/colorAccent"
android:textSize="12sp"
android:typeface="sans"/>
</LinearLayout>
<ImageView
android:id="#+id/iv_option"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignBottom="#+id/iv_user_icon"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/iv_user_icon"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="#null"
android:src="#drawable/ic_message"/>
<View
android:id="#+id/lst_item_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignLeft="#+id/username_container"
android:layout_alignStart="#+id/username_container"
android:layout_below="#+id/username_container"
android:background="#color/main_text_color"/>
</RelativeLayout>
<ImageView
android:visibility="visible"
app:layout_anchor="#+id/iv_user_icon"
app:layout_anchorGravity="bottom|center|end|right"
android:src="#drawable/icon"
android:id="#+id/iv_star"
android:layout_width="30dp"
android:layout_height="30dp"/>
</android.support.design.widget.CoordinatorLayout>
I'm doing some hiding views inside adapter when i need to, and maybe that's an issue, i really don't know. I have tried without hiding views when checking some conditions inside adapter, but same results. Just to explain, i'm hiding inbox icon because if my username is showed in list i would like to hide it because i wouldn't want to send myself message and also this badge icon will be shown to one user only.
Here is code from adapter:
#Override
public void onBindViewHolder(final TeamMembersListAdapter.HeaderViewHolder holder, int position) {
final User user = teamUsers.get(position);
Glide.with(mContext)
.load(Uri.parse(user.getImage()))
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.placeholder(R.drawable.ic_zelena_bez_lopte)
.error(R.drawable.ic_zelena_bez_lopte)
.dontAnimate()
.into(holder.profileImage);
String selfUserId = AppController.getInstance().getDatabase().getUserDetails().get("user_id");
if (user.getCreatorId() == Integer.parseInt(user.getId())) {
holder.ivBadge.setVisibility(View.VISIBLE);
} else {
holder.ivBadge.setVisibility(View.INVISIBLE);
}
if (selfUserId.equals(user.getId())) {
holder.ivSendMessage.setVisibility(View.INVISIBLE);
} else {
holder.ivSendMessage.setVisibility(View.VISIBLE);
}
holder.tvUsername.setText(user.getName());
holder.tvCityName.setText(user.getCity());
holder.bgContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mOnItemClickListener.onItemClickListener(holder.getAdapterPosition());
}
});
}
Shouldn't all views be together inside the RelativeLayout?
I don't get why You even need the CoordinatorLayout, maybe try to get rid off Coordinator and wrap all inside Relative.
I would suggest also to try the ConstraintLayout - it should be super easy to done this in such layout.
Here, check this out
https://developer.android.com/training/constraint-layout/index.html#add-constraintlayout-to-your-project
In my chat application I have a layout with a listview (a chat between two users) and an edittext input for writing a new message for the same conversation.
The first problem happens when a user clicks on input - a soft keyboard pops up but the listview remains at the same position as before, instead of jumping up along with an edittext input and keeping the same interval between.
The second problem is when I dismiss keyboard and return to the default state with a large number of lines, the listview again remains at the same position and the edittext overlaps it.
My layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/main_bg"
android:gravity="right"
android:orientation="vertical" >
<include
android:layout_alignParentTop="true"
layout="#layout/actionbar_layout" />
<ListView
android:id="#+id/lsvMessage"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:layout_marginTop="50dp"
android:background="#color/_transperant"
android:divider="#null"
android:dividerHeight="0dp">
</ListView>
<LinearLayout
android:id="#+id/txtMsgLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:background="#color/_transperant">
<EditText
android:id="#+id/txtMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="#string/btn_ask_hint"
android:background="#drawable/edit_text_style"
android:textColor="#color/_white"
android:singleLine="true"
android:textAlignment="viewStart"
android:inputType="textMultiLine|textCapSentences|textNoSuggestions"
android:layout_weight="1">
</EditText>
<RelativeLayout
android:id="#+id/message_spinner_layout"
style="#style/layout_fill_width"
android:gravity="right"
android:layout_weight="0">
<Spinner
android:id="#+id/message_lng_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:gravity="right"
android:paddingLeft="10dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:background="#drawable/spinner_setting"
android:layout_alignBottom="#id/message_lng_spinner"
android:layout_marginBottom="12dp"
android:layout_marginRight="20dp" />
</RelativeLayout>
<Button
android:id="#+id/btnSend"
style="#style/submit_button_style"
android:text="#string/btnSend"
android:background="#drawable/submit_button"
android:layout_weight="0"/>
</LinearLayout>
</RelativeLayout>
I tried to fix it programmatically and count the high of edittext layout to move listview accordingly but it didn't work.
final View activityRootView =
((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
final View msgLayout = (ViewGroup)findViewById(R.id.txtMsgLayout);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener
(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) {
scrollMyListViewToBottom();
} else {
int heighListview = msgLayout.getHeight();
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)
lsvMessage.getLayoutParams();
params.setMargins(0,50,0,heighListview);
lsvMessage.setLayoutParams(params);
}
}
});
Can I fix it with a properly marked up layout or code behind?
I had same problem. I have tried all answers in the stackoverflow, but not have any final solution.
I found solution from google native message app:
ComposeMessageActivity layout
ComposeMessageActivity here have a method: "smoothScrollToEnd"
and there are have a MessageListView.java class for detect onSizeChanged.