How to make Android textview selectable and scrolled to bottom (by default)? - android

I have a textview which text must be selectable textView.setTextIsSelectable(true) and scrollable to bottom by default textView.setMovementMethod(ScrollingMovementMethod.getInstance()).
<?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"
android:background="#color/colorScreenBackground"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.xxxx.xxxx.MainActivity"
tools:showIn="#layout/activity_main"
android:orientation="vertical"
android:weightSum="100">
<TextView
android:id="#+id/result"
android:scrollbars = "vertical"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:elevation="8dp"
android:textIsSelectable="true"
android:gravity="bottom"
android:background="#color/white"
android:padding="5dp"
android:focusable="true"
android:fastScrollEnabled="true"
android:fastScrollAlwaysVisible="true"
android:scrollbarSize="7dip"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium" />
<androidx.core.widget.NestedScrollView
android:id="#+id/nested_scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="60"
android:background="#color/gray_background"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:listitem="#layout/recyclerview_item"
android:background="#color/gray_background"
/>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
The problem is that the setMovementMethod disable the textview selectability.
How to solve this?
NB: The principal feature required here is that not only the textview must be scrollable, but it must also appeared initially scrolled to bottom by default, and this is provided by the setMovementMethod(ScrollingMovementMethod.getInstance()), according to How to get the Android TextView to scroll down to the end automatically.

Put your text view inside an scroll view in your xml
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40">
<TextView
android:id="#+id/textView"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="8dp"
android:textIsSelectable="true"
android:gravity="bottom"
android:background="#color/white"
android:padding="5dp"
android:text="#string/dummy"
android:focusable="true"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium" />
</ScrollView>
Then do this in your class
//This will scroll to bottom
ScrollView scrollView=findViewById(R.id.scrollView);
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});

Related

Two recycleviews in one activity scroll problem

I have created 2 recycleviews in one activity. One scrolls horizontally while other scrolls vertically. I can scroll correctly inside each RecyclerView but the page as a whole won't scroll i.e. top RecyclerView stays at the top always and bottom one stays at the bottom like both are fixed in position.
<ScrollView
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"
tools:context="com.shakeelnawaz.recipes.AllRecipesFragmet">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="#string/trending_recipes"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/horizontaltrendingrecycleview"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginStart="15dp"
android:orientation="horizontal"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="#string/all_recipes"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</ScrollView>
I read this post "Scolling with multiple RecyclerViews in Layout" and set vertical recycleview's height programmatically. like this
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// calculate height of RecyclerView based on child count
params.height=1150;
// set height of RecyclerView
recyclerView.setLayoutParams(params);
But the problem is that how I can calculate the height of RecyclerView based on child count?
Use NestedScrollView instead of ScrollView
and use this below line in your Fragment in Activity.
ViewCompat.setNestedScrollingEnabled(mYourRecycleView, false);
This will be work on all your Android API level.
Replace ScrollView with NestedScrollView
Then add: horizontaltrendingrecycleview.isNestedScrollingEnabled = false
Your XML should look like below:
<android.support.v4.widget.NestedScrollView
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:fillViewport="true"
tools:context="com.shakeelnawaz.recipes.AllRecipesFragmet">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="#string/trending_recipes"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/horizontaltrendingrecycleview"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginStart="15dp"
android:orientation="horizontal"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="#string/all_recipes"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

View doesn't reappear after setVisibility(View.GONE)

I have a LinearLayout which I'm trying to make VISIBLE, then GONE, then VISIBLE again. However after making it GONE, it doesn't become visible again. As a note, it works fine if I make it INVISIBLE: it appears and disappears with no problem.
These are my files:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/noconnectionll"
android:visibility="gone"
android:padding="#dimen/node_default_spacing"
android:background="#color/category_no_internet"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="#string/no_internet_short"
android:textColor="#fff"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/no_connection_retry"
android:text="#string/try_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<ListView
android:dividerHeight="0dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:id="#+id/spinner_wrapper"
tools:visibility="visible"
android:visibility="invisible"
android:background="#a0ffffff"
android:gravity="center_horizontal|center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/spinner"
android:src="#drawable/ic_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/category_loading"
android:text="#string/loading"
android:textColor="#000"
android:padding="#dimen/activity_padding"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
This is the method I'm using to show/hide:
private void hideOrShowLoading(final boolean show) {
final ViewGroup spinnerWrapper = (ViewGroup)ll.findViewById(R.id.spinner_wrapper);
final View spinner = ll.findViewById(R.id.spinner);
if (show) {
spinnerWrapper.setVisibility(View.VISIBLE); // Not working after being set to GONE previously
final Animation rotation = AnimationUtils.loadAnimation(getContext(), R.anim.rotate);
rotation.setFillAfter(true);
spinner.startAnimation(rotation);
} else {
spinnerWrapper.setVisibility(View.GONE);
spinner.clearAnimation();
}
}
Any tips? Could it have something to do with the ListView above it having a height of 0 and a weight of 1?
Thanks
Update
I also tried the following after the if (show) block, with no luck:
listview.invalidate();
spinnerWrapper.invalidate();
ll.invalidate();
ll.requestLayout();
ll is the LinearLayout that contains both.
While I believe this is caused by the layout_weight=1 property, I wasn't able to properly fix this.
The following hack did work. Basically, I'm surrounding the spinnerWrapper in another linear layout which is always visible.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/noconnectionll"
tools:visibility="visible"
android:visibility="gone"
android:padding="#dimen/node_default_spacing"
android:background="#color/category_no_internet"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="#string/no_internet_short"
android:textColor="#fff"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/no_connection_retry"
android:text="#string/try_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<ListView
tools:background="#d496d4"
android:dividerHeight="0dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<!-- Weird bug with layout_weight=1 on the listview above means we need this linearlayout -->
<LinearLayout
android:background="#0ca917"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/spinner_wrapper"
android:background="#a0ffffff"
android:gravity="center_horizontal|center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/spinner"
android:src="#drawable/ic_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/category_loading"
android:text="#string/loading"
android:textColor="#000"
android:padding="#dimen/activity_padding"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

RecyclerView does not show all items inside Fragment

The problem
In my Activity I initialize a Fragment, which contains a RecyclerView. But I've recognized that it is not displaying all items. That means instead of 14 it only shows 11 items. In addition the last visible item (number 12) is cutted off
My Implementation
The Activity contains an empty FrameLayout that acts as the Fragment container:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res./android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e0e0e0">
<LinearLayout
android:id="#+id/layout_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/layout_border_white"
android:paddingBottom="#dimen/footer_padding">
<include layout="#layout/footer"></include>
</LinearLayout>
<FrameLayout
android:id="#+id/fragment_start"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/layout_footer"/>
</RelativeLayout>
And here is the RecyclerView in the Fragment:
<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="#e0e0e0"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout_header"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:padding="#dimen/startpage_padding"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<include layout="#layout/header"></include>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/startpage_margin"
android:scrollbars="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/layout_header"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ProgressBar
android:id="#+id/pb_loading_indicator"
android:layout_width="#dimen/startpage_progressbar"
android:layout_height="#dimen/startpage_progressbar"
android:visibility="invisible"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:padding="#dimen/startpage_text_padding"
android:text="#string/error_message"
android:textSize="#dimen/startpage_text"
android:visibility="invisible"/>
</LinearLayout>
Solving Approaches
I've already tried it with match_parent in the RecyclerView and also added a paddingBottom to it. With paddingBottom="30dp" one more element is visible but that's not a good solution. Furthermore my Activity is using the AlertDialog-Theme which is setted in the Manifest. Removing it shows the same result.
Any help would be greatly appreciated.
I finally fixed it. The problem was the wrap_content in my RecyclerView inside ConstraintLayout. By using wrap_content for the height of the RecyclerView the parent cuts the bottom of it off. So you have to use 0dp for the height and a constraintBottom.
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="#dimen/startpage_margin"
android:scrollbars="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/layout_header"/>
try to change your xml to following, as constraint layout wraps spaces when used in incorrect way:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#e0e0e0">
<LinearLayout
android:id="#+id/layout_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white">
<include layout="#layout/support_simple_spinner_dropdown_item"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/startpage_margin"
android:scrollbars="vertical"
android:layout_below="+id/layout_header"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/pb_loading_indicator"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_centerInParent="true"
android:visibility="invisible"/>
<TextView
android:id="#+id/tv_error_message_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="Error"
android:textSize="20sp"
android:visibility="invisible"/>
</RelativeLayout>
</LinearLayout>
I used wrap_content for the height but the bottom of the recycleview still scrolled off bottom of the screen so that the last item was not visible. I resorted to using a finagled solution of adding a bottom padding on the recyclerview until the last item was visible

How to remove default divider from recylerview

I have added android-recyclerview in my app which is running fine. According to my layout i have to remove divider between cells. In list view this can easily be achieved by setting divider property to null like below in code but i am not able to find any such property in android-recyclerview. How can i achieve this in android-recyclerview.
getListView().setDivider(null);
getListView().setDividerHeight(0);
OR
android:divider="#null"
android:dividerHeight="0dp"
Recylerview xml below :
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_contact_list_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:clickable="true"
android:focusable="true"
android:scrollbars="vertical" />
Adapter xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/recyclerview_item_borders"
android:padding="10dp">
<LinearLayout
android:id="#+id/section_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:paddingBottom="30dp"
>
<TextView
android:id="#+id/textview_section_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/circular_image_contact"
android:layout_width="#dimen/auto_complete_circular_image_dimen"
android:layout_height="#dimen/auto_complete_circular_image_dimen"
app:civ_border_color="#FF000000"
android:layout_weight="0.2"
android:src="#mipmap/ic_default_contact"
app:civ_border_width="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:orientation="vertical">
<TextView
android:id="#+id/textView_contact_name"
android:layout_width="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:textColor="#color/textDarkColor"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/textView_contact_number"
android:layout_width="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:textColor="#color/textDarkColor"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
</LinearLayout>
</LinearLayout>
Remove background from adapter
android:background="#drawable/recyclerview_item_borders"
Recyclerview don't have the divider by default. If you need to add the divider, you have to add the decoration to the recyclerview.
It is from the custom view you are using by default there is no divider. share your code thn any one can help you.
You need to change it in the List item view. Add following parameters to list item view.
card_view:cardCornerRadius="0dp"
card_view:cardElevation="0dp"
card_view:cardUseCompatPadding="true"
In list_item_view.xml
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="0dp"
card_view:cardUseCompatPadding="true">
Then set background color of your Recycle view to its foreground color. If it is white,
In recycle_view.xml
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:background="#ffffff"
android:scrollbars="vertical"
android:layout_height="wrap_content"/>

Android textview inside gridview

I put a textview inside the header of a gridview but if I need to scroll the textview it not scroll, instead scroll the gridview.
This is the code:
<GridWithHeader
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridWithHeader"
android:numColumns="2"
android:focusableInTouchMode="true"
android:nestedScrollingEnabled="true"
android:focusable="true" />
And this is the content of header
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:id="#+id/row_2"
android:weightSum="1">
<TextView
android:id="#+id/text_header_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
where is the error or how can I solve the problem?
try to delete from "GridWithHeader" android:focusableInTouchMode and android:focusable="true" and add them to you Textview like this:
<TextView
android:id="#+id/text_header_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:fadeScrollbars="false"
android:scrollbars="vertical" >

Categories

Resources