not enough space for ListView - android

i have TextView and ListView on LinearLayout, unfortunately on small screen i have not enough space for ListView(i mean it display about 0.5row). Is it possible to scroll up everything together.? Is in SDK any alternative for ListView that i can use for it.? Or maybe You got other idea how to make it userfriendly.?
layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<TextView
android:id="#+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/studio"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2" >
</ListView>
</LinearLayout>

There is a better solution for this instead of adding title to the main layout you can add that title to another layout and inflate that layout as header to the listview.
Like this -:
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.title, myListView, false);
myListView.addHeaderView(header, null, false);
layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
title.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<TextView
android:id="#+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/studio"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

Your problem is the sizes and weights that you have assigned to various components. Assign proper weights to the listview and the text views layouts and it should work fine on any screen. e.g do some thing like this :
<LinearLayout
android:id= "#+id/parent_layout"
android:layout_height = "match_parent"
android:layout_width = "match_parent"
android:weightSum = "1.0"
android:orientation = "vertical" >
<LinearLayout
android:id= "#+id/text_views_layout"
android:layout_height = "0dp"
android:layout_width = "match_parent"
android:layout_weight = "0.7"
>
<!-- place your text views here -->
</LinearLayout>
<ListView
android:id ="#+id/list"
android:layout_height = "0dp"
android:layout_width ="match_parent"
android:layout_weight = "0.3"
>
</ListView>
</LinearLayout>
Note: you can specify any weights proportion for the textview_layout and the listview (i have set it to 0.7 : 0.3) but remember what ever weights you assign to them, they must sum up to the weight sum of the parent which is 1.0 in this case. Otherwise you might see unexpected behaviour.

Put the LinearLayout with the 2 TextViews as a header view for the ListView using addHeaderView

There is ScrollView which could solve your problem, for example
<ScrollView>
<LinearLayout>
......
//More code here
</LinearLayout>
</ScrollView>
but remember Scroll View can only have only and only one child, and that child could have its own further child but ScrollView could have only one child. My this code is to give you idea it will not work by just copy and paste you have to made width , height etc adjustment according to your need

Related

RelativeLayout : giving priority to a view

i will try to simplify as much as I can without changing the real xml:
<TextView
android:id="#+id/txtTitleDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"/>
<android.support.v4.view.ViewPager
android:id="#+id/datePickerPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtTitleDate"/>
<TextView
android:id="#+id/txtPriorityDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:layout_below="#+id/datePickerPager"
android:layout_alignParentBottom="true"/>
<LinearLayout
android:id="#+id/layoutPriorityDate"
android:layout_width="200dp"
android:layout_height = "wrap_content"
android:layout_alignTop="#+id/txtPriorityDate"
android:layout_alignParentRight="true"
android:orientation="vertical">
<!-- omitted -->
</LinearLayout>
Without the ViewPager, I had enough space to show the LinearLayout in the bottom. But this ends up with the ViewPager taking up more space than it should, and flowing over the parent view.
How can i give priority to the LinearLayout to for the use of space without any hardcoded size values?
Thanks.
You can solve this by using the android:layout_weight attribute from LinearLayout.
As explained on the Linear Layout site from the API Guide:
This attribute assigns an "importance" value to a view in terms of how
much space is should occupy on the screen. A larger weight value
allows it to expand to fill any remaining space in the parent view.
Child views can specify a weight value, and then any remaining space
in the view group is assigned to children in the proportion of their
declared weight. Default weight is zero.
In your case, nesting the other views in a LinearLayout would do the job:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<TextView
android:id="#+id/txtTitleDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginBottom="30dp"/>
<android.support.v4.view.ViewPager
android:id="#+id/datePickerPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_below="#+id/txtTitleDate"/>
<TextView
android:id="#+id/txtPriorityDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:layout_below="#+id/datePickerPager"
android:layout_weight="2"
android:layout_alignParentBottom="true"/>
<LinearLayout
android:id="#+id/layoutPriorityDate"
android:layout_width="200dp"
android:layout_height = "wrap_content"
android:layout_alignTop="#+id/txtPriorityDate"
android:layout_alignParentRight="true"
android:layout_weight="2"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
You should test a bit with different values for the weights to fit your precise needs.

AbsListView LayoutParams Set Layout Gravity

How can I set the layout gravity for AbsListView.LayoutParams?
I am using this in a custom array adapter where I need to set the layout gravity based on a variable. My list child contains a linear layout root but in the adapter, I cannot set the LinearLayout params as i get the error that LinearLayout.LayoutParams cannot be cast to AbsListView.Layout params.
So, I tried to set the layout_gravity for an AbsListView, but it has no option for a gravity.
How can I set the layout_gravity programatically in this case?
EDIT: (Added source code)
The Listview Parent
<?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"
android:background="#drawable/messaging_background"
android:orientation="vertical"
android:padding="2dp" >
<ListView
android:id="#+id/messageThread"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/list_separator"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:cacheColorHint="#00000000"
android:divider="#null"
android:dividerHeight="2dp"
android:fastScrollEnabled="false"
android:listSelector="#00000000"
android:transcriptMode="disabled" />
<View
android:id="#+id/list_separator"
android:layout_width="match_parent"
android:layout_height="1.0dip"
android:layout_above="#+id/footer"
android:background="#android:color/white" />
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/filter"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.1"
android:background="#null"
android:contentDescription="#string/message_filter_button"
android:scaleType="fitCenter"
android:src="#drawable/filter" />
<EditText
android:id="#+id/send_message_text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:imeOptions="actionSend"
android:inputType="textMultiLine"
android:maxLines="6"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarDefaultDelayBeforeFade="200"
android:scrollbarFadeDuration="300"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
<ImageButton
android:id="#+id/send"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.1"
android:background="#null"
android:contentDescription="#string/send_message_button"
android:scaleType="fitCenter"
android:src="#drawable/send_btn" />
</LinearLayout>
</RelativeLayout>
The Listview Child
<?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="match_parent"
android:background="#drawable/incoming"
android:orientation="horizontal" >
<TextView
android:id="#+id/messaging_bubble_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="sans-serif-light"
android:maxWidth="200dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textIsSelectable="true" />
<TextView
android:id="#+id/messaging_bubble_time"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:paddingRight="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textIsSelectable="false" />
<ImageView
android:id="#+id/messaging_bubble_megaphone"
android:layout_width="20dp"
android:layout_height="match_parent"
android:contentDescription="#string/message_child_megaphone"
android:src="#drawable/megaphone"
android:visibility="gone" />
</LinearLayout>
What should I call in the getView of my adapter to push the child to the right according to a variable in the adapter?
EDIT 2:
A possible solution:
Wrap the whole child under another element under the root and then you can set gravity instead of layout gravity which can be easily done using the setGravity() method.
<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" >
<LinearLayout
android:id="#+id/messaging_bubble_wrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/incoming"
android:orientation="vertical"
tools:ignore="UselessParent" >
The second linear layout is useless but having it gets the job done!
To change the gravity you could wrap the existent row layout in another layout(FrameLayout for example) and then simply set the gravity on the LinearLayout from that extra layout(which now can be moved inside the parent).
To avoid adding an extra level in the layout you could keep the current row layout and replace the root LinearLayout with a RelativeLayout and change the position of the views inside it according to your desired gravity.
I solved this problem by creating another "useless view (in eclipse's words)" under my root element.
I set the width of the parent to match_parent and then changed the gravity to move the "useless view" left or right. The setGravity(Gravity.LEFT) method is allowed on the AbsListView.
I think you need to get rid of the ListView. Replace the ListView with a FrameLayout and use the layoutinflater to add the "listview" child into the FrameLayout. When using the layoutinflater set the FrameLayout.LayoutParams with the gravity property assigned to the root of the child (LinearLayout). Good luck.
You can set the gravity of the child's content with setGravity():
View child;
boolean left;
child.setGravity(left ? Gravity.LEFT : Gravity.RIGHT);
And the child should be changed to android:layout_width="match_parent".

Android: even spacing between view aligned vertically

My layout have 4 EditText views aligned vertically. but there is empty spacing after the last EditText. I want to align these views evenly so that the spacing b/w, before and after the views is same.
How to achieve this?
--nehatha
You can wrap every of your EditText to a FrameLayout with equal layout weight and set the Gravity of those frame layouts to center (or layout_gravity of EditTexts).
Edited:
Well, my initial solution leaves less space at top and bottom than between the edit texts. But this one works fine: simply add FrameLayout between each view and at top and bottom with layout_weight="1":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
>
<FrameLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
></FrameLayout>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
></EditText>
<FrameLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
>
</FrameLayout>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
></EditText>
<FrameLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
>
</FrameLayout>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
></EditText>
<FrameLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
>
</FrameLayout>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
></EditText>
<FrameLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
>
</FrameLayout>
</LinearLayout>
if you are using xml, then in all EditText add this tag,
android:layoutMargin = "5dip"
and in your parent layout (i ll assume its LinearLayout )
android:layoutHeight = "wrap_content"
Updated
adding hard coded values does't harm your code, since "dip" and "dp" are all units which get adapted to different screen resolutions. it has zero affect evn if the screen resolution changes..
chek this.. http://developer.android.com/resources/samples/MultiResolution/index.html

android:layout_height 50% of the screen size

I just implemented a ListView inside a LinearLayout, but I need to define the height of the LinearLayout (it has to be 50% of the screen height).
<LinearLayout
android:id="#+id/widget34"
android:layout_width="300px"
android:layout_height="235px"
android:orientation="vertical"
android:layout_below="#+id/tv_scanning_for"
android:layout_centerHorizontal="true">
<ListView
android:id="#+id/lv_events"
android:textSize="18sp"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_scanning_for"
android:layout_centerHorizontal="true">
</ListView>
</LinearLayout>
Is that possible?
I did something similar for a button and an EditText, but doesn't seem to work on Layouts.
This is my Code:
//capture the size of the devices screen
Display display = getWindowManager().getDefaultDisplay();
double width = display.getWidth();
//my EditText will be smaller than full screen (80%)
double doubleSize = (width/5)*4;
int editTextSize = (int) doubleSize;
//define the EditText
userName = (EditText) this.findViewById(R.id.userName);
password = (EditText) this.findViewById(R.id.password);
//set the size
userName.setWidth(editTextSize);
password.setWidth(editTextSize);
Set its layout_height="0dp"*, add a blank View beneath it (or blank ImageView or just a FrameLayout) with a layout_height also equal to 0dp, and set both Views to have a layout_weight="1"
This will stretch each View equally as it fills the screen. Since both have the same weight, each will take 50% of the screen.
*See adamp's comment for why that works and other really helpful tidbits.
This is easy to do in xml. Set your top container to be a LinearLayout and set the orientation attribute as you wish. Then inside of that place two linearlayouts that both have "fill parent" on width and height. Finally, set the weigth attribute of those two linearlayouts to 1.
This is my android:layout_height=50%
activity:
<?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="vertical" >
<LinearLayout
android:id="#+id/alipay_login"
style="#style/loginType"
android:background="#27b" >
</LinearLayout>
<LinearLayout
android:id="#+id/taobao_login"
style="#style/loginType"
android:background="#ed6d00" >
</LinearLayout>
</LinearLayout>
style:
<style name="loginType">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">0.5</item>
<item name="android:orientation">vertical</item>
</style>
best way is use
layout_height="0dp"
layout_weight="0.5"
for example
<WebView
android:id="#+id/wvHelp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5" />
<TextView
android:id="#+id/txtTEMP"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:text="TextView" />
WebView,TextView have 50% of the screen height
To make sure the height of a view is 50% of the screen then we can create two sub LinearLayouts in a LinearLayout. Each of the child LinearLayout should have "android:layout_weight" of 0.5 to cover half the screen
the parent LinearLAyout should have "android:orientation" set to vertical
.
.
here is code for your reference....
this code contains two buttons of height half the screen
<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:layout_weight="0.5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:padding="10dp"
android:layout_weight="0.5"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="button1"
android:id="#+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
/>
<Button
android:padding="10dp"
android:layout_weight="0.5"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="button2"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="0.5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
This kind of worked for me.
Though FAB doesn't float independently, but now it isn't getting pushed down.
Observe the weights given inside the LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/andsanddkasd">
<android.support.v7.widget.RecyclerView
android:id="#+id/sharedResourcesRecyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="bottom|right"
android:src="#android:drawable/ic_input_add"
android:layout_weight="1"/>
</LinearLayout>
Hope this helps :)
You should do something like that:
<LinearLayout
android:id="#+id/widget34"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_below="#+id/tv_scanning_for"
android:layout_centerHorizontal="true">
<ListView
android:id="#+id/lv_events"
android:textSize="18sp"
android:cacheColorHint="#00000000"
android:layout_height="1"
android:layout_width="fill_parent"
android:layout_weight="0dp"
android:layout_below="#+id/tv_scanning_for"
android:layout_centerHorizontal="true"
/>
</LinearLayout>
Also use dp instead px or read about it here.
it's so easy if you want divide your screen two part vertically ( top30% + bottom70%)
<LinearLayout
android:id="#+id/LinearLayoutTop"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayoutBottom"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
</LinearLayout>
To achieve this feat, define a outer linear layout with a weightSum={amount of weight to distribute}.
it defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.Another example would be set weightSum=2, and if the two children set layout_weight=1 then each would get 50% of the available space.
WeightSum is dependent on the amount of children in the parent layout.
You can use android:weightSum="2" on the parent layout combined with android:layout_height="1" on the child layout.
<LinearLayout
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:weightSum="2"
>
<ImageView
android:layout_height="1"
android:layout_width="wrap_content" />
</LinearLayout>

How to make layout with View fill the remaining space?

I'm designing my application UI. I need a layout looks like this:
(< and > are Buttons). The problem is, I don't know how to make sure the TextView will fill the remaining space, with two buttons have fixed size.
If I use fill_parent for Text View, the second button (>) can't be shown.
How can I craft a layout that looks like the image?
Answer from woodshy worked for me, and it is simpler than the answer by Ungureanu Liviu since it does not use RelativeLayout.
I am giving my layout for clarity:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width = "80dp"
android:layout_weight = "0"
android:layout_height = "wrap_content"
android:text="<"/>
<TextView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_weight = "1"/>
<Button
android:layout_width = "80dp"
android:layout_weight = "0"
android:layout_height = "wrap_content"
android:text=">"/>
</LinearLayout>
If <TextView> is placed in a LinearLayout, set the Layout_weight proprty of < and > to 0 and 1 for the TextView.
If you're using a RelativeLayout, align < and > to the left and right and set "Layout to left of" and "Layout to right of" property of the TextView to the ids of < and >.
If you use RelativeLayout, you can do it something like this:
<RelativeLayout
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
<ImageView
android:id = "#+id/my_image"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentTop ="true" />
<RelativeLayout
android:id="#+id/layout_bottom"
android:layout_width="fill_parent"
android:layout_height = "50dp"
android:layout_alignParentBottom = "true">
<Button
android:id = "#+id/but_left"
android:layout_width = "80dp"
android:layout_height = "wrap_content"
android:text="<"
android:layout_alignParentLeft = "true"/>
<TextView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_toLeftOf = "#+id/but_right"
android:layout_toRightOf = "#id/but_left" />
<Button
android:id = "#id/but_right"
android:layout_width = "80dp"
android:layout_height = "wrap_content"
android:text=">"
android:layout_alignParentRight = "true"/>
</RelativeLayout>
</RelativeLayout>
Using a ConstraintLayout, I've found something like
<Button
android:id="#+id/left_button"
android:layout_width="80dp"
android:layout_height="48dp"
android:text="<"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/left_button"
app:layout_constraintRight_toLeftOf="#+id/right_button"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/right_button"
android:layout_width="80dp"
android:layout_height="48dp"
android:text=">"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
works. The key is setting the right, left, top, and bottom edge constraints appropriately, then setting the width and height to 0dp and letting it figure out it's own size.
It´s simple
You set the minWidth or minHeight, depends on what you are looking for, horizontal or vertical.
And for the other object(the one that you want to fill the remaining space) you set a weight of 1 (set the width to wrap it´s content), So it will fill the rest of area.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|left"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="80dp"
android:layout_height="fill_parent"
android:minWidth="80dp" >
</LinearLayout>
you can use high layout_weight attribute. Below you can see a layout where ListView takes all free space with buttons at bottom:
<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"
tools:context=".ConfigurationActivity"
android:orientation="vertical"
>
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1000"
/>
<Button
android:id="#+id/btnCreateNewRule"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Create New Rule" />
<Button
android:id="#+id/btnConfigureOk"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ok" />
</LinearLayout>
You should avoid nesting 2 relative layout since relative layout always make 2 pass for drawing (against 1 for any other type of layout). It becomes exponential when you nest them. You should use linear layout with width=0 and weight=1 on the element you want to fill the space left.
This answer is better for performance and the practices. Remember: use relative layout ONLY when you don't have other choice.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="#+id/prev_button"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="<" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
android:gravity="center"
android:text="TextView" />
<Button
android:id="#+id/next_button"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text=">" />
</LinearLayout>
</LinearLayout>
For those having the same glitch with <LinearLayout...> as I did:
It is important to specify android:layout_width="fill_parent", it will not work with wrap_content.
OTOH, you may omit android:layout_weight = "0", it is not required.
My code is basically the same as the code in https://stackoverflow.com/a/25781167/755804 (by Vivek Pandey)
When using a relative layout, you can make a view stretch by anchoring it to both of the views it's supposed to stretch toward. Although the specified height will be disregarded, Android still requires a height attribute, which is why I wrote "0dp". Example:
<View
android:id="#+id/topView"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_marginTop="8dp"/>
<View
android:id="#+id/stretchableView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/topView"
android:layout_above="#+id/bottomView"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="true"/>
<View
android:id="#id/bottomView"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="16dp"/>
You can use set the layout_width or layout_width to 0dp (By the orientation you want to fill remaining space).
Then use the layout_weight to make it fill remaining space.
use a Relativelayout to wrap LinearLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:round="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text="<"/>
<TextView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_weight = "1"/>
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text=">"/>
</LinearLayout>
</RelativeLayout>`
i found
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
android:fontFamily="casual"
android:text="(By Zeus B0t)"
`` android:textSize="10sp"
android:gravity="bottom"
android:textStyle="italic" />

Categories

Resources