I'm trying to put a TextView on top, another on bottom, and a ListView in between using the remaining space.
However, my ListView is taking all the space to the bottom, making the last TextView to not even show up. The only way I can make it show up is to give the ListView a fixed height, like 100dp.
My layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/id_task_list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_task_list"
style="?android:attr/listSeparatorTextViewStyle" />
<ListView
android:id="#+id/id_task_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/id_task_list_title" />
<TextView
android:id="#+id/id_task_list_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/id_task_list_view"
android:text="OMG Test" />
</RelativeLayout>
Use LinearLayout as follows:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="#+id/id_task_list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_task_list"
style="?android:attr/listSeparatorTextViewStyle" />
<ListView
android:id="#+id/id_task_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="#+id/id_task_list_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OMG Test" />
</LinearLayout>
This should solve your issue by placing your ListView between the TextViews using both android:layout_above and android:layout_below :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/id_task_list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_task_list"
style="?android:attr/listSeparatorTextViewStyle" />
<TextView
android:id="#+id/id_task_list_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="OMG Test" />
<ListView
android:id="#+id/id_task_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/id_task_list_test"
android:layout_below="#id/id_task_list_title"/>
</RelativeLayout>
It can be achieved one of the two ways
1) Using Linear Layout (Straight forward)
2) Using Relative Layout with referencing the view position with respected to the other views.
In your case
1) Change Relative to LinearLayout and work will be done.
2)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/id_task_list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_task_list"
style="?android:attr/listSeparatorTextViewStyle" />
<ListView
android:layout_below="#+id/id_task_list_title"
android:id="#+id/id_task_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/id_task_list_title" />
<TextView
android:layout_below="#+id/id_task_list_view"
android:id="#+id/id_task_list_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/id_task_list_view"
android:text="OMG Test" />
</RelativeLayout>
This can be done through coding by getting the height of screen and alloting layout param width to list view like this
height of list view=screen height-(height of textView1+Height of textView2).
Instead of Using RelativeLayout , use LinearLayout with Orientation Vertical.
I think it solves your problem
Thank you
Related
I want to show the word "STATS" above a Listview containing either 2 or 3 list items. I want the ListView at the bottom edge of the screen and the remaining space to be filled by the "STATS" TextView and the text centered vertically and horizontally. Basically a single word floating lonely in the exact middle of the remaining space.
I cannot do this in the "drag and drop" layout editor because it shows 8 placeholder items in the ListView, so I can't snap to the edges of things properly.
I've got the ListView how I want it, but the "STATS" is causing problems.
I've tried all sorts of things (apart from the right one, obviously). Here's my currently broken layout...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="uk.co.xxxxxxxxxxx.xxxxxxx.MainActivity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom"
>
<TextView
android:id="#+id/tv_reboot_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:textAlignment="center"
android:text="#string/stats"
/>
<ListView
android:id="#+id/lv_app_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/reboot_screen_items"
android:layout_gravity="center_horizontal|center_vertical"
/>
</LinearLayout>
I'm sure it's something trivial to do but its driving me mad.
Try this, it works well
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_reboot_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:textAlignment="center"
android:text="STATE"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center" />
<ListView
android:id="#+id/lv_app_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/reboot_screen_items"
android:layout_gravity="bottom" />
</LinearLayout>
Result
Can you try this?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="uk.co.xxxxxxxxxxx.xxxxxxx.MainActivity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/tv_reboot_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:textStyle="bold"
android:textAlignment="center"
android:gravity="center"
android:text="STATS"
android:layout_above="#+id/lv_app_info"
/>
<ListView
android:id="#+id/lv_app_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/values"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
/>
</RelativeLayout>
You can use RelativeLayout to fill up the remaining space to show your TextView.
Use gravity and layout_gravity attributes.
Gravity and layout_gravity
I am making an app where there is a custom search bar. I want there to be a search part that is big, a spinner for region selection that is a small square, and a search button that is also a small square.
Here is what I am talking about.
The code I have right now is this:
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:hint="#string/summoner_name"
android:layout_width="wrap_content"
android:layout_weight="75"
android:layout_height="wrap_content"
android:id="#+id/summoner_name"/>
<Spinner
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/regions">
</Spinner>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/search_button"/>
</LinearLayout>
However, this makes the search half the screen, and the button for 2/5's the screen, and the spinner for a smaller ammount. Anyone know how to accomplish this?
Thanks :)
give weight 1 to EditText android:layout_weight="1" , and remove weight tags from other components.
Also you will require to set Spinners width statically, if above doesnt work because of Spinner's width part
like this?
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:layout_width="wrap_content"
android:layout_weight="75"
android:layout_height="wrap_content"
android:id="#+id/summoner_name"/>
<Spinner
android:layout_weight="1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/regions"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/search_button"/>
</LinearLayout>
</LinearLayout>
Good day!
I have a ListView with news list, but if the title is very long- he lost on the edge of the screen.
How do I text-wrap to the next line?
I read many posts about this, but can not find solution that works to me.
Here the code:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.app.SportNewsReaderApp.NewsActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:textSize="16.5dip"
android:padding="2dip"
android:minLines = "1"
android:maxLines = "100"
android:singleLine = "false"
android:inputType="textMultiLine"
android:scrollHorizontally="false"
android:lines="2"
/>
</LinearLayout>
How do I text-wrap to the next line?
by removing minLines attribute
I got a little problem: I can't put views to the verry right or to the verry left of my screen... In the graphical layout preview of eclipse, it looks like this:
If I upload it to my Galaxy Tab 3 (10.1"), it looks exactly the same...
Here's the XML-Code:
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/layout"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
As you can see, android:layout_alignParentLeft and android:layout_alignParentTop are set to true...
#dimen/activity_vertical_margin and #dimen/activity_horizontal_margin have their default value of 16dp.
It don't concerns just TextViews, but also every other view I tried out. If I select other screen sizes than 10.1", the view is at the right place.
Anybody can help me?
Thank you!
Try this:
<RelativeLayout 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"
android:id="#+id/layout"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
You have given padding to the parent layout. That's why you couldn't place your view close to left.
Remove these lines from your RelativeLayout
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
Remove the paddings of your Relative Layout
<RelativeLayout 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"
android:id="#+id/layout"
tools:context=".MainActivity" >
In my app I've got an activity with an EditText view on top of a ListView. The simple problem I'm having is that the topmost element of the list is "underneath" the EditText view.
I'm not very experienced in Android and so I have no idea how to fix this!
My xml looks like this
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".AcceptDeliveriesMainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- Editext for Search -->
<EditText android:id="#+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/search_field_text"
android:inputType="textVisiblePassword"/>
</LinearLayout>
<!-- List View -->
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
as long as your linearlayout just contains one element, so you can delete it.
you have to use android:layout_below="#+id/inputSearch" in your listview
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".AcceptDeliveriesMainActivity" >
<EditText
android:id="#+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textVisiblePassword" />
<!-- List View -->
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/inputSearch" />
</RelativeLayout>
If you don't want to remove the linearlayout
you can use this:
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".AcceptDeliveriesMainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/llID"
android:orientation="horizontal" >
<!-- Editext for Search -->
<EditText
android:id="#+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textVisiblePassword" />
</LinearLayout>
<!-- List View -->
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/llID" />
</RelativeLayout>
In the previous code, I just gave an ID to the linearlayout and made the listview below it
You should replace your parent RelativeLayout with a LinearLayout.Or place the child views in relation to each other like android:layout_below which will then align children in relation to each other. Else each child will align to top left of the parent.
Tell your 2nd ListView to layout:below your LinearLayout.