I have a custom info window for Google Maps API v2 that uses a RelativeLayout added by overwritting getInfoWindow in my class. This layout has a TextView with a title in the first line and three TextViews in the next line, one aligned to the left, another to the center and the thrid one to the right. It works, but the window takes the whole of the screen, which is very ugly in big screens like tablets.
I have tried to set the width of the RelativeLayout to some fixed value (let's say "50dp") or to "wrap_content", but it always takes the whole of the screen.
This is the XML for the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bus_stop_info_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffffff" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="A title" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:gravity="left"
android:text="Test1" >
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:gravity="center"
android:text="Test2" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:gravity="left"
android:text="Test3" >
</TextView>
</RelativeLayout>
How can I achieve the desired effect?
For that make root element as Linear-layout and put Relative-layout inside it.so that window will not fill the whole screen in tablet.This will not affect whether Relative-layout width is wrap_content or fixed size.
U can try my answer here
Related
Having a problem with nested LinearLayouts to use as a custom row in a ListView.
App is a kind of internal corporate phone book and this row will be repeated in the ListView.
The "Details Witheld" on the right looks fine when gravity is set to left for it, but if gravity is set to center then it appears half off the page.
I'd guess my parent LinearLayout is too wide for the screen but it's set as fill_parent so I don't see how.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
Full XML source is below.
Aligned Left looks like this and I'd like Details to be a space in so it's centered above the Witheld.
Aligned Center looks like this
Full XML Source
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- Left side name & post / parish -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="80"
android:orientation="vertical" >
<TextView
android:id="#+id/txtrowname"
android:layout_marginBottom="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Smtih, Mr John"
android:textSize="24sp"
android:textColor="#color/darkblue_text"
android:textStyle="normal" />
<TextView
android:id="#+id/txtrowpost"
android:layout_marginBottom="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12 Some Street, SomeTown. SS11 1SS"
android:textSize="12sp"
android:textColor="#000000"
android:textStyle="normal" />
</LinearLayout>
<!-- Details withheld on right -->
<TextView
android:id="#+id/txtrowwitheld"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="Details Witheld"
android:layout_weight="20"
android:textSize="18sp"
android:gravity="center"
android:lines="2"
android:maxLines="2"
android:textColor="#FF0000"
android:layout_marginBottom="2dp"
android:textStyle="normal" />
</LinearLayout>
Image with layout_gravity as suggested by Ramesh
try layout_gravity in place of gravity that will work and dont use line="2". it will automatically fit into 2 lines if width exceeds.
I ended up just making do without it being centered.
I have posted the exact layout below. I want to show an image and text vertically centered inside the footer. I have applied:
android:gravity="center_vertical|center"
To both the LinearLayout containing these elements and the TextView inside but nevertheless the whole line image and text appears way too far towards the top of the footer. I want it centered vertically but instead it is in the top 30 % of the footer at all times.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/someMessageMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_margin="10dip"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical|center"
android:paddingBottom="2dip"
android:paddingTop="16dip" >
<ImageView
android:id="#+id/myImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dip"
android:paddingTop="3dip"
android:src="#drawable/picimg" >
</ImageView>
<TextView
android:id="#+id/myMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:gravity="center_vertical|center"
android:text="This message appears way to close to the top of the footer. It should be along with the image in the center:"
android:textStyle="bold"/>
</LinearLayout>
<Button
android:id="#+id/myButton"
style="#style/mybuttonstyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:text="#string/lbl_send"
android:visibility="gone" />
</LinearLayout>
After seeing your layout it seems you haven't given android:paddingTop to your TextView. Thats why its appearing to the top.
Moreover you can also remove android:paddingTop from ImageView if that suits to your layout.
Hope that helps.
How big is the image? I think the problem is in your second LinearLayout. You're setting the height to be "wrap_content" so that means that it will only be as big as the biggest child. If the image and the text are about the same height, then it won't seem as if anything is getting centered. You can test this theory by forcing the height to be something big enough.
https://docs.google.com/document/d/1SDDnAG-jkQjC6F85iPgfv3Et1pYl8t03k-TkCN3YcCw/edit
I have an Android preference page containing a mixture of stock, and custom List Items. The custom list items "Mood" and "Mixed Mood" can be seen in the screen shot in the referenced Google Doc, and function in the following way. (The external link is because, without a reputation of at leat 10, I'm not allowed to post images in stackoverflow...)
1) Before a user has chosen a value, they simply display their title. (either Mood or MixedMood)
2) After a user has chosen a value, they display their title AND a custom tool tip (the little colored squares), indicating their choice.
THE PROBLEM: You can see from the screen shot that before a user has chosen a value, the "Mood" text is not centered. I would like for it to be centered, and then dynamically make room for the custom tool tip after a choice is made. In other words, I would like
<TextView android:id="#+android:id/title"
To respond to
android:gravity="center_vertical".
Is this possible?
My layout file looks like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="60dip"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginLeft="15dip"
android:layout_marginRight="6dip">
<TextView android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18sp"/>
<!-- This image represents the dropdown arrow -->
<ImageView
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#android:id/title"
android:scaleType="fitEnd"
android:src="#drawable/ic_btn_round_more_normal_cropped" />
<monarca_rct.client.customcomponents.MoodScalePresentation
android:id="#+android:id/hpmp_mood_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:paddingTop="4.0dp"/>
</RelativeLayout>
</LinearLayout>
If i get your question right:
You can set android:layout_alignWithParentIfMissing="true" in <TextView android:id="#+android:id/title" for more info visit http://developer.android.com/resources/articles/layout-tricks-efficiency.html
I can't exactly try this out but I think it might be something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="60dip"
//REMOVED GRAVITY
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
//REMOVED GRAVITY
android:layout_marginLeft="15dip"
android:layout_marginRight="6dip">
<TextView android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical|left"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18sp"/>
<!-- This image represents the dropdown arrow -->
<ImageView
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#android:id/title"
android:scaleType="fitEnd"
android:src="#drawable/ic_btn_round_more_normal_cropped" />
<monarca_rct.client.customcomponents.MoodScalePresentation
android:id="#+android:id/hpmp_mood_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:paddingTop="4.0dp"/>
</RelativeLayout>
</LinearLayout>
The reason to go from center_vertical to center_vertical|left is because your parent layout is like a grid and you can "choose" your position. So if you have:
(1) (2) (3)
(4) (5) (6)
(7) (8) (9)
[Don't mind the white spaces between each line, for some reason if I don't do it like that it gets displayed all in one line.]
The 4 is where you want your text. As you can see, the 4 is on the left but also vertically centered hence the combination:
android:gravity="center_vertical|left"
Quick question, why do you need a RelativeLayout inside a LinearLayout? You could do without the LinearLayout altogether from the looks of it.
I am trying to make a scroll view that has a nested relativeLayout view. The realative layout has a background image. When the realativeLayout is the only layout the background image is the size I want it to be.
When I add scroll view it makes the image bigger and I am not sure why and what properties I need to set to fix it.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:id="#+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background2"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/txtApplicationTitle"
android:text="My Application"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="5dp"
android:textSize="15dp"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/txtAbout" />
<TextView android:id="#+id/txtPrivacyHeader"
android:layout_width="wrap_content"
android:text="#string/PrivacyHeader"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="21dp"
android:layout_marginTop="94dp" />
<TextView android:id="#+id/Privacy"
android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/Privacy"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/txtPrivacyHeader"
/>
</RelativeLayout>
</ScrollView>
// this is what I get (you can't tell but it is scrollable)
(source: gyazo.com)
// this how I want the SrcollView to look(this is the exact same code as above just without the ScrollView wrapping it)
(source: gyazo.com)
See how the top part has all of sudden grown so much.
Your ScrollView has its height set to wrap_content and your RelativeLayout has its height set to fill_parent. That means your ScrollView is going to expand to the entire view.
I'm new to android development... while making my application layout i want a button to remain at the very bottom of the screen while a scroll view is placed above it. I am unable to do this i was using the size of the scroll view as 430dp so that it works but when i change the orientation of the screen this does not work as 400dp is bigger than the screen.
how do i make it so that the button stays at the bottom irresepective of the screen orientation ?
:/
Set the ScrollView's layout_height to fill_parrent and layout_weight to 1 and the Button's height to wrap_content.
You could go with this
android:gravity="bottom"
This should always push your element to the bottom of its container.
But it'd more helpful if you'd post up your layout XML.
Here's a real world example of precisely what you're asking.
In this layout, I have a header at the top, a list view taking all the space below it and a button to clear (cancelled, failed, finished) elements of the list view, then right at the bottom I have a custom control showing a toolbar.
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="#+id/layout" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/browsePeerHeader"
android:layout_width="fill_parent" android:layout_height="70sp"
android:layout_marginBottom="2sp"
android:layout_gravity="top"
android:background="#drawable/background_barbed_wire"
>
<ImageButton
android:id="#+id/frostwire_sphere"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="#drawable/icon"
android:layout_gravity="center_vertical"
android:layout_margin="3sp"
android:background="#00000000"/>
<TextView android:id="#+id/browsePeerTitle" android:textSize="30sp"
android:text="Downloads"
android:textColor="#ffffffff" android:textStyle="bold"
android:shadowColor="#ff000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="4.0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10sp"
android:gravity="left|center_vertical"/>
</LinearLayout>
<ListView android:id="#+id/ListViewTransfers"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2sp"
android:layout_marginBottom="2sp"
android:layout_weight="1"></ListView>
<Button android:id="#+id/ButtonClearFinished" margin="2sp"
android:layout_width="fill_parent" android:layout_height="wrap_content"
/>
<com.frostwire.android.views.FrostWireStatusBar
android:id="#+id/FrostWireStatusBar" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" /></LinearLayout>
Here's a screenshot
The trick is basically to have the list view use all the space left in the layout that contains it, you don't even have to tell it to fill_parent, just with android:layout_weight="1 it should work.