ScrollView - How to enable horizontal scrolling in ScrollView - java

So I have a ScrollView which has a TextView inside. When the String for the text is longer than the screen width, It simply shifting in the next line. I want to avoid the new lining by adding HorizontalScrolling in ScrollView. Also, I want to make the text selectable so the user can copy the text. Is it possible?
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="15dp"
android:ellipsize="end"
android:textIsSelectable="true"
android:justificationMode="inter_word"
android:lineSpacingExtra="5dp"
android:textSize="15sp"
tools:targetApi="o" />
</ScrollView>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:scrollbars="vertical">
<HorizontalScrollView
android:layout_width="match_parent" android:layout_height="wrap_content">
<FrameLayout android:layout_width="320dp"
android:layout_height="match_parent"
android:background="#000000"/>
</HorizontalScrollView>
</ScrollView>

Try this:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:scrollHorizontally="true"
android:text="Horizontal scroll text view"/>
</HorizontalScrollView>

Related

How to slow down the Scrolling speed of Recyclerview both in vertically and horizontally?

In My Android project I am using RecyclerView inside one layout. Here is the code given below-
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
tools:context=".ui.reader.pageview.PageViewFragment">
<TextView
android:id="#+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:overScrollMode="never"
android:id="#+id/rv_page_view"
android:layout_below="#id/pb"
android:layout_above="#+id/ll_page_controller_bottom"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:background="#color/colorWhite"
android:orientation="vertical"
android:id="#+id/ll_page_controller_bottom"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#DADADA" />
<org.adw.library.widgets.discreteseekbar.DiscreteSeekBar
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:id="#+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:dsb_indicatorColor="#color/colorOrange"
app:dsb_trackColor="#color/colorOrange"
app:dsb_progressColor="#color/colorOrange"
app:dsb_rippleColor="#color/colorGray"
app:dsb_min="0"
app:dsb_max="100"/>
<TextView
android:layout_marginTop="-10dp"
android:layout_marginBottom="5dp"
android:text="1"
android:id="#+id/tv_page_number"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_below="#id/pb"
android:layout_above="#id/ll_page_controller_bottom"
android:id="#+id/view_touch"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<RelativeLayout
android:visibility="invisible"
android:id="#+id/rl_settings"
android:layout_alignParentEnd="true"
android:layout_below="#id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<include layout="#layout/reader_settings"/>
</RelativeLayout>
<TextView
android:layout_margin="10dp"
android:layout_below="#id/pb"
android:id="#+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16sp"
android:visibility="invisible"
android:layout_above="#id/ll_page_controller_bottom"/>
</RelativeLayout>
Now, for rendering a lot of data when the users scrolls too much fast the screen got hanged. So, I want to slow down the speed of the scrolling.
I have tried the following solution-
How can i control the scrolling speed of recyclerView.smoothScrollToPosition(position)
It works well with horizontal scrolling but when I switch to vertical view then It doesn't work.
Is there any way to solve this?

Android Margin and Padding aren't working for RecyclerViews inside ScrollViews

When I place a RecyclerView as a child of a ScrollView, marginBottom and paddingBottom doesn't work. Setting these values for the parent LinearLayout doesn't affect anything either. It seems that the ScrollView doesn't scroll to the bottom because the ScrollBar doesn't reach the bottom. How can I fix this bug?
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<!--To avoid scroll view from scrolling to the bottom automatically-->
<View android:layout_width="match_parent" android:id="#+id/focus_view" android:layout_height="0dp" android:focusable="true" android:focusableInTouchMode="true"><requestFocus/></View>
<ScrollView
android:id="#+id/sv_categories_statistics"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="4dp">
<LinearLayout
android:id="#+id/ll_categories_statistics_filters_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp">
<LinearLayout
android:id="#+id/ll_categories_statistics_select_date_fragment_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/border_primary_light"
android:padding="4dp"
android:layout_marginEnd="4dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/border_accent"
android:padding="4dp">
<Spinner
android:id="#+id/spn_categories_statistics_bill_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<com.github.mikephil.charting.charts.PieChart
android:id="#+id/pc_categories_statistics"
android:layout_width="match_parent"
android:layout_height="325dp"
android:layout_margin="4dp"/>
<fragment
android:id="#+id/fgm_categories_statistics_not_enough_data"
class="com.dolinsek.elias.cashcockpit.NotEnoughDataFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:text="#string/label_not_enough_data_for_statistic"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="8dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_categories_statistics"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
Have you tried clipToPadding
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_categories_statistics"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="50dp"
android:clipToPadding="false"/>

Android linearLayout children not expanding to fill parent

I have the following as a layout of mine:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MyActivity">
<EditText
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/bubble_b"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RatingBar
android:id="#+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:numStars="5"
android:rating="4"
android:stepSize="0.5"
android:layout_weight="1"/>
This makes my edittext and ratingbar be pushed up to the top and both of the heights are just 'wrap_content'. I have tried with RelativeLayouts as well and all that happens is the EditText takes up the entire screen and pushes the ratingbar off the screen.
Why are the layout weights not working as expected? If edittext weight is 3 and the ratingbar is 1 I would assume the edittext would take 75% of the screen, and so forth?
The background is a 9patch image so it should have no problem expanding either.
Thank you
I believe you want something like this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EditText"
android:ems="10"
android:id="#+id/editText"
android:layout_weight="0.53" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.53">
<RatingBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="#+id/ratingBar"/>
</RelativeLayout>
</LinearLayout>
use Relative Layout to center object:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MyActivity">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<EditText
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bubble_b"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<RatingBar
android:id="#+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:numStars="5"
android:rating="4"
android:stepSize="0.5"
android:layout_weight="1"/>
</RelativeLayout>
Change LinearLayout's
android:layout_height="wrap_content"
to
android:layout_height="match_parent".
Also, instead of setting "0dp" to EditText and RatingBar set it to match_parent.
[Update]
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MyActivity">
<EditText
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1">
<RatingBar
android:id="#+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:numStars="5"
android:rating="4"
android:stepSize="1" />
</LinearLayout>
</LinearLayout>

LinearLayout: 3 Elements, Top, Center, Bottom

I've the following xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/topFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="#+id/imageViewTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_00" />
<TextView
android:id="#+id/textViewTest"
android:layout_below="#+id/imageViewTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a Test"/>
</RelativeLayout>
<FrameLayout
android:id="#+id/bottomFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal" />
</LinearLayout>
The first FrameLayout contains a fragment, which contains some buttons.
The RelativeLayout contains for example an image (changig size) and a text.
The last FrameLayout contains also a fragment, which contains either some buttons or some text or an image.
My aim is, that:
- the first FrameLayout is on top (that works)
- the elements in the RelativeLayout are centered in the middle of the screen (doesn't work)
- the last FrameLayout is on the bottom (doesn't work)
It should look like this:
My previous attempts with android:gravity="center" in the RelativeLayout and android:layout_gravity="bottom|center_horizontal" for the second FrameLayout failed.
Change the LineareLayout to Relative and use layout_centerInParent for the middle layout and alignParentBottom for the last one.
The top View will be anchored to the top by default
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> //removed orientation as it won't be needed anymore
<FrameLayout
android:id="#+id/topFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="wrap_content" // change here
android:layout_height="wrap_content"
android:layout_centernInParent="true"> // here
<ImageView
android:id="#+id/imageViewTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_00" />
<TextView
android:id="#+id/textViewTest"
android:layout_below="#+id/imageViewTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a Test"/>
</RelativeLayout>
<FrameLayout
android:id="#+id/bottomFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" /> //here
</RelativeLayout>
Use relative layout and use android:layout_centerHorizontal="true" instead of gravity.
EDIT
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/topFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/topFragment">
<ImageView
android:id="#+id/imageViewTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_00"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/textViewTest"
android:layout_below="#+id/imageViewTest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a Test"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<FrameLayout
android:id="#+id/bottomFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>

How to center in ScrollView and LinearLayout

Please have a look at the following code
<ScrollView 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=".DisplayResult" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/zodiac1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/aries"
/>
<TextView
android:id="#+id/orTxt"
android:text="Or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/zodiac2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gemini"
/>
</LinearLayout>
</ScrollView>
I need to center everything inside the LinearLayout. I must use scrollview because when the app is 100% done, there will be lot of information which cannot be viewed by a single glance, without scrolling. How can make these center? Please help!
UPDATE
use any layout inside the scroll, no prob. But I need those elements to be in the same order (one after another) and centered.
you need to set gravity as centered for linear layout
< LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >
This one worked for me:
<LinearLayout 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"
android:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
Try using layout_gravity on your LinearLayout to center_horizontal:
<LinearLayout
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
<ScrollView 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=".DisplayResult"
scrollbars="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/zodiac1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/aries"
/>
<TextView
android:id="#+id/orTxt"
android:text="Or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/zodiac2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gemini"
/>
</LinearLayout>
</ScrollView>
#If you want to Linear layout in a center you need to write android:gravity="center_horizontal" in your linear layout.

Categories