I have a ListView which I am populating using adapter arrays. I would like to add a search bar there, but whenever I add a search bar it repeats it, so I ended up making 2 layout files: one for the search-bar and the other one for the item-list.
ListView in my Android:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="8dp"
android:paddingRight="16dp">
<ImageView
android:id="#+id/icon"
android:layout_width="40dp"
android:layout_height="70dp"
android:background="#color/bckground"
android:src="#drawable/placeholder" /> <!-- ree image-->
<LinearLayout
android:id="#+id/textContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/icon"
android:orientation="vertical"
android:paddingTop="16dp">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
tools:text="name" />
<TextView
android:id="#+id/surname"
android:layout_width="match_parent"
tools:text="surname" />
</LinearLayout>
ListView layout print-screen:
Search-bar file layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/searchtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="Search.." />
<Button
android:id="#+id/sumbit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search" />
</LinearLayout>
Search-bar layout print-screen:
I would like to have the search-bar and item list in one layout.
How can I combine these two layout files together?
Add your listview in layout file which contains search bar.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/searchtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:text="Search.." />
<Button
android:id="#+id/sumbit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search" />
<!--Add your list view here-->
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
I would like to have the search-bar and item list in one layout. How can I combine these two layout files together?
You can't. These are conceptually two different layouts. One describes each item in the list, which is provided by the ListView adapter, and the other describes the top level view that contains the ListView and any other views that you want as part of that layout.
Related
What I have so far
I have a recyclerview and buttons below it. On initial state everything looks good: recyclerview makes space for a button and I can scroll down to the end of the list and the last item won't be hidden by the button. When I pick one of the items, one button becomes invisible and other buttons become visible.
Actual behavior
When buttons change their visibility, recyclerview fills parent, so part of the content is being hidden under the recently appeared buttons.
Expected behavior
I expect that recyclerview will shrink its height leaving some space for the buttons.
What I've tried to do
I've tried to apply this to a swiperefreshlayout:
android:layout_height="0dp"
android:layout_weight="1"
but now recyclerview will just disappear when first button hides and others show up.
Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<include
layout="#layout/toolbar_actionbar" />
<TextView
android:gravity="center"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="center|center"
android:maxLines="10"
android:textSize="20dp"
local:MvxBind="Text EmptyMessage;Visibility Visibility(ShowEmptyMessage)" />
<MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout
android:id="#+id/timeslotsSwipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/getTimeslotButton"
android:layout_below="#+id/appbar"
local:layout_behavior="#string/appbar_scrolling_view_behavior"
local:MvxBind="Refreshing IsRefreshingTimeslots; RefreshCommand RefreshTimeslotsCommand">
<!-- Here is my recycler view -->
<MvxRecyclerView
android:id="#+id/MyTimeslotsRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="ItemsSource Timeslots;Visibility Visibility(ShowList)"
local:MvxItemTemplate="#layout/item_my_timeslot" />
</MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout>
<!-- Here is my disappearing button -->
<android.support.v7.widget.AppCompatButton
android:id="#+id/getTimeslotButton"
android:text="Получить таймслоты"
android:theme="#style/ButtonPrimary"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
local:MvxBind="Click GetTimeslotsCommand; Visibility InvertedVisibility(IsEditModeEnabled)" />
<!-- Here are my appearing buttons.
I placed it inside of a relativelayout
to make a single visibility data binding -->
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
local:MvxBind="Visibility Visibility(IsEditModeEnabled)">
<android.support.v7.widget.AppCompatButton
android:id="#+id/copyToSmsButton"
android:text="Отправить по SMS"
android:theme="#style/ButtonPrimary"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginBottom="0dp"
android:layout_alignParentLeft="true"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="#id/copyToSmsButton">
<android.support.v7.widget.AppCompatButton
android:id="#+id/deleteTimeslotButton"
android:text="Удалить"
android:theme="#style/ButtonSecondary.Red"
android:layout_marginTop="0dp"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/moveTimeslotButton"
android:text="Перенести"
android:theme="#style/ButtonSecondary"
android:layout_marginTop="0dp"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
I am working on list view..list view items display in vertical.but I am display items in horizontal but not show the items in horizontal.
and simple_list_item_1 is not working..I am changing gravity left or right but items show vertically..here is my code
here is content_main
<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:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Searach Now"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/editText" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/headingText">
</ListView>
</LinearLayout>
</RelativeLayout>
here is simple_list_item. XML
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"></TextView>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
></TextView>
</Relativelayout>
I know you are a beginner, recyclerView offers LayoutManagers for this purpose but as you are new it is good to know about listview as it is the base concept and simpler to setup than recyclerview.
Use Horizontal scrolling view and embed your list view in that. What this does is instead of positioning your list items in vertical it will do so in horizontal way with scroll option.
Must use way: As per Android Documentation RecyclerView is the BEST way to organize the items in listview and to be displayed horizontally
Advantages:
Since by using Recyclerview Adapter, ViewHolder pattern is
automatically implemented
Animation is easy to perform
Many more features
Sample:
survivingwithandroid.com
Just add the below block to make the ListView to horizontal from vertical
Code-snippet
LinearLayoutManager layoutManager= new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(layoutManager);
Reference:Horizontal ListView in Android?
Another way:
<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"
tools:context=".MainActivity">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#f00"
android:padding="10dp"
android:text="Button 1"
android:textColor="#fff"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#0f0"
android:padding="10dp"
android:text="Button 2"
android:textColor="#fff"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:background="#00f"
android:padding="10dp"
android:text="Button 3"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
Result code:
Problem with HorizontalScrollView is it shows horizontal bar and it is not recommended to use.
I hope it will help you.
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fillViewport="true">
<ListView
android:id="#+id/attackslist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff" />
</HorizontalScrollView>
I put a ListView inside an HorizontalScrollView because its content is way too long to fit in the screen, but what i get is that the list looks like this (the background color is there to show where the list's childs stop)
while i want all the childs equally spaced in there until the end of the list, which i get if the ListView is not inside a scrollbar
My custom layout I use in the adapter looks like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent">
<TextView
android:id="#+id/Moves"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="fill"
android:layout_weight="1"
android:gravity="center"
android:text="Moves"
android:textSize="14dp" />
<TextView
android:id="#+id/Hitboxactive"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="fill"
android:layout_weight="1"
android:gravity="center"
android:text="Hitbox Active"
android:textSize="14dp" />
</LinearLayout>
How should I handle this situation? Can I change the values during runtime?
Should I use a different View?
Thanks
I am creating a layout with a listview and some other views. I want complete layout to scroll with list so I am adding the other views as header. The problem is that my listview has some margin which is applied to the header as well.
What should I change so that the header looks something like this-
ListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_color">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:scrollbars="none"
android:id="#+id/lvProfileAddresses"
android:layout_gravity="center_horizontal"
android:divider="#color/transparent"
android:dividerHeight="10dp"
android:paddingBottom="10dp"
android:clipToPadding="false"
android:cacheColorHint="#color/background_color"
android:fadingEdgeLength="3dp"
/>
</LinearLayout>
Header
<?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"
android:background="#color/transparent"
>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:text="My Addresses"
android:textSize="20sp"
android:padding="10dp"
android:textColor="#color/black"
/>
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add New Address"
android:drawableRight="#drawable/small_list_arrow"
android:layout_margin="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#drawable/linearlayout_shadow"/>
</LinearLayout>
How to make the ListView's header have a different margin?
I think It would be better to not to add Header in a ListView, and keep a separate Header. May be you can use include for that.
Something like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_color">
<include
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
layout="#layout/header" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:scrollbars="none"
android:id="#+id/lvProfileAddresses"
android:layout_gravity="center_horizontal"
android:divider="#color/transparent"
android:dividerHeight="10dp"
android:paddingBottom="10dp"
android:clipToPadding="false"
android:cacheColorHint="#color/background_color"
android:fadingEdgeLength="3dp" />
</LinearLayout>
You just need that once in ListView at top. So I think this would be a better approach. Hope it helps.
EDIT:
As you need to keep header inside ListView for scrolling it along with ListView items, you can probably go with other way:
Remove the margin properties from entire ListView.
Apply the margin to ListView's row_item.xml. For this, you would need to include your row.xml's outer container inside another parent container, say LinearLayout. So margin will work.
Hope this helps.
If you want to do it with header you need to remove margin attributes from your listview because it affects all children and add the same margin for your listItem.
I run into the same problem recently, From what I saw on the platform some people are still struggling with this.
Based on my research It looks like the listview will override the header margins as well as paddings.
The solution will be:
to put your header viewgroup inside another viewgroup whose
android:layout_height="wrap_content" and android:layout_width="match_parent".
Then apply whatever margins you want to the inner viewgroup.
In your case the header layout would look like this :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop ="40dp" //For Example
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:background="#color/transparent">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:text="My Addresses"
android:textSize="20sp"
android:padding="10dp"
android:textColor="#color/black"/>
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add New Address"
android:drawableRight="#drawable/small_list_arrow"
android:layout_margin="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#drawable/linearlayout_shadow"/>
</LinearLayout>
</LinearLayout>
Hope this helps
I have a ListView with a custom view rendered by an adapter. It contains an image, some text and another ListView. In the getView method of adapter I creator another adapter to set up the embedded ListView.
It all works fine, but for some reason the height of the embedded ListView is only enough to view one list item. If I manually set the height to pixels I can see more items in the list. All heights are set to wrap_content so I'm not sure why it's not working.
My first ListView looks like this:
<ListView
android:id="#+id/newswire"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:cacheColorHint="#ffffff"
/>
The item that gets rendered for this ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
>
<ImageView
android:id="#+id/aggSparkAvatar"
android:layout_width="45dp"
android:layout_height="45dp"
android:scaleType="centerCrop"
/>
<LinearLayout
android:orientation="vertical"
android:paddingLeft="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/aggSparkName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#1B5F7C"
android:textStyle="bold"
/>
<TextView
android:id="#+id/aggSparkDateStamp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold"
/>
<View
android:layout_height="1dp"
android:layout_width="fill_parent"
android:background="#F2F2F2"
/>
<ListView
android:id="#+id/aggSparkList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cacheColorHint="#ffffff"
/>
</LinearLayout>
</LinearLayout>
Then for the embedded ListView each item looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
>
<ImageView
android:id="#+id/sparkAvatar"
android:layout_width="45dp"
android:layout_height="45dp"
android:scaleType="centerCrop"
/>
<LinearLayout
android:orientation="vertical"
android:paddingLeft="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/sparkTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold"
/>
<TextView
android:id="#+id/sparkText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
/>
<TextView
android:id="#+id/sparkDateStamp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#A2A2A2"
/>
</LinearLayout>
</LinearLayout>
it's never a good practice to include scrollable views inside another scrollable view. you should consider redesigning your application.
in your case, the ListView you're embedding is set to wrap_content, wrap_content - also not a recommended practice. a ListView is designed to show a list of items that collectively will be too big for the screen to show at once. using wrap_content, wrap_content doesn't make much sense.