Fixed Button in scrollview - android

I want to make a view( a button actually) which is inside a scrollview , but when the user scolls down and the button is going up, it moves up only till it reaches the top of the visible screen and then stays there like a fixed header until the user scrolls up again and then it returns to its original position.
I have given the screens for understanding ;
At Start
Then

Change you parent viewgroup to RelativeLayout and then make a Button and ScrollBar as a direct child of this RelatieveLayout.

You can simply achive this by
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
------put your code here-----/>
<Button
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>

Related

How do I fix a layout at bottom and at top without hiding elements on Android?

I'm having problems when using RelativeLayout, ScrollView and some LinearLayout to get some elements always on top and something scrollable.
This is my XML
https://pastebin.com/Uai2UynP
(I don't know why it got separated in this post but it's the same code)
The point is, when i add the android:alignParentBottom="true" to the RelativeLayout with the "always on top bar", it hides me a part of the ScrollView above. This can be solved adding a android:PaddingBottom="[something]" to the ScrollView above, but doing this it hides me something at the top, since I basically have the LinearLayout with email, gold and coin that i want too always on top.
Use RelativeLayout as main layout and use android:layout_alignParentTop="true" in your layout which you want to place top and use android:layout_alignParentBottom="true" in your next layour which you want to set bottom of your layout. I am giving you a Example,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true">
</RelativeLayout>
</RelativeLayout>

Android not able to scroll up

I have a login layout with two EditText and a Button. When I'm typing some text in EditText, as the keyboard open it overlap the some of my layout. I try to scroll up but not able to scroll up layout to be visible. I'm using Relative Layout to create it. How to solve it?
Thank you.
Place your EditTexts and Button inside a ScrollView. Whatever is inside ScrollView can be scrolled. So your problem will be solved.
Note that ScrollView can host only one child. So you have to place all your Views inside a ViewGroup like LinearLayout or RelativeLayout
Although Julia Zhao's answer is correct, it uses RelativeLayout. If you use RelativeLayout you have to do more work to make your Views appear one below the other. So I suggest you to use LinearLayout with android:orientation="vertical" inside it. It will automatically place one View below the other without any extra effort. So you won't issues like one View overlapping on other.
<?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="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UserName"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Password"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Login"/>
</LinearLayout>
</ScrollView>
Try nesting everything in a ScrollView, hope this helps.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#id/RLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
xmlns:android="http://schemas.android.com/apk/res/android"
>
// Your code
</RelativeLayout>
</ScrollView>

How to display a view on top of another view in same activity

I am have an activity in android application . It displays a view (lets say CurrentView)with a button in the centre of the screen . I am setting the content of the activity using :
setContentView(R.layout.layout_xml_file);
The layout xml file defines the button.
My requirement is that , when user clicks on the button , i need to show another view (lets say NewView)containing a listView on Current View.
The NewView transition should happen like this :
It should start from bottom of the screen and should take some proportion of the screen , say 3/4 or 1/2 . I mean the view's height should be configurable .
This NewView should be in the same activity . I do not want to create another activity for displaying it.
I don't understand which android widget is should use for holding the NewView
Thanks. Your help is appreciated ...
Take a Relative Layout align to bottom and set its height what you want and inside Relative Layout Take Listview after that setVisibility gone. And when click button in activity setVisibility Visible.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="128dp"
android:id="#+id/button" />
<RelativeLayout
android:id="#+id/rel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#f0f0f0"
android:layout_below="#+id/button"
android:visibility="gone">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:divider="#000"
android:dividerHeight="1dp"/>
</RelativeLayout>
</RelativeLayout>
use LinearLayout - layout_weight attribute in layout.xml
View.setVisibility(View.VISIBLE or View.GONE)
perhaps use FrameLayout for both views parent layout. Initially the list should be inivisble and button visible.
I think you are looking something like this library ->
https://github.com/umano/AndroidSlidingUpPanel

Layout changes animations Android

I have a RelativeLayout which has a nested empty FrameLayout and a ListView below the FrameLayout. I am adding a view to the FrameLayout with animation (setting android:animateLayoutChanges="true" in xml) and I am doing this because I want it to appear at the top of my window. So it appears with the default fade in animation. The ListView on the other hand moves down, but it is not animated. What can I do for the ListView to slide down to its position, because this is not very good looking. If I have to change the way I define my views in the layout, that's ok also. I will consider every suggestion. Thx
Here is my code:
<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"
>
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"></FrameLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/frame"
/>
</RelativeLayout>

Activity does not scroll when rotating screen

when I turn the phone and the screen is rotating I cannot see the whole screen anymore. This is ok but I cannot scroll! Do I have to set some flag or property for scrolling? This is part of my xml...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="*">
...
</TableLayout>
<TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
...
</TableLayout>
</LinearLayout>
Thanks!
You have to put your complete layout inside a ScrollView in order for it to scroll.
It scrolls if your layout height is more than the screen height, which happens generally in landscape mode.
In your case put the LinearLayout inside a ScrollView, since ScrollView can only have 1 child.
You must know that in order to use an scrollView , you must put only one component inside (maybe a linearLayout) .Maybe that's your problem if you are trying to put in more components at that level.

Categories

Resources