ListView + textview below layout problems - android

So I've got this layout scenario that I havent found any desirable solution for. I've made a paint of it, i hope it's understandable.
Right now my ListView is always above the LinearLayout at the bottom. When the ListView has few items, the TextView is just below and it looks fine. However when it has many items, it crushes the TextView in between, which should always be there no matter what, between the LW and bottom container.
Could anyone give me a xml example that achieves just this?

You can wrap the ListView inside a LinearLayout and set weights to this Linearlayout, the TextView and the bottom LinearLayout. (You have to wrap the ListView inside a LinearLayout because you can't apply weights to a ListView).

If I don't missundestand you, You try to create a ListView such as wrap_content? and the TextView is always below the ListView? If i'm not wrong, so, this is what you need:
<LinearLayout // Your list view and TextView in a same linear layout
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="400dp" // With this specific height, your TextView will
// always in this range!
android:orientation="vertical" >
<ListView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.1" >
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="91dp"
//layout_gravity = "right"?? I'm not sure, you should make it right alignment
/>
</LinearLayout>
<LinearLayout /> // This is your linear layout at the Bottom, 2 Lineary layout is
// putted in a Relative layout
The layout above give you a ListView at the top, and the TextView is below, the LinearLayout at the Bottom. Try it out!!

Related

How to disable default scroll behavior of an EditText?

The title of question might look similar to other questions but It's not. I have several strange problem that I am not being able to solve it.
I have a xml layout and the structure of that layout looks like below.
LinearLayout
|
ScrollView
|
LinearLayout
|
TextView
EditText(multiLine)
The xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"/>
<EditText
android:layout_width="match_parent"
android:inputType="textMultiLine"
android:layout_height="match_parent"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Now let's discuss about problem.
If I keep adding new line to the editText, the editText itself scroll and the TextView which is located above EditText is not scroll. But what I want is EditText not to scroll and leave the job for scrollView so that the textView which is loacated above EditText also scroll along with EditText when new Line is added.
ScrollView behave ok when I press back(usually when the editText is not focused anymore).
Setting to
<ScrollView
...
layout_height="wrap_content">
fix the problem but the desired height then compromised. Same thing happen with Horizontal scrollView also.
Is there any way to solve this problem ? Detailed Answer will be helpful.
Set your EditText's height to wrap_content so it expands instead of scrolling. Set your LinearLayout's height to wrap_content so it's the size of its contents, i.e. the TextView and EditText.
The ScrollView should be whatever height you want for your "window" into the contents layout, the LinearLayout that moves up and down "behind" the ScrollView.
The scrolling happens if that contents layout is too big to display in the visible ScrollView, which is why that contents layout should always be wrap_content (the size of the contents) and never match_parent (the size of the scroll view "window"). If the contents are always the same size as the scroll view, they'll never need to scroll, right?

LinearLayout should fill minimum space

I have this layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text" />
</LinearLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"/>
</LinearLayout>
But if in first text view (that in LinearLayout) is a lot of text, second text view goes off the screen. This can be solved by setting android:layout_weigh="1" to linear layout. But in this case linear layout will fill all space, and I need that linear layout fill minimum space when in it short text, and not hiding second text view when in linear layout a lot of text.
If the first textview occupies too much horizontal space, the second textview is rendered outside the screen...
To avoid rendering the second textview outside the screen, you need to limit the horizontal size of the first textview. You could give the first textview an android:layout_width with a fixed numeric value. You could set android:singleLine to false. You could set android:maxWidth to an appropriate value. (I haven't tried these solutions, but they should work).
Alternatively, you may consider using a single textview instead of two in a row.
A solution is to put your first TextView in a ScrollView, and put your second TextView in a fixed footer which will never be hidden (this can be done with a RelativeLayout).
If you have too much text in your first TextView then you will be able to scroll it while still showing the second TextView.

Android 2.3.3 ListView with ScrollView

I have a ListView and TextView. They are both in a LinearLayout. And this LinearLayout is inside the ScrollView. So it looks like this:
Linearlayout
ScrollView
linearlayout
ListView
TextView
Linearlayout
ScrollView
linearlayout
But you can't scroll through it.
It just displays the ListView. And the ListView is cropped for some reason.
Is this normal? If not then how to solve this?
use following xml code.If i am right you don't need Listview.you can do this with only text view. Here is code for Scrolling Listview.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="#+id/linearLayout2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="30px" >
</TextView>
</LinearLayout>
Yup, this is a problem because list-view is already a scroll-view. Basically, when you encapsulate a scroll-view inside another scroll-view, android has no way to know which scroll-view the user wants to scroll. Generally, the bottom-most layer is scrolled. iOS still gets you through this problem in certain cases but android never does. You'll get weird scrolling characteristics. You can put a horizontal scroll-view inside a vertical one and that'll work fine. So I'd recommend changing your design in a way that you do not use the ScrollView
I've faced same problem.
Just change your ScrollView tag into HorizontalScrollView inside your xml and you're done.

Getting Views to fit

I have a main.xml with a LinearLayout with three items inside, LinearLayout, ListView and LinearLayout. I would like the ListView to be as big as possible while keeping the bottom LinearLayout always showing and not shrunk or knocked off the screen.
I have tried making the ListView height fillparent, but then the bottom LinearLayout doesn't show up, same with wrapcontent, since the ListView has lots of content.
When I make the ListView a set size, say 300dp, I have space below the bottom LinearLayout. I tried to fix this by making the top level LinearLayout gravity=fill, but that didn't help.
Also, depending on the android I try it on, the bottom LinearLayout will drop off the screen, or get shrunk.
In case it's relevant, the top level LinearLayout is set to fillparent for height.
My goal is to keep the top and bottom LinearLayout to wrap their content, and the middle ListView fill what's left... any suggestions?
Thanks in advance for your efforts!
I believe you can just add android:layout_weight="1" to the ListView, with the ListView set to a height of fill_parent, and the two LinearLayouts set to wrap_content. Regardless, I usually prefer to use a RelativeLayout. You can specify the header to align to the top of the screen, the footer to align to the bottom, and the ListView to fill the space in between, like so:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
>
//...insert stuff here
</LinearLayout>
<LinearLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
//...insert stuff here
</LinearLayout>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/footer"
android:layout_below="#id/header"
/>
</RelativeLayout>

Buttons below ListView are not fixed to the bottom of the screen when the ListView is empty

I have a Layout which contains a ListView some buttons below it.
What I want is for the buttons to be fixed to the bottom of the screen.
When the ListView has several items (more than what fits the screen) the code is working properly and the buttons appear in the bottom of the screen.
However, when the ListView is empty or does not require scrolling, the buttons are coming up the screen (they are not staying fixed in the bottom).
Here's the layout (some details omitted but I can add them if necessary):
<LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView />
<ListView
android:width="fill_parent"
android:height="wrap_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|bottom"
android:orientation="horizontal"
android:layout_weight="0" />
<Button
android:layout_weight="1" />
<Button
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
I expected setting the "gravity" of the second LinearLayout to "bottom" to make the buttons fixed to the bottom of the screen, but it's not working in the mentioned case.
Any ideas?
Thanks.
You have the layout_height of your top LinearLayout set to wrap_content. This means that it will shrink as its contents shrink. Try setting it to fill_parent instead. Or, give it a layout_weight of 1.
One of the options is to create TextView which will be shown instead of listview, when it is empty.
You do it with listView.setEmptyView(emptyView)
Then set layout_weight of this TextView to same weight as your listview. So when listview is empty, its place will be taken by emptyView and buttons will stay fixed at the bottom.
Also set your emptyView to android:gravity="center".
Other option:
In vertical RelativeLayout add to the layout with buttons property android:layout_alignParentBottom="true".
All the stuff with listView put inside LinearLayout and add to it android:layout_above="#+id/buttons_layout".
That's it. Elements inside LinearLayout will take their place depending on weight but whole layout wont go beyond buttons.

Categories

Resources