I am trying to use horizontalscrollview from Right to Left.
My targetSDK is 17.
I have inside the manifest
<application android:supportsRtl="true">
My layout is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/lay_main_firma_outer"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1.0"
android:background="#color/bg_firma" >
<HorizontalScrollView
android:id="#id/lay_main_firma_hscroll"
android:fillViewport="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0" >
<LinearLayout
android:id="#id/lay_main_firma_objekti"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"/>
</HorizontalScrollView>
I add objects at runtime to the linearlayout. At runtime I also set RTL with
private void rightToLeft_post17(HorizontalScrollView hscv){
hscv.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
LinearLayout lay=(LinearLayout) hscv.findViewById(R.id.lay_main_firma_objekti);
if(lay!=null){
lay.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
}
but it is not working.
Any idea what might be wrong?
Thanks
Try to invalidate your layout:
if(lay!=null){
lay.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
lay.invalidate();
}
First, hide your parent layout of horizontalscrollview before loading the items in horizontalscrollview.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/lay_main_firma_outer"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1.0"
android:setVisibility="gone"
android:background="#color/bg_firma" >
When the uploading gets done,then programatically make your linearlayout visible like this:
findViewById(R.id.lay_main_firma_outer).setVisibility(View.VISIBLE);
Related
I want to remove the shadow(scrollbartrack) shown below scrollbarthumb.
But unable to do it. I tried to set android:scrollbarTrackVertical="#null"
Please ignore the black part.
You can do it using XML or code.
In XML:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/yourScroll"
android:scrollbars="none" <!-- add this -->
>
or in your code:
yourScrollView.setVerticalScrollBarEnabled(false);
yourScrollView.setHorizontalScrollBarEnabled(false);
Edit
You have changed the question with your new image , for that task add drawable image as your requirement to android:scrollbarThumbVertical
<ScrollView
android:scrollbarThumbVertical="#drawable/line" <--like this-->
android:id="#+id/my_scroll"
android:layout_width="match_parent"
android:layout_height="500dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp">
</LinearLayout>
</LinearLayout>
</ScrollView>
Note : My drawble image contain transparent area(in left and right sides) ,thats why its bit right from the edge
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">
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.
I get the Exception:
java.lang.IllegalStateException: ScrollView can host only one direct child
This is my layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/bg_listgrey"
android:scrollbars="none" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/bg_listgrey" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rl1">
<ImageView
/>
<TextView
/>
<TextView
/>
<TextView
/>
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rl2"" >
<ImageView
/>
<TextView
/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
The same layout worked fine in case of an Activity. Where as it gives the exception when using in a Fragment.
MainActivity:
FragmentTransaction t = this.getSupportFragmentManager()
.beginTransaction();
t.add(R.id.frame, new mFragment());
t.commit();
Thank you
EDIT:
Bit if i wrap this ScrollView in a FragmeLayout, it works fine.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/bg_listgrey" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none" >
.......
.......
.......
</ScrollView>
</FramLayout>
The same layout worked fine in case of an Activity. Where as it gives
the exception when using in a Fragment.
The layout will work just fine as the content view for an Activity(but try to add another view to the ScrollView and see how it goes;) ) and it will work as well for a Fragment, you just don't use it well. This:
t.add(R.id.frame, new mFragment());
will add the Fragment's view(the one created in onCreate) to the ScrollView(the ScrollView has the id R.id.frame), meaning that you'll have two views in the ScrollView(which is not allowed): the RelativeLayout and the root of the Fragment's view. The addView methods of the ScrollView class will check to enforce that you end up with a single child in the ScrollView.
Bit if i wrap this ScrollView in a FragmeLayout, it works fine.
This is normal as you add the Fragment's view to the parent FrameLayout and not to the 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();
}
});