How to make a linear layout scrollable? - android

Hey all - is it possible to make an entire linear layout scrollable when it needs to be? (when all of the elements in the layout don't fit on the main screen)?
I know it is doable with views, etc...but is there a way to incorporate everything on the layout to be scrollable at the same time?
Maybe scrollable is not the right term...basically - if one of the elements (a button in this case) doesn't entirely make it onto the main screen of the phone and I need to slide a finger down to access it...if that makes sense.

A LinearLayout is a subclass of View, so anything you can do with a View you can with a Linear Layout.
So just use ScrollView with a single LinearLayout as a child

Just exemplifying what the other guys are talking about
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|left"
android:orientation="vertical" >
Here is your layout!
</LinearLayout>
</ScrollView>

Yes you can make your whole view scrollable using ScrollView but since you can only contain one main child so you will have to add another LinearView View inside ScrollView and put your other views inside this.
-Main View
-- ScrollView
--- LinearView
---- Sub View 1
---- Sub View 2
---- Sub View 3
....

you can make any layout scrollable. Just under <?xml version="1.0" encoding="utf-8"?> add these lines:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
and at the end add </ScrollView>
example of a non-scrollable activity:
<?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"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:verticalScrollbarPosition="right"
tools:context="p32929.demo.MainActivity">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp"
android:id="#+id/textView"
android:textSize="30sp" />
</RelativeLayout>
After making it scrollable, it becomes like 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">
<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"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:verticalScrollbarPosition="right"
tools:context="p32929.demo.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp"
android:text="TextView"
android:textSize="30sp" />
</RelativeLayout>
</ScrollView>

Related

How to achieve that background image doesn't get stretched on linear layout

I have a linear layout with a background image.
I need to scroll view before I create the linear layout.
After I do that, my image on linear layout is getting stretched.
Can you help me to create my image so it doesn't get stretched?
This is my code on .xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<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:layout_width="match_parent"
android:layout_height= "wrap_content"
android:gravity="center_horizontal"
android:background="#drawable/bg_image_login"
android:orientation="vertical"
android:windowSoftInputMode="stateVisible|adjustResize"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.fania.suara_project.LoginActivity">
<ImageView
android:id="#+id/haha"
android:background="drawable/bg_image_login"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Help me please. Thank You :)
put scrollview background
android:background="#drawable/bg_image_login"
instead of Linearlayout.

How to force SurfaceView to occupy specific section othe screen

I am trying to use SurfaceView. So I created a SurfaceView widget in the layout file as shown below in the code. The problem I have is how to set a weight to the SurfaceView?
As shown below in the code I set :
android:layout_weight=".4"
but still the SurfaceView occupies the eintire screen, what i want is to make the SurfaceView occupies only 40% of the screen. I also refered to a post in this link SurfaceView in Layout
but it does not show how to set the weight to the SurfaceView from the xml file.
Please let me know how to set weight to the SurfaceView from the xml file.
code:
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.surfaceview_00.MainActivity">
<SurfaceView
android:id="#+id/mainActivity_surfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"/>
</RelativeLayout>
update:
I also tried the below code but still i got one SurfaceView that occupies the whole screen. I expected after running the below code is to have two SurfaceViews each occupies half of the screen, but i got only one
code:
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.surfaceview_00.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">
<SurfaceView
android:id="#+id/mainActivity_SurfaceView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<SurfaceView
android:id="#+id/mainActivity_SurfaceView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
RelativeLayout does not support layout_weight. You need to use the LinearLayoutfor that.
Read more at https://developer.android.com/guide/topics/ui/layout/linear.html
But basically change it to something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_height="match_parent"
android:weightSum="1">
<SurfaceView
android:id="#+id/mainActivity_surfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"/>
</LinearLayout>
android:layout_weight is only used by LinearLayout. Since you use RelativeLayout it is ignored.
SurfaceView doesn't belong to App's view hierarchy. It creates separate layer on the background, and position it depending on its size. All other views are displayed over SurfaceView. To do what you want you need to use TextureView.

How to Scroll A Part of An Activity

I have a question that i couldn't Find Any Answer For it.
How can I Make An Layout Scrolling And Make Another Layout that doesn't Scroll?
On the other hand i want to make a layout Including Some buttons that scrolls if necessary and i want to put Two buttons at the bottom of the screen and i don't want them to disappear when scrolling.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.myname.myapp.MainActivity">
// a bunch of Buttons and TextViews
</RelativeLayout>
</scrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
// two Moveless Buttons
</RelativeLayout>
But it doesn't Work.
Your ScrollView has width and height that matches the parent. This means it takes up the entire space available leaving nothing for the RelativeLayout. You probably want to wrap what you already have inside a LinearLayout and then use layout_weight attribute to divide the space up.
I'm not sure I understand what You want to do. But try this and give feedback
( code example not tested in Android Studio)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.myname.myapp.MainActivity">
// a bunch of Buttons an TextViews
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp (for example)>"
android:layout_alignParentBottom="true">
// two Moveless Buttons
</RelativeLayout>
</RelativeLayout>
Make your root a vertical linearlayout with a scrollview, to hold the scrollable views, and a RelativeLayout, to hold your buttons.
You can take advantage of the LinearLayout's weighting ability by applying a weight of 1 to the Scrollview so it takes up as much room it can while the button-holding-RelativeLayout will be the size of your biggest button.
Here is an example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.myname.myapp.MainActivity">
// a bunch of Buttons an TextViews
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
// two Moveless Buttons
</RelativeLayout>
</LinearLayout>
HTHS!

Android - I want the listview rows to fill the screen

I want my listview rows to fill all the screen. For example if I have 4 rows I want the screen to be filled with all the rows taken even space within the screen. Right now the list only take 1/3 of screen.
activity_subject.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".SubjectListActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"/>
</LinearLayout>
row_layout.xml // the list row layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner_list_view"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/list_text_view"
android:textSize="25sp"
android:textStyle="bold"
android:padding="15dp"/>
</LinearLayout>
Try to change parent layout from LinearLayout to RelativeLayout.
Try this in ListView:
Make android:layout_height="0dip"
and add android:layout_weight="1"

I want to have a RelativeLayout and LinearLayout in the same layout

http://i.stack.imgur.com/Utrxn.png
I made a picture showing what I want my layout to look like. Basically it's a RelativeLayout on top of a LinearLayout. I set the layout_weight=3 for relative, and layout_weight=1 for linear. This is not working so far. Can anyone tell me whats wrong with my XML?
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="kim.albert.marveleventtracker.EventDescriptionActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"></RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal">
</RelativeLayout>
You should change top layout is LinearLayout, set orientation is vertical

Categories

Resources