How to remove focus from RecyclerView inside ScrollView? - android

I have a Scrollview which contains an ImageView and RecyclerView.
if navigation drawer opened then closed the RecyclerView auto scrolling to top, How to stop this?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollViewMain"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView_main_icons_line"
android:src="#drawable/main_line" />
...
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_activity_main_passenger_log"
android:paddingBottom="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</RelativeLayout>

This problem because of recyclerView has default focus.
Solution
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
Add android:descendantFocusability="blocksDescendants" to your immediate layout of scrollView

That is because RecyclerView ALWAYS set:
setFocusableInTouchMode(true);
this is hard coded in its constructor.
so if you apply those attribute in your XML for a RecyclerView
android:focusable="false"
android:focudeableInTouchMode="false"
that would be useless, you end up with recycleView.isFocusable() == true...
so the most elegant solution is to disable foucusable for RecyclerView's Parent.
<LinearLayout
android:focusable="false"
android:focusableInTouchMode="false"
android:descendantFocusability="blocksDescendants"
...
>
<android.support.v7.widget.RecyclerView
...
/>
/>
or you can just simply setFocusable(false)

Check out clearFocus() from here Android Dev Doc.
You can set a DrawerListener to your navigation drawer and use the onDrawerStateChanged() or some of the other options from here to call clearFocus() on your RecyclerView.

Adding android:descendantFocusability="blocksDescendants" can restrict scroll to recylerview but if you have edittext inside your layout, this property can block the focus of that particular edittext too. So if you are using this property please make sure you are removing this property in your kotlin/java class once the layout loaded.
parentLayout?.descendantFocusability = FOCUS_BEFORE_DESCENDANTS
(view as ViewGroup).descendantFocusability = FOCUS_BEFORE_DESCENDANTS

Just add the following code in your linear layout, works 100% ` android:descendantFocusability="blocksDescendants"
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent">

Related

How to scroll whole activity

I wrote an application and home page is LinearLayout and is in Fragment. In this LinearLayout there are two RecyclerViews and SearchBar etc. I want to scroll whole activity. I spent 2 days for it but cannot succeed. How can I do that in easy way? There are lots of adapters and connections in that LinearLayout. How can I achieve that without broke any code.
I want to scroll whole activity.
Thanks in advance.
Set Scroll/NestedScrollView as a parent view in xml to scroll whole layout.
Add below attribute in recycler view to stop recycler scroll:
android:overScrollMode="never"
Try below code:
<android.support.v4.widget.NestedScrollView
android:id="#+id/navigation_nested"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:overScrollMode="never">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="#dimen/margin_15">
<TextView
android:id="#+id/video_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_15"
android:drawableLeft="#drawable/ic_video_tutorial"
android:drawablePadding="#dimen/margin_20"
android:fontFamily="#font/poppins"
android:paddingLeft="#dimen/text_15"
android:text="#string/videos"
android:textColor="#color/blackTextColor"
android:textSize="#dimen/text_15" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
You just add this line:
//java
yourRecyclerView.setLayouManager(new LinearLayoutManager(this));
//kotlin
yourRecyclerView.layoutManager = LinearLayoutManager(this)
or from xml:
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Notice that there is not only LinearLayoutManager. If you are using a Grid RecyclerView than you might want to use GridLayoutManager
I solved the problem.
Just write ScrollView over the LinearLayout
<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">
<LinearLayout>
.....
</LinearLayout>
</ScrollView>

Not able to move view up with keyboard Android

I have gone through various answers but was unable to move my view up when keyboard is visible. I have a edittext in recyclerview on whose focus keyboard is visible.I want the tv_character_limit text to be moved up when keyboard is visible. I need the character limit text to be shown elevated with a z-index. I have set the windowSoftInputMode to adjustResize for the activity and for the fragment in view pager i have added the java code:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
Thank you for help.Below is my layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rel_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/pink">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_post" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_status"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<TextView
android:id="#+id/tv_character_limit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginRight="10dp"
android:layout_marginBottom="30dp"
android:text="#string/character_limit"
android:textColor="#color/tap_to_type"
android:textSize="15dp" />
</FrameLayout>
use,
android:windowSoftInputMode="adjustPan"
in the manifest file for your activity.
ex :
<activity
android:name=".View.Activity.yourActivity"
android:windowSoftInputMode="adjustPan"/>
Instead of FrameLayout, use RelativeLayout and when your EditText gets focus, just change the position of TextView by calling the RelativeLayout class method similar to layout_above.

How to make the last footer fixed (ListView)

I have a ListView with 2 footer views. The first one is a bunch of TextViews, while the second one is a button. I'm trying to fix the second footer so that it always shows at the bottom of the screen.
I tried the following:
1. alignParentBottom = true
2. layout_gravity="bottom"
3. footerView2.bringToFront()
And combinations of the above. But none of them worked out. How can I achieve this?
UPDATE
I shouldn't have added the View which I always want on the screen (fixed) as footer.
I just added the wannabe-fixed-view with the listView. Did alignParentBottom = true and also view.bringToFront(). It worked out for me.
Separate your ListView and bottom footer. The idea is:
<?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">
<Button
android:id="#+id/btn_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Bottom button"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/btn_bottom"/>
</RelativeLayout>
Create separate xml file which contain all textviews and button you want as footer part.Then bind this xml as a footer of listview. You can binf footer with listview by this method: listview.addFooterView(footerView);
For example:
View footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.listviewfooter, null, false);
spDate = (Spinner) headerView.findViewById(R.id.spdate);
llDatepicker = (LinearLayout) headerView.findViewById(R.id.lldatepicker);
rvDatepicker = (RecyclerView) headerView.findViewById(R.id.rvdatepicker);
rvTimepicker = (RecyclerView) headerView.findViewById(R.id.rvtimepicker);
lvAddress.addFooterView(footerView);
If you are Trying to add a View which will be shown even if we scroll the List Up or Down... u can do something like this. I had done this before But as a header.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/new_container"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOAD EARLIER MESSAGES"
android:id="#+id/loadMore"
android:background="#90F58220"
android:visibility="visible"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listContainer"
android:layout_below="#+id/loadMore"
android:layout_above="#+id/container">
<ListView
android:id="#+id/messagesContainer"
android:transcriptMode="normal"
android:dividerHeight="0dp"
android:divider="#null"
android:stackFromBottom="true"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/container"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:background="#ffffff"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:layout_height="wrap_content" >
</RelativeLayout>
The LoadMore Button is always visible on the Top of the List...
To add it to footer, Just Change
android:layout_below="#+id/loadMore"
android:layout_above="#+id/container"
to
android:layout_above="#+id/loadMore"

Adding buttons below GraphView, all inside a Fragment

I have been trying to add Button's below a GraphView, and all these elements are part of a Fragment. Tried many approaches but none of them worked properly.
This is the layout file for the Fragment (fragment_graph.xml).
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.nma.util.sdcardtrac.GraphFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph_fragment_layout"
android:orientation="vertical"
/>
</FrameLayout>
And this is the Java code dynamically adding a graph and button, placed in the Fragment's onViewCreated (View view, Bundle savedInstanceState).
storageGraph = new LineGraphView(getActivity(), graphLabel);
storageGraph.addSeries(graphSeries); // More config calls follow
...
LinearLayout view = (LinearLayout)getView().findViewById(R.id.graph_fragment_layout);
Button button = new Button(getActivity());
button.setText("Test");
view.addView(storageGraph);
view.addView(button);
The Button is not visible though I have set orientation to vertical for the LinearLayout containing it.
EDIT - solved!
I found that nesting the graph under its own LinearLayout and the buttons under another LinearLayout, and both of these wrapped in a LinearLayout fixed the problem! The LinearLayout containing the graph must be weighted (I chose a weight of 0.8).
Layout file looks like:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.nma.util.sdcardtrac.GraphFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph_fragment_wrap"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:id="#+id/graph_fragment_layout"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/graph_buttons"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_navigation_previous_item"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_navigation_next_item"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
I've just tried it and it works. Perhaps your graph is taking all the available space, so added button is below the screen? Try to wrap your LinearLayout into a ScrollView and see if there is a button in the bottom.

How can I set attribute onClick to a ScrollView?

I have the following layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<LinearLayout
android:id="#+id/answerMainFrame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:isScrollContainer="true"
android:onClick="toQuestion" >
<ImageView
android:id="#+id/answer_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="#string/question_img_cd" />
<TextView
android:id="#+id/answer"
style="#style/Question" />
</LinearLayout>
Sometimes the ScrollView is too small and won't fill the whole screen but still I want to call the method toQuestion() clicking anywhere on the screen.
I've tried setting android:layout_height="wrap_content" to the LinearLayout and android:onClick="toQuestion" to the ScrollView but same result.
You could try to let the LinearLayout implement the onClick attribute and then set:
android:fillViewport="true"
to the ScrollView.
I had a similar issue, and simply got rid of the scrollview itself. Instead, I directly inserted the TextView itself, with the constraints previously put on the ScrollView. The whole purpose of my ScrollView was to scroll in the TextView, not among several items, so it seemed a more elegant way to do.
Add a (vertical) scrollbar to my textview in activity.hmtl :
<TextView
android:id="#+id/tvInfo"
(...)
android:scrollbars="vertical"
/>
Add this to the onCreate method:
oTV = (TextView) findViewById(R.id.tvInfo);
oTV.setMovementMethod(new ScrollingMovementMethod());
No need to write the scrolling response. I like that.
I found the solution here (thx Juned Mughal) :
http://www.android-examples.com/make-textview-scrollable-in-android-programmatically/
To make the scroll view fill the screen change:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
to
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/white" >
and for the click put this in your onCreate method:
((ScrollView) findViewById(R.id.scrollView))
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toQuestion();
}
});

Categories

Resources