I would just like to know if every time I need to set the View's size (width or/and height) as a function of its parent size (e.g. width = 0.5 * parent_size), I have to create a custom View and override onMeasure() or is there a way to do it using XML?
Thank you in advance!
Edit:
What I'd like to achieve is something like : place a view left aligned, with a width equals to 1/10th of the parent width and a height equals to 1/5th of the parent height. So on for other Views.
Here is an answer with two nested LinearLayouts and a TextView in the upper left corner, the TextView is 10% of width and 10% of height.
Just to prove it works with only one view.
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="10"
android:background="#FF0000"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="0dp"
android:orientation="vertical"
android:weightSum="10"
android:background="#00FF00"
android:layout_weight="1"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#000000"
android:layout_weight="1"
android:text="Hello"
android:textColor="#FFFFFF"
/>
</LinearLayout>
</LinearLayout>
you can use LinearLayout as a container and android:layout weight attribute for example. You can read more here: developer.android.com/guide/topics/ui/layout/linear.html#Weight
So, if you want to add only one element, you can add your view with weight=1 and one element with weight=9. But if you want to add 10 view in one row (1/10 of height for each) - you can use GridView for that or TableLayout.
I have to create a custom View and override onMeasure() or is there a
way to do it using XML?
There are no such way to achieve you goal using XML Layout file.
You have to manage this view dynamically .Your second approach is right approach .
You need to create dynamic view and make onMeasure() method according to your functions.
Related
I am trying to add view in GridLayout(3 columns) in horizontal manner but child view width is unspecified, sometime it has very broad width. Because of it third element goes out of view width.
I want to know if there is any layout or any way to make it done.
<GridLayout
android:id="#+id/activity_detailed_view_gl_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:layout_gravity="start|top"
android:orientation="horizontal"
android:layout_marginBottom="10dp" />
Any specific reason for using Grid Layout ? You can use Linear Layout with orientation Horizontal
I am trying to insert an imageView into a Relative/Linear layout in such a way that the imageView is larger than the parent. So far I have tried a negative margin and padding on the imageView and set clipChildren to false on the parent, but nothing seems to help. Has anyone done something like this before?
<FrameLayout 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:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="center"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/text_black"
android:paddingBottom="-100dp"
android:paddingLeft="-100dp"
android:paddingRight="-100dp"
android:paddingTop="-100dp"
android:src="#drawable/logo"/>
</LinearLayout>
</FrameLayout>
Basically I am trying to achieve something like this...
state1 - Has a series of viewgroups. When I click on one of them, I get state 2 where the image inside the viewgroup is visible fully and overlaps any of the surrounding viewgroups without affecting their position.
so why don't u just give parent to Width and Height= wrapcontent
The size of the container is limited by teh Parent, so it is not possible to put a container inside that is larger (you couldn't put a 5 liter box inside a 3 liter box).
ImageView has an "scale" attribute that will clip or adjust your view (and keep it sized appropriately).
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
I know that you can use weight parameters for linear layout in order to make two fields align nicely. What I want to do is I want to make sure that left half of the screen is used by one text field and other half is used by other text field (I am talking about width).
How to do so?
Use a "hidden view", with no height or width, in the center and put the text views on either side. Use parent align to set left and right.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<View android:id="#+id/dummy"
android:layout_height="0dp"
android:layout_width="0dp"
android:layout_centerInParent="true"/>
<TextView
android:layout_alignRight="#id/dummy"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_alignLeft="#id/dummy"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Can you calculate width dynamically? Maybe you could use 2 RelativeLayouts in a horizontal LinearLayout?
This is not possible with RelativeLayour as a parent. You need to wrap these TextViews inside a LinearLayout and set the widths with layout weight.
I just know this is simple and in about 30 minutes time, I'll hate myself...
I have a splashscreen which consists of a static image which fills the screen. So I simply set the background attribute of whatever root view I use in my layout.
The image has a blank area over which I need to place an "I accept" button. To deal with different resolutions, I must position it using a percentage of the display height - 58% is the spot.
I can't use layout_weight because that sizes the button and absolutelayout (setting the y position in code) is deprecated.
How can I achieve this? I don't care what viewgroup is the parent and I'm fine with having "blank" views filling up space.
I am aiming to do this entirely in layout XML to keep my code clean...
Thanks!
You say you can't use layout_weight, but that's your only option if you want to do it purely in XML. I don't understand why you think you can't use it anyway. Here's an example of how you might do it:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="58" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="42" >
<!-- Place buttons here -->
</LinearLayout>
</LinearLayout>
I don't see any other way that to use a layout_weight... Also the whole class AbsoluteLayout is deprecated, so try to avoid using it. I suggest to use an LinearLayout as your rootView with a given weight_sum of 1. add another Space-filling LinearLayout width a weight of 0.58 and below your Button with wrap_content attributes. Unfortunately I cannot tell you more unless you post your xml, so that I can see, what you try to achieve.
Kind of this should work:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/your_desired_background"
android:orientation="vertical"
android:weight_sum="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".58" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
What is the best layout to use tp produce a data entry form like this one in android:
Should I use a vertical linear layout,, with a horizental layout for each of the items? or a relative layout. I can't use a table layout because each text entry might have it's own width.
Thanks
As main layout, you can use a linear layout vertical, and for each row, a linear layout horizontal, setting width to fill_parent.
For the 4 first rows (data form container), when setting width, use "0 dip" for width and set a proportion to layout_weight as you need, keep the same value for the 3 next row in order to keep the alignement. Dont forget to set gravity right fo first column
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="50dip"
android:paddingRight="50dip">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="label"
android:gravity="right"/>
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:hint="value"
android:layout_marginLeft="15dip"/>
</LinearLayout>
</LinearLayout>
Do so, for last row with 0.5 proportion for each column.
Think, that helped you...
relative layout will be useful and use padding attribute to obtain the UI as image
As per my view you can pick linear layout it will be easy to handle
one Linear layout with vertical orientation
and 5 linear layouts with horizontal orientation