Adding Child View in a Layout Programatically - android

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

Related

View to fill and center contents in the remaining space Android

I have a LinearLayout which is a view that I need to display under another view, all within a ConstraintLayout. I want to LinearLayout's height to be dynamically equal to the space between the bottom of the above view, and the bottom of the parent. I've tried
<android.support.v7.widget.LinearLayoutCompat
android:id="#+id/sample_layout"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:visibility="gone"
without success. I am very new at Android UI work so I'm not sure if I'm using the right layout. Can anyone suggest a better implementation, or a fix to my current attempt?
You means this?:
<TextView
android:id="#+id/your_linear_layout"
android:layout_width="200dp"
android:layout_height="0dp"
android:layout_marginEnd="56dp"
android:background="#0ff"
android:text="your LinearLayout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/above_view"
app:layout_constraintVertical_bias="0.0"/>
you can put the LinearLayout inside a RelativeLayout that is under the needed view, and make the height of the relative layout to fill the space, while you can make the linear layout to be in the center of the relative layout such as:
<RelativeLayout ... >
<LinearLayout
....
android:centerVertical="true"
</RelativeLayout>

How can I make a child larger than the parent in Android?

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

Android and View size

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.

ListView not getting space to show content on smaller screens

So I am developing a screen where there are some images and buttons on top and Below that is a list view which shows a list of some activity.
The design is something like this :-
Now on smaller screen the ListView height becomes very small as the screen space is taken up by the above icons and images.
So how can i increase the height of the Linearlayout or ListView so that user can scroll to the see the rest of the ListView.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
.... Other Layouts .....
<ListView
android:id="#+id/listArea"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:paddingLeft="#dimen/list_padding"
android:paddingRight="#dimen/list_padding" />
</LinearLayout>
Edit: Tried using the top view as a header to the List but since I want an EmptyView too, this is creating a problem as it replaces the whole header + listview
From what I read about that issue, you should specify the Views on top as header of the list, and the'll scroll properly.
Afaik this only works if the list is non-empty, as the empty view replaces the whole list including headers.
You can use the weightSum and layout_weight attributes to control how much of the parent's available space a child view will take up. To use these, a parent layout, for example, your LinearLayout, gets the android:weightSum attribute. Each child layout gets the android:layout_weight attribute, where the sum of all child weights is the weightSum of the parent. In addition, each child should have their layout_height or layout_width set to 0dp, whichever is going to be decided by the weight.
Here's an example based on your diagram. Let's say you want the two top views to take up 1/4 of the screen each, and the ListView to take up the bottom half. Add android:weightSum="4" to your LinearLayout, android:layout_weight="1" to the two child layouts that you represent with ...Other Layouts..., and android:layout_weight="2" to the ListView. Code might look something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="4">
<ImageView
android:layout_weight="1"
android:layout_height="0dp"
...some other attributes.../>
<LinearLayout
android:layout_weight="1"
android:layout_height="0dp"
android:orientation="horizontal"
...some other attributes...>
...some children of the LinearLayout...
</LinearLayout>
<ListView
android:id="#+id/listArea"
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingLeft="#dimen/list_padding"
android:paddingRight="#dimen/list_padding"
android:layout_weight="2"/>
</LinearLayout>

What layout to use to produce a data entry form in android

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

Categories

Resources