I was wondering if it's possible to set an automatic/dynamic margin (padding?) between elements in an Android layout without having to do it programmatically?
For example let's say there is a horizontal LinearLayout which is set to android:layout_width="fill_parent" and that contains five elements. Is there a setting that evenly shares the remaining empty space of the LinearLayout to the margins of the child elements?
See image at http://img63.imageshack.us/img63/8/margin.png
Thanks for any help!
You could use view spacers as your margin, with the layout weight set on them.
<LinearLayout ...>
<View id=marginLeft android:layout_weight="1"/>
<Element1/>
<Element2/>
<Element3/>
<Element4/>
<Element5/>
<View id=marginRight android:layout_weight="1"/>
</LinearLayout>
This should make the two views use up any remaining space in your row. Note, the above XML will not parse :)
-- Edit
Just saw the picture. To add equal spacing between each of the elements too would just be a case of adding more spacer elements between your content elements (all with the same layout weight)
Yup. Only LinearLayouts support it. Tis called layout weight
Look for LinearLayout at http://developer.android.com/guide/topics/ui/layout-objects.html
For a good intro, look here
Related
I am working on linear layout for my simple android application. I wanna make the portion of two views dynamically change based on the size ( I want to have, for a row for left to right, the first 20% is empty, and all the content is inside the rest of 80%) . For this approach, i chosen the weight for different view. I created an nested linear layout for this approach. For example, the layout hierarchy is something like this.
<linearLayout> //parent layout
<linearLayout //child 1 layout
android:layout_weight="1">
//so that this view occupy 20% of the space regardless the width of device. I intensionally wanna keep this view empty.
</linearLayout>
<linearLayout //child 2 layout
android:layout_weight="4">
//so that this view occupy 80% of the space regardless the width of device. and
//inside this view I have whatever view I wanna add on it.
<EditText>
<ImageView>
</linearLayout>
</linearLayout>
With this approach, the Lint in Android Studio tell me the following warnings:
This is a Nested Layout. Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.
the child 1 layout is useless: This LinearLayout view is useless (no children, no background, no id, no style)
Can anyone address me the right layout to use in order to have the layout dynamically change based on the size of devices? How should I correctly set up the empty space for a linear layout case?
This is a possible solution using weights:
<LinearLayout
android:layout_width="match_parent"
android:gravity="end"
android:weightSum="1">
<!-- Your content here: -->
<View
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_gravity="end" />
</LinearLayout>
Have a look at PercentRelativeLayout.
Note: You need the Percent library to use it.
I am a newbie in Android-programing and currently I'm building my first application. I have a LinearLayout (with several imageviews), which is situated inside of basic RelativeLayout.
The xml width and heigth settings of LinearLayout are as follows:
android:layout_width="135dp"
android:layout_height="match_parent"
The settings of imageviews inside LinearLayout look like this:
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="125dp"
android:layout_weight="3"
.....
When I start my application on small screens, it looks normal, but when the screen is big, all the pictures become very narrow (the width of LinearLayout inside RelativeLayout stays the same on small and big screen). I tried to set it in percentage, but that is not allowed. So I ask you: how can I set the width of LinearLayout to be like 25-30% of the width of parent RelativeLayout(to make my images look pretty on any device).
I know that there is a layout_weight attribute, but it doesn't seem to work with layouts inside another layouts (or maybe I have no idea what is correct or what is not).
As I know, you cant take parent layout percantage to child layout.
Linearlayout add elements to as columns or as row.
linearlayout api
Relative Layout
A Layout where the positions of the children can be described in
relation to each other or to the parent.
Relative Layout api
Why do you use linearlayout inside relative layout? In linearlayout you cant design your layout elements puting element with mouse,But in relativelayout you can.
I have a table layout with 5rows. I'd like the size of first row to take the rest of the screen while the remaining rows take as much space they need based on the content inside.
My first intuition was to make the first row's layout width and height set to match_parent while having the other rows' width to match_parent and height to wrap_content. Needless to say, this does not work.
How can I accomplish this?
I'd like the size of first row to take the rest of the screen while
the remaining rows take as much space they need based on the content
inside.
I think that using android:layout_weight="1" on the first TableRow should solve the problem you have.
Put your TableLayout inside a ScrollView and fix the Height of your First TableRow which will solve your issue. Hope it works.
Seeing as TableLayout inherits from LinearLayout, have you tried setting Row height to 0dp and setting layout weights for each one to achieve the desired effect? Like so:
<TableRow
android:layout_height="0dp"
android:layout_weight="2"
android:layout_width="match_parent">
<TableRow
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent">
and so on....
stick to your first intuition but add layout_weight="1" to the first row.`
I have ads layout under the ScrollView. In order to prevent the ScrollView overlapping ads layout, I have to use
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" // <--- THIS!
>
//...scrollview content
</ScrollView>
<include layout="#layout/ads_468x60"/>
What is the meaning of layout_weight if both width and height have been set to fill_parent?
According to documentation, this should not work, or to be precise, if both layout_height and layout_width have been set other than 0dp, then layout_weight is disregarded. But, in this example it works and bottom ads layout will not be shown without the attribute android:layout_weight="1" inside of the ScrollView.
use android:layout_height="0dp"
android:layout_weight="1" tells layout manager to fill all the free space.
In your case, the layout_weight makes it ok to make your view smaller than defined (fill_parent) and it is shrinked to fit the screen space available. The weight indicates how to change the sizes of one or more components relatively to each other. In your case, you have just one view with weight. It is considered faster to use 0dp, as the system needs to measure less.
layout_weight is used for situations when you want to decide dynamically the width or height, so when you say layout_height = "0dp".
It actually now depends on layout_weight attribute to make correct decision for it.
So by setting up weightsum property on parent layout you can accordingly distribute weights on child layouts by providing layout_weight.
refer this guy's answer and description on layout_weight :
Android Layout Weight
I am building something like a ListView, but I'm rolling my own because I want to do some custom stuff and have more control than using ArrayAdapters.
So I've defined part of my layout in XML, including one LinerLayout inside a ScrollView. My goal is to bind to that Linearlayout in code, then insert additional RelativeLayouts inside the LinearLayout using no XML, just code.
Here is my XML:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ListScroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="#+id/ListHolder"
android:layout_height="400px"
android:background="#323232"
>
<!--Here is where I want the RelativeLayouts to go... -->
</LinearLayout>
</ScrollView>
Then in code, I'm trying to add RelativeLayouts, each 50px in height, to the LinearLayout, the one above that has a height of 400px.
//The parent container - is defined above in XML.
itemContainer = new LinearLayout(context);
itemContainer = (LinearLayout)findViewById(R.id.ListHolder);
Layouts = new ArrayList<RelativeLayout>();
Layouts = LoadWithRelativeLayouts();
for(RelativeLayout listItem: Layouts){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 40);
listItem.setLayoutParams(params);
itemContainer.addView(listItem);
}
Each one of the layouts in the array has a text view in it that says "Test". When I step through the code, there are 10 elements in the array, and all of the textviews are there, so I would expect to see the 400px LinearLayout filled with 10 Relative layouts one after another, each with 50px height (and fill_parent width) reading "Test" - but all I see is one, as if only one got added, or they are all positioned on top of one another.
Getting screenshot now...
When you add something to a layout, you have to use layout params of that kind. So as you're adding to a LinearLayout, you should use LinearLayout.LayoutParams.
Then you'll probably also need to set your LinearLayout orientation to vertical, because right now the items you don't see are all in a row offscreen at the right :)
Try adding android:orientation="vertical" to the LinearLayout holding the RelativeLayouts.