Android RelativeLayout above Fragment - android

I am trying to display a RelativeLayout above a Fragment without success.
And here my xml :
<?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="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#5C5A59"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="5dp" >
</RelativeLayout>
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:background="#5C5A59"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<TextView
android:id="#+id/lbl_altitude"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/lbl_altitude"
android:textColor="#00FFFF"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</LinearLayout>
Using this code the Fragment takes always the entire size of the screen and my RelativeLayout above isnt display.
Thank you in advance.

Try to use layout_weight property
You can read this guide: Linear Layout

if you are using the Linear layout then you have to set the weight this will set your screen according to the percentage for example if you give weightsum 1 to the parent and then you first give the layout weight 0.2 to your layout ang 0.8 to your fragement its means your layout acquires the 20% screen and other fragement acquire 80%
So i think you want to use the Relative layout as your parent and set first child aligment on the top of parent and set the fragement below to the relative layout.

Related

Center ListView horizontally

As easy as it seems to be, as stubbornly this ListView won't center itself (or it's content within - doesn't matter to me).
This is activity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.drobiazko.den.mobineon.MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#drawable/bg">
<RelativeLayout android:id="#+id/clock_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_dark">-->
<com.drobiazko.den.mobineon.ListViewRowsHeight
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:scrollbars="none"
android:divider="#null"
android:listSelector="#android:color/transparent"
android:persistentDrawingCache="scrolling|animation"
android:background="#android:color/holo_green_dark" />
<!--</RelativeLayout>-->
</LinearLayout>
This is ListView's item layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/btn_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/custom_btn_icon" />
<TextView android:id="#+id/btn_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/custom_btn_name" />
<TextView android:id="#+id/btn_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/custom_btn_count" />
</RelativeLayout>
I've tried everything (including simplifying layout to max), but it still won't go.
Funny that the first relative layout clock_container - centers like a charm, due to parent LinearLayout's android:gravity="center_horizontal". Why won't ListView?
Added later:
Inspired by FOliveira's answer, I've done some investigation and found that:
everything works fine, parent LinearLayout has android:gravity="center_horizontal" and centers his children respectfully. The reason for ListView's children are left-aligned is that ListView takes all possible width despite his android:layout_width="wrap_content" attribute.
Anyone knows why is that?
First of all , i would remove the first linear layout and stay only with the relative layout as the parent.
After having the relative layout as the main parent , set the CENTER_IN_PARENT attribute to true.
The mistake you are making is that you are setting the gravity of Linear Layout child to center horizontal, which is correct, but the only children being affected by this option is the Relative layout itself.

Custom Button Class not resizing based on layout_weight android

I have a CircularMeter class which is derived from Button. The problem is that it is not resizing even if the weight given is 0.5 (ie. half the vertical screen).
<LinearLayout
android:id="#+id/WidgetDataLayout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_weight="0.5"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
<com.test.CircularMeter
android:id="#+id/cm1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Any Ideas ?
You can take help from this example to make your circularmeter fill half of the vertical screen
eg.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="0.5"
android:background="#123456"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#123456"
android:text="#string/hello_world" />
</LinearLayout>
</LinearLayout>
There's no layout_weight in CircularMeter.
It's parent LinearLayout has both android:layout_alignParentTop="true" and android:layout_weight="0.5" which is not correct since layout_alignParentTop is a layout attribute for a RelativeLayout parent while android:layout_weightis an attribute for a LinearLayout parent. The layout XML does not show what is the parent layout actually is. android:layout_height="0dp" would make the layout invisible unless there really is a vertical LinearLayout as a parent.
Also note that layout_weight="0.5" won't make the view size half the screen. After doing first pass layout for a linear layout, the weight mechanism just distributes any remaining space in proportion of element weights. By default an element's weight is 0. So if you have just one element with non-zero weight, it will get all remaining space in its linear layout parent.

2 RelativeLayouts in one layout

I've decided to use 2 RelativeLayouts for my app, one Layout for one portion of child Views on the screen to the left, the other for child Views to go to the right.
The problem is I don't know how to lay them out in XML so that the middle white space isn't included when I inflate my Layout.
This is what I want.
When I use 1 RelativeLayout, the middle white space is filled with the RelativeLayout, and I can't touch anything behind it.
Thank you very much for reading.
Do something similar to the following example.
This will create a LinearLayout with 2 RelativeLayouts using layout_weight to space the RelativeLayouts and then you can populate the RelativeLayouts with whatever you want.
The Buttons are just place holders for the example.
<?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="fill_parent"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TEST1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TEST2" />
</RelativeLayout>
</LinearLayout>

Android - why do two TextView elements line up side by side instead of one below the next?

I have this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/solution_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label1"
/>
<TextView
android:id="#+id/solution_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label2"
/>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="20px" >
</ListView>
</LinearLayout>
I am combining two TextView elements with a ListView. For some reason, the TextView items render next to each other instead of one below the other. Why does that happen? And how do I make them line up one below the other?
Thanks!
in the LinearLayout you need to specify the orientation as follow:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
That's because the LinearLayout has a horizontal orientation by default.
Specify android:orientation="vertical" for it.
See LinearLayout tutorial
But you want: android:orientation="vertical"
Have you tried to set the attribute:
android:orientation="vertical"
Hello See Vertical orientation
You may consider to have two linear layouts and have the vertical orientation within another linear layout which is horizontal if that helps.

How to scale an Android layout object during runtime?

I want to set the width of a LinearLayout to half of the screen width dynamically in my UI initialization. I have a RelativeLayout wrapped around the LinearLayout, the hierarchy is the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/left_linear_layout"
android:layout_alignParentLeft="true"
android:layout_height="fill_parent"
android:layout_width="155dp" <!--want to set this to 1/2 screen width-->
android:orientation="vertical">
...
</LinearLayout>
<LinearLayout
android:id="#+id/right_linear_layout"
android:layout_alignParentRight="true"
android:layout_height="fill_parent"
android:layout_width="385dp"><!--want to set this relative to screen width as well-->
....
</LinearLayout>
</RelativeLayout>
Alternatively, can this problem be solved using View instead of Layout? Any suggestion is appreciated!
You can do this by using layout_weight, but you'll need to add some invisible views for padding. For instance the following would make your top panel half the screen width:
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/left_linear_layout"
android:layout_alignParentLeft="true"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical"
>
...
</LinearLayout>
<!-- need this view to fill the other half of the screen -->
<View
android:id="#+id/spacer"
android:layout_toRightOf="#id/left_linear_layout"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
....
</RelativeLayout>
The amount each view will take up is layout_weight/total_layout_weight. In this case total_layout_weight = 1+1 = 2 and each view has a layout_weight of 1, so each view takes up 1/2 of the screen.
You could simply use a LinearLayout as your top-level layout, and then set the weight of the two child layouts.

Categories

Resources