Creating a ListView with Checkboxes - android

I have a listview with each item containing an image, a textview and a checkbox. I am trying to have it so wherever you click within the item it highlights the checkbox. As stated in this response I set the focusable attribute of the checkbox to false, but I am still only able to click the checkbox to change its state. What's even more interesting is when I click on a checkbox every proceeding 10th item is also highlighted. Any suggestions?
Here is my xml for an item.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/stub"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="20dip"
android:layout_marginLeft="10dip"/>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"/>
</LinearLayout>
Main xml
<?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">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Clear Cache"/>
</LinearLayout>

create a CheckableLinearLayout or CheckableRelativeLayout for your ListView's item layout container,
set your listview's mode to 'multipleChoice'
find more details in the links below:
http://tokudu.com/2010/android-checkable-linear-layout/
http://www.marvinlabs.com/2010/10/custom-listview-ability-check-items/

in the process of populating the list, you can set a OnItemClickListener and change the checkbox to checked. try something like this:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ListItem item = (ListItem) adapter.getItem(position);
if(item.checkbox.isChecked())
item.checkbox.setChecked(false);
else
item.checkbox.setChecked(true);
}
});

in the main.xml
<?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" android:layout_marginTop="5dp">
<RelativeLayout
android:layout_width="450dp"
android:layout_height="25dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="3dp">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="450dp"
android:layout_height="25dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="3dp">
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Clear Cache"/>
</RelativeLayout>
</LinearLayout>
Hope this help you...

Related

3 vertical listViews in one scrollview

I need to implement such a screen :
So, I've created the adapter with ImageView, 2 TextViews and CheckBox.
I need to implement 3 listViews and make the screen scrollable.
I tried to implemen the solution, but that is not workable for me - so I made like this :
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:fillViewport="true"
android:orientation = "vertical" android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:weightSum="1.0"
>
<LinearLayout android:layout_weight="0.5"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:text="#string/textview_settings_categories"
style="#style/settings_label"/>
<ListView android:id="#+id/listView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
<LinearLayout android:layout_weight="0.25"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:text="#string/textview_settings_categories"
style="#style/settings_label"/>
<ListView android:id="#+id/listView2"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
<LinearLayout android:layout_weight="0.25"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:text="#string/textview_settings_categories"
style="#style/settings_label"/>
<ListView android:id="#+id/listView3"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
I also tried this one solution, but both not workable for me - the ScrollView can't be scrollable.
I also tried to implement 3 vertical ListFragments one above another, listviews are scrollable inside, but the scrollview is not - so I can't see the bottom of the screen.
It's not the best practice, but if you really want that functionality, you can just disallow the touch event of the parent scrollview once you touch it and re allow it once you leave the child container.
This is how I have done it before, where "listScanners" is my listview:
listScanners.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
And this is the part of my layout that is relevant to the question:
/* Theres more before this ... */
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarSize="0dp" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#null"
>
<TextView
android:id="#+id/emptylist1"
android:layout_width="450dp"
android:layout_height="80dp"
android:textColor="#6f6f6f"
android:textSize="20sp"
android:padding="3dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="No Scanners have been added..."/>
<ListView
android:id="#+id/listview_scanners"
android:layout_width="450dp"
android:layout_height="80dp"
android:padding="3dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:cacheColorHint="#00000000"
android:divider="#DEDEDE"
android:dividerHeight="1px">
</ListView>
</RelativeLayout>
/* Theres more after this ... */
Correct Way:
Ideally you should rather have something like this, where your layout looks like this:
Where it is very important to have this in your linear layout:
android:isScrollContainer="true"
isScrollContainer means that the linearlayout contains a view that scrolls, meaning that it can be a certain size within the linear layout, however, it may contain much more when you scroll it. Here is the layout:
<?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"
android:background="#afafaf"
android:isScrollContainer="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:background="#FF63FF9B"
android:layout_height="50dp"
>
<TextView
android:id="#+id/textview_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:text="View First List"/>
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginTop="5dp"
android:background="#null"
>
<ListView
android:id="#+id/listview1"
android:layout_width="fill_parent"
android:layout_height="130dp"
android:cacheColorHint="#00000000"
android:divider="#DEDEDE"
android:dividerHeight="1px">
</ListView>
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:background="#FF63FF9B"
android:layout_height="50dp"
>
<TextView
android:id="#+id/textview_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:text="View Second List"/>
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginTop="5dp"
android:background="#null"
>
<ListView
android:id="#+id/listview2"
android:layout_width="fill_parent"
android:layout_height="130dp"
android:cacheColorHint="#00000000"
android:divider="#DEDEDE"
android:dividerHeight="1px">
</ListView>
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:background="#FF63FF9B"
android:layout_height="50dp"
>
<TextView
android:id="#+id/textview_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:text="View Third List"/>
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginTop="5dp"
android:background="#null"
>
<ListView
android:id="#+id/listview3"
android:layout_width="fill_parent"
android:layout_height="130dp"
android:cacheColorHint="#00000000"
android:divider="#DEDEDE"
android:dividerHeight="1px">
</ListView>
</RelativeLayout>
</LinearLayout>
I just made this in 5 minutes and you get a layout looking like this: (this is just a draft, obviously just to give you an idea)
And last but not least, in your code where you display the lists, you will have something like this:
public class Screen_View_Lists extends Activity
{
BaseAdapter listAdapter;
ListView list1,list2,list3;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.screen_view_packages);
list1 = (ListView) findViewById(R.id.listview1);
list2 = (ListView) findViewById(R.id.listview2);
list2 = (ListView) findViewById(R.id.listview3);
listAdapter = new Adapter_List_Main(this, packages);//This is my own adapter, you probably use your own custom one as well.
list1.setAdapter(listAdapter);
//Setup list to support context menu
registerForContextMenu(list1);
//Setup list to support long click events.
list1.setLongClickable(true);
//Action Listener for long click on item in the list
list1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id)
{
//do things here
}
});
//Action Listener for short click on item in the list
list1.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
//do things here
}
});
//And so one . . .
}
}
I would suggest you to have a better look at the documentation of ListView or if you want to work a little bit more to RecyclerView. The latter one will allow you definitely more flexibility and maintainability.
Anyway, a base solution, that flattens a little bit your UI hierarchy is to use only one ListView/RecyclerView. Then in your adapter you can decide the type of view that your container is going to present.
In your design I'd identify 2 types:
TYPE_HEADER
TYPE_CHECKBOX
Once you have this things in place in your getView(...) you can decide based on the position and type what view instantiate(have a look at how to reuse them efficiently) and bind.
If you can try to avoid complexities those will become impossible to maintain :)

Multiple TextViews in the same List Item

I'm using a ListView with an array adapter to display multiple list items. I'd like to have multiple TextView in the list item as per the picture
I'm currently using a linear layout which doesn't seem to be suitable... Would a relative layout help me achieve this?
Example code or tutorial link would be cool
Cheers
ListView xml
<?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" >
<ListView
android:id="#+id/listViewTest"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
ArrayAdapter xml
<?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="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="89dp"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:layout_weight="0.34"
android:src="#drawable/ic_launcher" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_weight="0.97"
android:gravity="center"
android:text="#+id/label"
android:textSize="30dp" >
</TextView>
</LinearLayout>
I'm trying to add another textView under the existing textView using graphical layout tab in eclipse but my noobieness is getting in the way
This link uses Multiple Texts in a Single Listview...
http://publicstaticdroidmain.com/2011/12/building-a-custom-multi-line-listview-in-android/
Use the Code as follow..
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="Custom ListView Example" />
<ListView
android:id="#+id/srListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
and,
<?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"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingTop="10dip" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#C20000"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="#+id/cityState"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Follow the Link. It will Help you..
android Multiple selection ListView & Textview
And
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Main.xml for List action
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="785dp"
android:choiceMode="multipleChoice" >
</ListView>
</LinearLayout>
And Row.xml as,..
<?xml version="1.0" encoding="utf-8"?>
<TableRow>
<TextView
android:id="#+id/StudNo"
android:layout_width="50px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5px" />
</TableRow>
<TableRow>
<TextView
android:id="#+id/StudNo"
android:layout_width="50px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5px" />
</TableRow>
</TableLayout>
and try with Customized Codes..
The linked picture has only two TextViews one below another. In case your using only 2 TextViews then a vertical LinearLayout should solve the purpose. However, if you intent to use more than two Views inside your ListItem then I would recommend you to use Relative Layout as it is way more flexible in holding multiple child-views, when it comes to aligning them with respect to one another.
You can follow a very simple tutorial provided by Google here:
Relative Layout
Make xml layout for list item which contains two textview
**listItem.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
///*now in set this layout in your listview using Adapter as following*/
mlistview.setAdapter(new ArrayAdapter<String>(this,
R.layout.spinneritem, StringArray) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
if (v == null) {
if (DiscountActivity.this != null) {
LayoutInflater vi = (LayoutInflater) DiscountActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listItem, null);
}
}
TextView txtview1 = (TextView) v.findViewById(R.id.textview1);
TextView txtview2 = (TextView) v.findViewById(R.id.textview2);
txtview1.setText("textview1");
txtview2.setText("textview2");
v.setTag(campaign);
return v;
}
});

Width of clickable area in ListView w/ onListItemClick

I'm trying to get my list items in a ListView clickable. At the moment they are clickable (see my screenshot) but they're only clickable within the rectangle the text takes up.
I'm using protected void onListItemClick(ListView l, View v, int position, long id) {
for the clickable list items.
Here is my list.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_favorites"/>
and my row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="#+id/artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/songtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentRight="true"/>
<TextView android:id="#+id/album"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentRight="true"/>
<TextView android:id="#+id/playtime"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_alignParentRight="true"/>
<TextView android:id="#+id/playlistnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:visibility="gone" />
</LinearLayout>
And here's the screenshot example:
(New users aren't allowed to post images... grumble... have a hyperlink!)
In list.xml, the Listview has android:layout_width="fill_parent" so it should be the full width of the screen. (All of the items in row.xml are also width="fill_parent".) What am I missing?
The TextView in your list.xml needs to be set to fill_parent. The selection will then fit the width of the parent ListView.
android:layout_width="wrap_content"

Checkbox state problem in ListView

Hello with regard to the known issue of unwanted checkboxes being checked while scrolling through a listview, can anyone please give me an idea how to use a checkedTextView properly so that i can maintain check state properly when scrolling through the list. I thought the listview takes care of maintaining the checkbox state when using a checkedtextView but that does not seem to be the case. or do i have to use the default Id for the checkedTextView. I can't seem to find an example that uses a cursorAdapter or a SimpleCursorAdapter for this case. Thank you
i have tried using this, but am not really sure what to do with the set tag position. here is a small piece of code:
//bindView() method in SimpleCursorAdapter
//viewHolder holder;
//Cursor c;
holder.checkedText = (CheckedTextView)view.findViewById(R.id.view_checked);
holder.checkedText.setTag(c.getPosition());
holder.checkedText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
v.getTag();
((CheckedTextView) v).toggle();
}
});
my xml standard layout:
<?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">
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="multipleChoice"/>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_notes"/>
</LinearLayout>
my custom layout:
<?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="horizontal"
android:padding="6dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/viewNameText"
android:id="#+id/viewNameId">
</TextView>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:padding="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start Date"
android:layout_gravity="right"
android:id="#+id/DateId">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start Time"
android:layout_gravity="right"
android:id="#+id/TimeId">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
>
<CheckedTextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="false"
android:focusable="false"
android:clickable="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
/>
</LinearLayout>
</LinearLayout>
I thought the listview takes care of maintaining the checkbox state when using a checkedtextView but that does not seem to be the case.
It should, if your ListView is android:choiceMode="multiple".
do i have to use the default Id for the checkedTextView
You might. The only times I have implemented a multiple choice ListView, I have used the standard row layout rather than a custom one.

How to show a button at the end of an Android ListView

I want to show a button at the end of an Android list view. How can I achieve this?
I don't want to stick it to the activity bottom using alignparentbottom="true". Using layout_below does not work for me either.
My current XML:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/main_bg">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
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"
android:drawSelectorOnTop="false"
android:cacheColorHint="#ff6a00"
android:divider="#ff8f40"
android:dividerHeight="1px" />
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:background="#676767"
android:orientation="vertical">
<Button
android:layout_width="100px"
android:layout_height="wrap_content"
android:id="#+id/btnGetMoreResults"
android:layout_marginLeft="10px"
android:text="Get more" />
</RelativeLayout>
</RelativeLayout>
You may want to use ListView#addFooterView() to add a View at the bottom of the ListView.
I do it like this fixed button at the buttom of the screen
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/btn_New" >
</ListView>
<Button
android:id="#+id/btn_New"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="20dp"
android:text="#string/New"
android:width="170dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
if ur using linearLayout then assign android:layout_weight="1" to the listview and dont assign weight for button it works
You could do something like this:
final Button btnAddMore = new Button(this);
btnAddMore.setText(R.string.art_btn_moreIssues);
exArticlesList = (ExpandableListView) this.findViewById(R.id.art_list_exlist);
exArticlesList.addFooterView(btnAddMore);
1 If you want to add Button as the last element of the list view
You must create custom ListAdapter for your ListView which will create a view with a Button in the getView method. You should decide how to return your custom view for the last element, you can hardcode it (return element count +1 in getCount method and return custom view in getView when position > element count) or you can add element to the structure you will be taking data from (Array, Cursor etc.) and check if field of element have certain value
2 If you want to add element below list view
You should use android:layout_width attribute and make ListView and "empty" TextView (you should use it to show users that list is empty and View rendering is completed) layout_weight greater than buttons layout_weight
Check how it's done in Transdroids search Activity http://code.google.com/p/transdroid/source/browse/trunk/res/layout/search.xml
Use Footer view to list-view it work.
list_layout.xml :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:cacheColorHint="#ff6a00"
android:divider="#ff8f40"
android:dividerHeight="1px" />
</LinearLayout>
footerview.xml :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="100px"
android:layout_height="wrap_content"
android:id="#+id/btnGetMoreResults"
android:layout_marginLeft="10px"
android:text="Get more" />
</FrameLayout>
and in Activity.java
list=(ListView)findViewById(R.id.list);
FrameLayout footerLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.footerview,null);
btnPostYourEnquiry = (Button) footerLayout.findViewById(R.id.btnGetMoreResults);
list.addFooterView(footerLayout);
Simply Provide "layout_weight = 1" of ListView.
Example :
<?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">
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:cacheColorHint="#ff6a00"
android:divider="#ff8f40"
android:layout_weight="1"
android:dividerHeight="1px" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#ff8f40"
android:padding="20dp"
android:text="Click"
android:textColor="#FFFFFF"
android:textSize="22sp"
/>
</LinearLayout>
I came here looking for an answer first but found it somewhere else...
It's really easy, you just need to put weight 1 to the list inside a linear layout, the other textviews/buttons/etc don't need to have any weight value.
Here is an example: https://bitbucket.org/generalplus/android_development/src/5a892efb0551/samples/ApiDemos/res/layout/linear_layout_9.xml
You could, of course, use a custom adapter and specify a footer item. But you could probably also get away with putting it at the bottom of a ScrollView and have the ListView stretch vertically to the content:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/main_bg">
<ScrollView
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout
android:orientation="vertical"
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"
android:drawSelectorOnTop="false"
android:cacheColorHint="#ff6a00"
android:divider="#ff8f40"
android:dividerHeight="1px"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50sp"
android:background="#676767"
android:orientation="vertical">
<Button android:layout_width="100px"
android:layout_height="wrap_content"
android:id="#+id/btnGetMoreResults"
android:layout_marginLeft="10px"
android:text="Get more" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Well... details of implementations are not so easy if you want to create a smooth "add more" button at the end of the list. So here is some code :
myArrayAdapter = new ArrayAdapter<Show>(this, android.R.layout.simple_list_item_1, myList) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if( position >= super.getCount() )
return buttonMore ;
MyNormalView view = null;
if (convertView == null || convertView instanceof Button )
view = new MyNormalView(getContext());
else
view = (MyNormalView) convertView;
//customize view here with data
return view;
}
#Override
public int getCount() {
return super.getCount()+1;
}//met
};

Categories

Resources