I am creating an Android app which has a main RelativeLayout and some LinearLayout within it.
Now i have this problem. When dragging items to the editor, e.g. a LinearLayout, it doesn't fit to the full width of the screen. How can I make this possible? This is my XML:
<RelativeLayout 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:fillViewport="true"
android:gravity="fill"
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=".MainActivity" >
<LinearLayout
android:id="#+id/itemDetailLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/itemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="40dp"
android:minWidth="40dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:fillViewport="true" >
<TextView
android:id="#+id/itemRecentHigh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recent High" />
<TextView
android:id="#+id/itemRecentLow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recent Low" />
<TextView
android:id="#+id/itemAverage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Average" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/listViewLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/itemDetailLayout"
android:layout_marginTop="10dp"
android:fillViewport="true"
android:gravity="fill_horizontal"
android:orientation="vertical" >
<ListView
android:id="#+id/searchListView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
</ListView>
</LinearLayout>
Added a screenshot for more info. Added blue background to have some contrast.
Replace this:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
With
android:layout_width="match_parent"
android:layout_height="wrap_content"
into your LinearLayout or Remove all the Padding from main RelativeLayout
Removing padding makes the linear layout fit the entire screen of the device
The space between the blue part and the "end of the screen". On the sides and above the part where the back button and home button are.
Remove the padding from the root RelativeLayout.
Try
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical>
// Other views within the linear layout
</LinearLayout>
I suppose you are using Eclipse. Personally, I hate the Graphical Editor of Eclipse and I usually write the XML code directly because working with the editor is a real pain
Don't use fill_parent as it is deprecated.
Instead to your RelativeLayout set
android:layout_width="fill_parent"
android:layout_height="fill_parent"
Then, to the list view that you want to fill the rest of the screen, you can set this tags
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
By doing so, you are telling the XML that you want your ListView to do what fill_parent used to do in previous versions of Android.
Kind regards!
you should try something like the following :
<LinearLayout
android:id="#+id/itemDetailLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
...
</LinearLayout>
what i mean you should do this :
android:layout_width="fill_parent"
for every LinearLayout that you want its width to fill_parent .
FILL_PARENT means that the view wants to be as big as its parent as mentioned in the documentation .
and please give me some feedback
Hope That Helps .
Related
I have a layout with listview and two buttons side by side at the bottom of the listview. Everything work fine until I have added the swiperefreshlayout for my listview.
My codes are as below.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFF"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/search_bar"
android:background="#color/colorPrimaryDark"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
>
<EditText
android:id="#+id/keyword_editText"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="#drawable/search_edit_text"
android:hint="Keyword"
android:textColorHint="#color/list_item_title"
android:textColor="#000"
android:layout_centerVertical="true"
android:maxLines="1"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView_layout"
android:orientation="vertical"
android:layout_below="#+id/search_bar"
>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/refresh"
android:layout_height="match_parent"
android:layout_width="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:id="#+id/RsListView"
android:dividerHeight="1dp"
android:divider="#D3D3D3"/>
</android.support.v4.widget.SwipeRefreshLayout>
<LinearLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/req_history_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Request History"
android:layout_weight="1"
android:background="#drawable/request_history_button"
android:textColor="#drawable/button_text_color"
/>
<Button
android:id="#+id/apprv_history_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Approval History"
android:layout_weight="1"
android:background="#drawable/approval_history_button"
android:textColor="#drawable/button_text_color"
/>
</LinearLayout>
</LinearLayout>
Sorry for dumping my codes here as I can't figure out any other way for you guys to understand my code.
Please help
Inside LinearLayout usually all children have even dimension according to orientation. However, when you would like one child to take a little amount of place and another to fill the rest, you might be interested in layout_weight parameter. If the child that should fill the space gets android:layout_weight="1" it will fill the space making a place for other views.
In order to see the buttons add android:layout_weight="1" to SwipeRefreshLayout and set android:layout_height="0dp". That will make your buttons visible.
Let me briefly explain what I'm trying to do. I am getting a restaurant from Yelp and putting the restaurant's attributes into a CardView. The reason I want a CardView is because I want swiping functionality. I want to be able to swipe left, right, and up. To do this, I need a RecyclerView to handle every direction (or so I've come to understand from reading this). If someone has a better way to do this please let me know.
For some reason, when you set MapView's layout_height to a concrete value, it resizes fine. But it won't fill the parent.
I don't want to set a concrete value for obvious compatibility issues.
Also, is it possible to disable swiping on the MapView part of the CardView?
So here are my layout files:
fragment_main.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:showIn="#layout/activity_main"
tools:context=".MainActivityFragment">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:id="#+id/radioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Use Zip Code"
android:id="#+id/useZip"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Use Location"
android:id="#+id/useLocation"
android:checked="false" />
</RadioGroup>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/zipcode"
android:hint="zip code"
android:layout_below="#+id/radioGroup"
android:layout_centerHorizontal="true"
android:visibility="gone" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Generate"
android:id="#+id/generate"
android:layout_gravity="bottom|center_horizontal"
android:layout_below="#+id/zipcode"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_below="#+id/generate"
android:layout_alignParentBottom="true">
<android.support.v7.widget.RecyclerView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/container">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</RelativeLayout>
restaurant_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/restaurantContainer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="#dimen/_75sdp"
android:layout_height="#dimen/_75sdp"
android:id="#+id/thumbnail"
android:layout_margin="#dimen/_5sdp"/>
<ImageView
android:layout_width="#dimen/_75sdp"
android:layout_height="#dimen/_15sdp"
android:id="#+id/rating"
android:layout_margin="#dimen/_5sdp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/name"
android:gravity="top"
android:layout_margin="#dimen/_5sdp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/categories"
android:layout_margin="#dimen/_5sdp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.gms.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mapView"
map:mapType="normal"
map:liteMode="true"
map:cameraZoom="14"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Here's a screenshot of the problem:
Either set CardView height to let's say 200dp and MapView to fill_parent.
This way it will fill remaining height, you can adjust CardView height. or set CardView height to wrap_content and then provide your MapView height like 200dp (or whatever height you want).
Update: RecyclerView is a scroll view. Which means it has infinite height and every Card you put in it should have it's own height, card can't have fill_parent.
Now if you want your MapView to have rest of the height. I assume you mean rest of the Card height. For this first you have to define how long you want your Card to be.
Ex: If you set your Card height to 500dp, MapView to fill_parent and your content other than MapView inside Card is 100dp then MapView will take rest of the height (400dp).
If you want MapView to take rest of the height on the mobile screen then you have to remove RecyclerView and just use Card.
I have a problem to fill the whole screen with a scrollview.
How do I make the scrollview cover the whole screen width and screenheight? I tried with fillviewport but that that works for the screenheight.
I am not sure if its the scrollview or its parent (FrameLayout) that is the problem?
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fillViewport="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="img1"/>
<TextView
android:id="#+id/text_img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ImageView
android:id="#+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="img2"/>
<TextView
android:id="#+id/text_img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ImageView
android:id="#+id/img3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="img3"/>
<TextView
android:id="#+id/text_img3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
</LinearLayout>
</ScrollView>
change your ScrollView and LinearLayout like this
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
Change your <ScrollView> from wrap_content to match_parent.
Example:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
Also, you should be using match_parent instead of fill_parent in API 8+.
EDIT:
After talking a bit in the comments, OP and I discovered that the FrameLayout's default background is transparent, which means you can see the old view underneath.
Using a LinearLayout or really anything instead of a FrameLayout will fix this problem.
Other answers are going in the right direction but your main problem is probably due to the fact that it's a Dialog.
First of all make sure your Dialog is borderless. You can use one of approaches proposed here (not to duplicate answers).
I want to make an activity with 2 elements in the screen, one in the bottom, with a fixed size, and one on top of this which one must fill the screen.
How I can do that in a Layout?
Thank you
use layout_weight="1" for 1st element and put layout_height="0dp" and use linearlayout as the parent which contains the 2 elements.
Use below code:
<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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="#string/hello_world" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="test" />
</LinearLayout>
Or you can do this,
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_above="#+id/ll1"
>
</LinearLayout>
<LinearLayout
android:id="#+id/ll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
>
</LinearLayout>
Add this to the first element:
android:layout_weight="0.8"
and for the second one
android:layout_weight="0.2"
You can use Relative Layout like...following example
<//you are second element
android:id="#+id/ui_radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:clickable="false"
android:orientation="horizontal" >
<///you are second element>
<//you are First element
android:id="#+id/ui_pager"
android:layout_width="wrap_content"
android:layout_above="#id/ui_radiogroup"
android:layout_height="wrap_content"
android:scrollbars="horizontal" />
Other OPtion use Linear Layout assign android:layout_weight to each element depending upon your requirement
try this,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/footertext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="Footer" />
<TextView
android:id="#+id/centertext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/footertext"
android:gravity="center"
android:text="center" />
Here is the complete trick :
Use a RelativeLayout to wrap your two items,
Put layout_alignParentBottom="true" on your bottom item
And layout_above="#+id/your_bottom_item_id" on your top item
Here is a complete working example that you can cpy/paste :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/top_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_above="#+id/bottom_layout">
</LinearLayout>
<LinearLayout
android:id="#+id/bottom_item"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:layout_alignParentBottom="true">
</LinearLayout>
</RelativeLayout>
Simply use layout_weight="1" for first element and layout height should be match_parent.
Hey im having trouble with these linear layouts in android xml. initially i did them in a relative layout and it worked fine, but after some research i discovered that relative layouts inside of a scrollview doesnt work supposedly. so now after much tweaking my imageview shows up but has a huge margin above and below it and my textview doesnt even show up aside from being where its supposed to in the graphics editor but blank.
so whats wrong? should i even be using a linear layout?
this is roughly what its supposed to look like http://www.mediafire.com/view/?z945tz2vrb2x46t
and this is what it looks like after Abdul and Ralgha's help as well as setting the base linear layout's height to wrap content http://www.mediafire.com/view/?px17q2z3yyeo8az
Thanks
<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" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/today"
android:src="#string/today"
android:textSize="30dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<ImageView
android:id="#+id/image1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/weeklylist_blocks"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
`![screwed up layout][1]
Don't worry about weight right now, leave it removed. In your TextView, change "android:src" to "android:text" (without the quotes naturally). If that alone doesn't solve it, remove the textSize line and see what happens. If that works, then you can start playing with sizes (and weights if needed).
Also, textSize should be given in sp rather than dp. sp is for text, dp is for everything else, though that's not what is causing your problem.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="#+id/today"
android:textSize="30sp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/today"
/>
<ImageView
android:id="#+id/image1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/weeklylist_blocks"
/>
</LinearLayout>
</ScrollView>
android:layout_weight="1"
might be the cause of your problems
Its works like charm, Use the following coding to achieve your thing:
<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:gravity="center_vertical"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:fillViewport="true" >
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/today"
android:src="#string/today"
android:textSize="30sp"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="#string/today"
/>
<ImageView
android:id="#+id/image1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:contentDescription="#string/nissan"
android:src="#drawable/weeklylist_blocks" />
</LinearLayout>
</ScrollView>
</LinearLayout>