Multiple ListViews with Custom Adapters in a Single Layout - android

I created a Custom Tab Navigator and now I need to know how can I use multiple ListViews which will receive Custom Adapters.
When I needed to use a Custom Adapter I created a ListView with id=android:list and set the class to Extends ListActivity. But now I think I can't do that...

To have multiple listViews on a single activity, don't need to extend ListActivity. Just add normal ListViews to the xml lauyout file and the reference them on the activity and set the adapters you want.
Example: xmlfile
<ListView android:id="#+id/list_view1" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
<ListView android:id="#+id/list_view2" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
On the activity:
setContentView(R.layout.xmlfile)...
ListView lv1 = (ListView) findViewById(R.id.list_view1);
ListView lv2 = (ListView) findViewById(R.id.list_view2);
lv1.setAdaper(new CustomAdapter1());
lv2.setAdaper(new CustomAdapter2());

#Nuno Gonçalves
A small mistake/optimisation in your XML-file:
In case of ListViews, it is better to define the layout_height and layout_width attributes both as fill_parent and scale them using the layout_gravity attributes. Setting the layout_height of a ListView to wrap_content is not optimal and can cause errors or performance problems.
But your solution will work in this case :)
Example:
<ListView android:id="#+id/list_view1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="1">
</ListView>
<ListView android:id="#+id/list_view2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="1">
</ListView>

Related

Android custom list view?

In my layout I need a custom list view like a box type.
How can do that ?
My XML:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="317dp"
android:gravity="top"
android:orientation="vertical"
android:background="#android:color/transparent"
>
<ListView android:id="#+id/slistview"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:divider="#2867EA"
android:dividerHeight="1dp"
android:cacheColorHint="#00000000"
>
</ListView>
</LinearLayout>
You should create custom list adapter and supply xml layout for each row.
here's a comprehensive tutorial that fits your needs:
Link
You can use customBaseadapter and viewHolder ...
Create a xml for row of the list view and set the background of the row and create a custom adapter and set adapter to list view.
custom list view

Edit Text and Text View not visible when ListView is set in Android

here my xml layout of my Android app (where I show all of my contacts)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"
android:text="Scegli il contatto:"
/>
<EditText
android:id="#+id/editTextSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
I fill ListView using a custom Adapter.
But textView and Edit Text are not visibile. I want to show in particular edit text where users can write initial letters of his contacts name)
Suggestions to get them visible?
I fill ListView ... But textView and Edit Text are not visibile.
If you can only see a ListView then it appears you're using a ListActivity and forgot to call setContentView(). So you aren't actually using your layout... you only see ListActivity's default ListView.
That said, your XML code will work, but here are some quick notes:
As I stated in my comment, the TextView might be hidden depending on what type of Activity or Fragment you are using:
A ListActivity will automatically bind the #+id/android:empty and #+id/android:list Views, so the "empty" TextView is only shown when the ListView is empty.
An Activity won't recognize #+id/android:empty on its own. All the Views should be visible.
fill_parent is deprecated, simply use match_parent
Setting a ListView's height to wrap_content forces the Adapter to draw the ListView multiple times... with your layout I recommend using match_parent.
I'd say it is visible pretty fine with your layout. But anyway, I'd make listview declared that way:
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
so it will take all the remaining free space

Adding margins within a fragment

In my application, I am using fragments. The problem is I am not able to set margin for each item in the list inside TitlesFragment extends ListFragment. Also I have to add margin to my complete set of data shown inside DetailsFragment extends Fragment. How can this be done? Please help me out. Thanks in advance.
Please see my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/bg"
android:orientation="vertical" >
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"
android:scrollbars="none"
android:smoothScrollbar="true" >
</ListView>
ListViews (along with Fragments, ListFragments, etc) appear to override the provided layout's root parameters (height, width), in order to fit the predefined View/Item dimensions into the parent. This also seems to happen with each of the layout's children Maximum dimensions, for instance, the TextView from AOSP's simple_list_item_1.xml
Since you are asking specifically for ListView item spacing, I would still not use dividerHeight as suggested by blackbelt, as this would change the centering of your original content (also the first item would not have the desired spacing from the top of the list, as the divider is below each item).
For ListView items, you can generally override this behavior by setting a custom layout (called by a custom adapter) with a top and bottom margin. See the following sample based simple_list_item_1.xml and note the margin modifiers at the end:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
/>
(you might need to provide inline values for non-public references to "?android:attr")
You would then have to call a custom adapter for your list (such as ArrayAdapter) and supply it with your custom list item XML.
You have to set the divider and the dividerHeight properties in your layout.xml file.
For instance
<ListView
android:id="#android:id/list"
android:divider="#android:color/transparent"
android:dividerHeight="5dip" />
in this way you will get all elements of the ListFragment spaced by 5 dip

use a single layout xml file for an activity?

Is it possible to use a single xml file for the "whole layout" (activity)?
Can you "embed" the row layout, that is in a separate file, to the listview in the main layout xml?
So it look something like this
<LinearLayout>
...
<ListView>
<LinearLayout>
<ImageView>
<TextView>
...
</LinearLayout>
</ListView>
</LinearLayout>
If it is possible, how is it done, how do you use "id", adapter etc then?
Because you could use
<listview>
...
</listview>
and not only
<listview .../>
...I thought it maybe could be possible to put the row layout directly there inside, but dont know how to do, to get it to work if it is possible. Is it?
Would like to have one single layout xml file, per activity, instead of many small parts. Would be easier to get the whole picture then I think and simplify stuff. Any other ways to do achieve this maybe?
Its definitely possible to use single xml for listview and its cell layout
It will go like this
You are creating two layouts in xml
<LinearLayout android:id="#+id/listview_layout"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<ListView android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:id="#+id/cell_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
.............do your cell layout here
</LinearLayout>
Here use listview_layout for setting your ContentView
setContentView(listview_layout)
and use cell_layout in any adapter to set cell layout
you will find good tutorials for setting custom adapters for listview on StackOverFlow
Hope this will solve your problem

Android ListView is not expanding as intended

My scenario is like this:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:background="#ffffeb">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
// Other stuff in between here..
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="12dp"
android:paddingRight="12dp">
<Button
android:id="#+id/insert_ad_ad_information_category_and_type_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="#string/insert_ad_ad_information_category_and_type_button"/>
<ListView
android:id="#+id/insert_ad_ad_information_parameters_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffeb"
android:cacheColorHint="#00000000"
android:divider="#00000000"
android:dividerHeight="0dp"
android:scrollbars="none"
android:listSelector="#android:color/transparent"/>
The problem I have is that I can't get my ListView to get the right height that I want.
I'll add a couple of components with my custom BaseAdapter class and everything there works as intended.
But when I then debug my application I can only see 1,5 out of 3 components in the list and the rest is hidden further down in the ListView.
But how can I make my ListView calculate how many components I have and get it to show all my components directly without having too scroll down?
Another thought is if I could populate any other kind of View with my BaseAdapter? Because the only reason I am using ListView is because of the setAdapter() method.
Appreciate all thoughts and comments :)
You cannot have a ListView in a ScrollView. Thats where the issue is.
As has been said you can't use ListView inside ScrollView.
If you want your listview have different types of items and scroll those with "normal" items
you do in fact need custom adapter. You can base it on BaseAdapter if you like
Adapter has to support your item types, and inflate appropriate layouts,

Categories

Resources