Centering an element on the current Viewport in a Scrollview - android

I want to display a dot, which is always in the physical center of my device, even when I scroll the content around it, which is in a scrollview. This is my current code, but the dot is missing here, because I don't know where to place it. The scrollposition is only set via code, so it wouldn't be a problem, if the centered element blocks the scrollview.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:deviceIds="wear_square"
android:gravity="center">
<android.support.v4.widget.NestedScrollView
android:id="#+id/vertsv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<HorizontalScrollView
android:id="#+id/horisv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/map" />
</LinearLayout>
</HorizontalScrollView>
</android.support.v4.widget.NestedScrollView>
Solution :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:deviceIds="wear_square">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:id="#+id/vertsv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<HorizontalScrollView
android:id="#+id/horisv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/map" />
</LinearLayout>
</HorizontalScrollView>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_my_location_black_24dp" />
</RelativeLayout>

Put your dot below anything else in your layout. Also you should use FrameLayout/RelativeLayout instead of LinearLayout as a top parent container.

Related

FrameLayout after ScrollView allways on bottom

I need to made layout that will contain ScrollView with some dynamic length text and Button in FrameLayout after(below). With long text all is okay, but when text is short button stuck right below bottom of ScrollView text part. I want a FrameLayout in the bottom of the screen when text is short and right below ScrollView text part when text long .
Can it be done?
My XML
<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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".TeacherFullActivity"
tools:showIn="#layout/app_bar_teacher_full">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
layout="#layout/listview_element_teachers"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/job_title"
style="#style/TitleMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/teacher_job_title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/job_dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/subject_title"
style="#style/TitleMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/teacher_subject_title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/subject_dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/workPlace_title"
style="#style/TitleMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/teacher_workplace_title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/workplace_dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingLeft="35dp"
android:paddingTop="5dp"
android:paddingRight="35dp"
android:paddingBottom="5dp">
<Button
android:id="#+id/teacher_external_link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/btn_rounded"
android:text="#string/open_link"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="17sp"
android:textStyle="bold"
/>
</FrameLayout>
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
You can use the android:fillViewport attribute of ScrollView to accomplish this.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</FrameLayout>
</LinearLayout>
</ScrollView>
What this does is make it so that, in the case where the contents of the LinearLayout are not large enough to fill the screen, the LinearLayout is stretched to fill the screen anyway. That gives the FrameLayout room to grow (based on its layout_weight attribute), which in turn lets the Button float to the bottom of the screen (because of its layout_gravity attribute).
In cases where the contents of the LinearLayout are larger than the screen, no stretching happens. The button is pushed off-screen, but appears when you scroll down (with no space between it and the text).
You can try this code for your approach:
<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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".TeacherFullActivity"
tools:showIn="#layout/app_bar_teacher_full">
<ScrollView
android:id="#+id/scrollview"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraintTop_toTopOf="parent"
app:constraintStart_toStartOf="parent"
app:constraintEnd_toEndOf="parent"
app:constraintBottom_toTopOf="#id/frame">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
layout="#layout/listview_element_teachers"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/job_title"
style="#style/TitleMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/teacher_job_title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/job_dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/subject_title"
style="#style/TitleMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/teacher_subject_title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/subject_dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/workPlace_title"
style="#style/TitleMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/teacher_workplace_title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/workplace_dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<FrameLayout
android:id="#+id/frame"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="35dp"
android:paddingTop="5dp"
android:paddingRight="35dp"
android:paddingBottom="5dp"
app:constraintTop_toBottomOf="#id/scrollview"
app:constraintStart_toStartOf="parent"
app:constraintEnd_toEndOf="parent"
app:constraintBottom_toBottomOf="parent">
<Button
android:id="#+id/teacher_external_link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/btn_rounded"
android:text="#string/open_link"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="17sp"
android:textStyle="bold"
/>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Explaination:
What i did was took FrameLayout containing Button out side of ScrollView so that it can always be at bottom of the Container.

Scrollview top and bottom

I'm building and android application using android studio and I'm using scroll view in my layout, but the problem is that the scroll view doesn't start after the action bar and leaves huge white space at the bottom (as you can see in the screenshots below).
and here's my code:
<?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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="false"
android:background="#fefcf8"
android:orientation="vertical"
android:weightSum="1">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp"
android:orientation="vertical">
<ImageButton
android:id="#+id/info1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="2dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/info1" />
</LinearLayout>
.
.
.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp"
android:orientation="vertical">
<ImageButton
android:id="#+id/info6"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="2dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/info6" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:background="#3a3c3a"
app:menu="#menu/navigation"
app:itemTextColor="#ffffff"/>
</LinearLayout>
As you can see my xml above, i didn't any padding or margin. Yet there's a white space at the bottom and the scroll view starts at the very top of the activity not after the action bar. How to solve this?
Try by putting this inside your ScrollView or FrameLayout containing ScrollView
<ScrollView
...
app:layout_behavior="#string/appbar_scrolling_view_behavior"
...>

how to focus an activity to its top elements

i have an activity in which, on top i have a image slider, below this a layout in the form of grid which covers almost all of my phone's screen and below this at last i have a list view of of size approax half of my phone's screen.(i am using scroll view to scroll elements)
so altogether they are nearly of size twice of my phone's screen.
now whenever i start this activity, the screen automatically gets focused to the bottom part of my elements. in simple words, it does not show me the image slider and upper part of my grid view, it just show the lower part of grid view and the list view.
so i need to scroll it up manually when i start this.
i searched a lot but didn't get any way to focus it automatically to top elements of my activity. please suggest me how to proceed now.
thank in advance.
this is my xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.gehlot.nitin.kitchenspices.Main2Activity"
android:id="#+id/scrollView"
tools:showIn="#layout/app_bar_main2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_weight="1.0"
android:id="#+id/viewPager"/>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1.0"
android:text="Shop catagory"
style="#style/textStyle"
android:layout_gravity="center"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical"
android:background="#drawable/back"
android:layout_weight="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1.0">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:background="#drawable/back"
android:id="#+id/img1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:background="#drawable/back"
android:id="#+id/img2"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="#drawable/back"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:id="#+id/img3"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1.0">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="#drawable/back"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:id="#+id/img4"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:background="#drawable/back"
android:layout_margin="5dp"
android:id="#+id/img5"/>
<ImageView
android:adjustViewBounds="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:scaleType="fitXY"
android:background="#drawable/back"
android:layout_margin="5dp"
android:id="#+id/img6"/>
</LinearLayout>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/lv_main"
android:layout_below="#id/img4">
</ListView>
</LinearLayout>
</ScrollView>
Try to add this line to your direct child of your ScrollView in xml. It prevents the auto scrolling to bottom part.
android:descendantFocusability="beforeDescendants"
Example:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:orientation="vertical">
//children views
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Add this to your root layout in xml:
android:id="#+id/root_layout"
In your Acivity.java add:
ViewGroup layout = (ViewGroup) findViewById(R.id.layout);
layout.requestFocus();

LinearLayout inside ScrollView is autoscrolled to bottom

I've got a ScrollView which occupies the bottom half of the screen.
Inside this ScrollView a put a LinearLayout (vertical) with a lot of content.
When I start the activity, the content somewhy automatically scrolls itself down so that it starts at the top of the window. But I need it to start at the top of the ScrollView (i.e. at the center of the window).
What am I doing wrong?
<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"
tools:context="ru.intrigue.activities.EditActivity"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:visibility="visible">
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView2"
android:background="#color/colorDarkBack">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<!-- content -->
</LinearLayout>
</ScrollView>
</LinearLayout>
you can do like below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.45"
android:orientation="vertical">
<ImageView
android:id="#+id/frag_home_iv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/demo" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.55"
android:fadeScrollbars="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.26"
android:gravity="center"
android:orientation="horizontal"
android:padding="#dimen/padding_5dp"
android:weightSum="1">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:src="#drawable/demo" />
<TextView
android:id="#+id/fragment_home_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:paddingLeft="#dimen/padding_5dp"
android:text="#string/demo"
android:textColor="#color/BlackColor" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Your problem solve by below code :
When open your activity call below code:
your_scroll_view.post(new Runnable() {
#Override
public void run() {
your_scroll_view.fullScroll(View.FOCUS_DOWN);
}
});
Best of luck
You can achieve the desired results by using layout_weight property:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- your content here-->
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- your content here-->
</LinearLayout>
</ScrollView>
</LinearLayout>
Let me know if it helped you.

Scroll a TextView out the screen

I have a bit of a predicament.
My Layout is currently this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:isScrollContainer="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/item_shipping_shipping_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|left"
android:padding="#dimen/margin_16"/>
<View
android:id="#+id/line43"
android:layout_width="match_parent"
android:layout_height="#dimen/line_height"
android:background="#color/light_gray"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.v7.widget.RecyclerView
android:id="#+id/item_shipping_fragment_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:isScrollContainer="true"/>
</LinearLayout>
The TextView is dynamic content set by the server and it can be really long sometimes.
The problem I am having is the TextView can take up the whole screen. This XML is a fragment that is in a TabLayout with a collapsable toolbar. So when I scroll the content I would like the TextView to scroll out the screen, if this is possible. I tried to embed the Textview in a scrollView but this doesn't work as you see about?
Got any ideas how I can still have the CollapsableToolbar collapse as well as scroll the Textview out the screen to reveal the RecyclerView?
Thank you
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:isScrollContainer="true">
<TextView
android:id="#+id/item_shipping_shipping_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|left"
android:padding="#dimen/margin_16"/>
</android.support.v4.widget.NestedScrollView>
<View
android:id="#+id/line43"
android:layout_width="match_parent"
android:layout_height="#dimen/line_height"
android:background="#color/light_gray"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/item_shipping_fragment_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:isScrollContainer="true"/>
</LinearLayout>

Categories

Resources