Below is my layout. I want EditText to be on top of layout and I want VideoView to be vertically centered. EditText appears fine but I see my VideoView aligned below the EditText (not vertically centered). However if I remove android:layout_below="#+id/caption" then my VideoView is vertically centered but if the video is of larger height then it will come on top of EditText and I don't want that. How can I adjust VideoView such that it is both vertically centered and below EditText?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="5dp"
android:background="#FFFFFF"
>
<EditText
android:id="#+id/caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:maxLength="100"
android:hint="Hello"/>
<VideoView
android:id="#+id/previewvideo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_below="#+id/caption"
/>
</RelativeLayout>
Please check below code it's helpful for you
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10" >
<requestFocus />
</EditText>
<VideoView
android:id="#+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" />
Use a LinearLayout as the root ViewGroup and specify the layout_gravity for the VideoView with a vertical orientation. This will place the items top to bottom as they are ordered in the layout.
<LinearLayout
android:orientation="vertical"
...
>
<EditText
.../>
<VideoView
...
android:layout_gravity="center_vertical"/>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="5dp"
android:background="#FFFFFF">
<EditText
android:id="#+id/caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:maxLength="100"
android:hint="Hello"/>
<VideoView
android:id="#+id/previewvideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_below="#+id/caption"/>
</RelativeLayout>
adding layout_below invalidates layout_centerVertical. Instead make the EditText use android:layout_above="#+id/previewvideo" and remove the layout_below on the VideoView.
You may have to tweak it a little more but that should get you started.
Related
I've got a problem in my application.
My layout contains:
ImageView
Button
Four EditText
When I change the emulator in landscape mode the ScrollView goes to the third EditText. The last EditText is not displayed in landscape mode.
My layout.xml
<?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 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="235dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/formulario_foto"
android:background="#00A8EC"
android:src="#drawable/person"/>
<Button
android:layout_width="56dp"
android:layout_height="56dp"
android:id="#+id/formulario_foto_button"
android:layout_alignParentBottom='true'
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:background="#drawable/formulario_foto_button"
android:elevation="5dp"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="20sp"/>
</RelativeLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="Nome"
android:id="#+id/editTextNome" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:ems="10"
android:hint="E-mail"
android:id="#+id/editTextEmail" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="Endereço"
android:id="#+id/editTextEndereco" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:inputType="phone"
android:ems="10"
android:hint="Telefone"
android:id="#+id/editTextTelefone" />
</LinearLayout>
</ScrollView>
It looks like ScrollView does not respect the margin of its child. This behavior has been noted in
Android ScrollView not respecting child's bottom margin
Scrollview doesn't scroll to the margin at the bottom
Scrollview extends beyond screen bottom
The workaround is to wrap your LinearLayout in a FrameLayout, ie.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:orientation="vertical">
...
</LinearLayout>
</FrameLayout>
This is slightly inefficient, since it uses an extra ViewGroup. If you are able to do so, the more efficient solution would be to use paddingTop on your LinearLayout instead of the layout_marginTop, ie.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="60dp"
android:orientation="vertical">
...
</LinearLayout>
Change linear layout height to wrap_content
I want my inner RelativeLayout to match the size of my tallest child but if I do wrap_content or match_parent, it just takes up the entire space. The reason I want to have these 2 layouts separate is because if I don't, the EditText will be so transparent that the background of the EditText is the RecyclerView behind it.
Here's my layout files:
layout_edit_text_and_button.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/message_send_edit_ui"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<EditText
android:id="#+id/type_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/send_message"
android:layout_toStartOf="#+id/send_message"
android:ems="10"
android:inputType="textMultiLine"/>
<ImageButton
android:id="#+id/send_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/type_message"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:contentDescription="#string/send_message"
android:src="#drawable/ic_send_black_24dp"/>
</RelativeLayout>
fragment_chat.xml:
<RelativeLayout
android:id="#+id/chat_ui"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_to_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/message_send_edit_ui"
android:layout_alignParentTop="true">
<android.support.v7.widget.RecyclerView
android:id="#+id/messages"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.SwipeRefreshLayout>
<include
android:id="#+id/message_send_edit_ui"
layout="#layout/layout_edit_text_and_button"/>
</RelativeLayout>
The RelativeLayout in layout_edit_text_and_button.xml is only going to affect what's there and the include in fragment_chat.xml won't affected by it.
So you need to manipulate the size in fragment_chat.xml or in code.
I'm trying to get a relative layout to wrap around a button. It looks like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/Theme.MyCompany.Button.Container"
android:layout_weight="10">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
style="#style/Theme.MyCompany.Button.MyList"
android:id="#+id/this is my button"
android:text="#string/this_is_my_button"/>
</RelativeLayout>
It wraps fine in terms of the width, but the height reaches up to the above LinearLayout.
I tried to hack through it by create a button theme with the color white, but it just creates a lighter view.
How can I get this button to appear at the bottom of the page, with or without the benefit of a container?
If I understand you question correctly you nee to move this line android:layout_alignParentBottom="true" to the relative layout
So something like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/Theme.MyCompany.Button.Container"
android:layout_alignParentBottom="true"
android:layout_weight="10">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
style="#style/Theme.MyCompany.Button.MyList"
android:id="#+id/this is my button"
android:text="#string/this_is_my_button"/>
</RelativeLayout>
Try this out Just update the Relative layout's "android:layout_height" --
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/Theme.MyCompany.Button.Container"
android:layout_weight="10">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
style="#style/Theme.MyCompany.Button.MyList"
android:id="#+id/this is my button"
android:text="#string/this_is_my_button"/>
</RelativeLayout>
Some Changes In Your Code, Try This
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout //this will wrap your button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="10" >
<Button
android:id="#+id/this is my button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="this_is_my_button" />
</RelativeLayout>
</RelativeLayout>
I have a layout with some preferences. It works correctly in portrait, but not in landscape. I want this layout to refresh with a swiperefreshlayout, and does work. But as i i say in the title, it doesnt scroll up.
Ive been playing with a normal LinearLayout, with a RelativeLayout, using the swiperefreshlayout as the root view, playing with weights, ... but nothing. It either doesnt scroll up, or doesnt show the preferences correctly (only the 1st title). Dont know what else to try.
I should also say, i want this screen to keep a linearlayout at the bottom, fixed there. So, only the preferences would scroll.
Heres what i have right now:
<?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:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/edittexts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:elevation="8dp"
android:background="#color/primary"
android:visibility="gone"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/setwallpaperbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onActivarClick"
android:text="#string/setwallpaperbuton"
android:visibility="gone"
android:clickable="true"
android:enabled="true"/>
<EditText
android:id="#+id/edittextlocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dp"
android:textColor="#android:color/white"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:layout_gravity="center"
android:inputType="textMultiLine"
android:maxLines="2"
android:background="#android:color/transparent"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/edittextupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dp"
android:textColor="#android:color/white"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:layout_gravity="center"
android:inputType="text"
android:background="#android:color/transparent"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/swipelayout"
android:isScrollContainer="true"
android:layout_alignParentTop="true"
android:layout_above="#+id/edittexts">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<fragment
android:name="es.jnsoft.nowweather.PreferencesFragment"
android:id="#+id/preferencesfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="#xml/settings"/>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
Thanks in advance.
According to the documentation,
[SwipeRefreshLayout] should be made the parent of the view that will be
refreshed as a result of the gesture and can only support one direct
child.
This clause is violated in your layout.
I am trying to make an Activity that has a WebView and a small bar at the bottom with height of 40dp which has buttons for refresh, back, forward, etc. The issue i'm facing is that I want the WebView to consume the height of the entire view except for the bottom 40dp.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/id_web_view_browser"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="#drawable/ic_action_example"
android:onClick="onClick"
android:textSize="14sp" />
</RelativeLayout>
</RelativeLayout>
Above is what I have so far... Right now the browser has height of the whole screen with the RelativeLayout at the bottom (covering a piece of the browser). Is there anything else I can do to leave the bottom 40dp for the RelativeLayout only?
Put the webview so it layout_above the 2nd relative layout. Then change the relative layout to be aligned to parent bottom. That ought to work
Figured it out.
I was able to do the below:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/id_web_view_browser"
android:layout_above="#+id/id_web_view_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<RelativeLayout
android:id="#+id/id_web_view_bar"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="#drawable/icon_refresh"
android:onClick="onClick"
android:textSize="14sp" />
</RelativeLayout>
</RelativeLayout>
In the WebView I set the following property: android:layout_above="#+id/id_web_view_bar". That kept the bottom 40dp for the bar.
Kinda late... however might help any other arround...
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button_xs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_alignParentBottom="true"
android:background="#drawable/ic_action_example"
android:onClick="onClick"
android:textSize="14sp" />
<WebView
android:id="#+id/id_web_view_browser"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="#+id/button_xs"/>
</RelativeLayout>
In this case theres no need specific any height for the button or other view suppose to be after the webview.