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.
Related
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.
I'm new to Android have this problem making a scrollview that is supposed to scroll over a ListView of 10+ items.
<?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: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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.naqishop.naqi.MainActivity"
tools:showIn="#layout/activity_main">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:entries="#array/options" />
</ScrollView>
</LinearLayout>
The problem is that I can not extend scroll views to show more that one item at a time. I tried to tamper with layout_height of the scrollview (like assigning the value to 500dp)
Also I tried adding
android:fillViewport="true"
as suggestd here
but it did not change the height. So appreciate your hints.
This is because you've set android:layout_height="wrap_content". Replace that with
android:layout_height="match_parent" and it'll take up the whole height.
I have an xml file which contains two RelativeLayouts. The trouble is that the second Relativelayout does not go up to the display limit. It looks like a border around the layout.
How can I display the second layer also on the border of the display?
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:background="#color/colorAccent"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="newservice"
tools:showIn="#layout/activity_newservice">
<RelativeLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark"
android:layout_alignBottom="#+id/viewmap"
android:layout_centerHorizontal="true">
</RelativeLayout>
</RelativeLayout>
Remove this line on the top layout:
android:paddingTop="#dimen/activity_vertical_margin"
You are putting a padding on the top, so the second layout will not fill the first like you want.
Regards!
Remove your
android:paddingXXX from parent RelatvieLayout
that is making issue
<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"
android:background="#color/colorAccent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="newservice"
tools:showIn="#layout/activity_newservice">
<RelativeLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark"
android:layout_alignBottom="#+id/viewmap"
android:layout_centerHorizontal="true">
</RelativeLayout>
</RelativeLayout>
Try this
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
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>