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 :)
Related
I need to arrange two ListViews in a vertical LinearLayout in such a way that 2nd ListView will be at the end of last item of first ListView.
If ListView1 is empty, the view should be something like this.
As ListView1.size() increases ListView2 should be pulled down to a limit.
ListView1 reaches a fixed height (or weight) , it should stop expanding and start scrolling.
I think there is no method in android to set maxHeight for View.
I have already tried almost all combinations of layout_height="wrap_content", fixed heights and weights for both ListViews. In all cases,
either 1st requirement(image1) will fail : ListView1 will not be at top
or last requirement(image3) will fail : ListView2 will get pushed out of the screen.
Any help or suggestions will be appreciated. Thanks in advance.
Edit:
my current code is:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<ListView
android:id="#+id/myOrderListView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.6" >
</ListView>
<ListView
android:id="#+id/myCompletedOrderListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="0.4" >
</ListView>
</LinearLayout>
I have tried with different weight combinations, like:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<ListView
android:id="#+id/myOrderListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.6" >
</ListView>
<ListView
android:id="#+id/myCompletedOrderListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="0.4" >
</ListView>
</LinearLayout>
But it will fail last requirement.
Edit2: #dangVarmit
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<ListView
android:id="#+id/myOrderListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<ListView
android:id="#+id/myCompletedOrderListView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="20dp" >
</ListView>
<Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
If you mean this, It still fails req3.
You can find the below code for right and left listview you can change it to above and bottom listview or as you want to show
main_activity.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">
<LinearLayout
android:id="#+id/main_top_bit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/orange">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button
android:id="#+id/button_1"
style="#style/landscape_top_button"
android:text="button1" />
<Button
android:id="#+id/button_2"
style="#style/landscape_top_button"
android:text="button2"/>
</LinearLayout>
</LinearLayout>
<View android:id="#+id/main_horizontal_bar"
android:layout_height="3dip" android:layout_width="fill_parent"
android:background="#color/gray" android:layout_marginTop="1dip" />
</LinearLayout>
<LinearLayout
android:id="#+id/lower_linear_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/main_top_bit">
<RelativeLayout
android:layout_below="#+id/lower_linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dip"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/teal"
android:id="#+id/org_left_linearlayout2">
<Button
android:id="#+id/button_3"
style="#style/landscape_top_button"
android:text="button3"/>
<Button
android:id="#+id/button_4"
style="#style/landscape_top_button"
android:text="button4" />
</LinearLayout>
<ListView
android:background="#color/green"
android:id="#+id/lstLeft"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/org_left_linearlayout2"
android:layout_above="#+id/org_left_bottom_rl"
android:cacheColorHint="#00000000"/>
<RelativeLayout
android:id="#+id/org_left_bottom_rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/purple" >
<Button
android:text="button5"
android:id="#+id/button_5"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</RelativeLayout>
</RelativeLayout>
<View
android:layout_width="3dip"
android:layout_height="fill_parent"
android:background="#color/gray">
</View>
<RelativeLayout
android:layout_below="#+id/lower_linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/olive"
android:id="#+id/org_right_linearlayout2">
<Button
android:id="#+id/button_6"
style="#style/landscape_top_button"
android:text="button6" />
<Button
android:id="#+id/button_7"
style="#style/landscape_top_button"
android:text="button7" />
</LinearLayout>
<ListView
android:id="#+id/lstRight"
android:background="#color/blue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/org_right_linearlayout2"
android:layout_above="#+id/org_right_bottom_rl"
android:cacheColorHint="#00000000"/>
<RelativeLayout
android:id="#+id/org_right_bottom_rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/red"
android:layout_alignParentBottom="true">
<Button
android:text="button8"
android:id="#+id/button_8"
android:layout_alignParentRight="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private final String TAG = this.getClass().getName();
ListView leftList;
ListView rightList;
static final String[] FRUITS = new String[] {"Apple", "Bananna", "Cherry", "Pear", "Strawberry"};
static final String[] VEGETABLES = new String[] {"Asparagus", "Bamboo shoots", "Celery", "Ginger", "Spinach"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
leftList = (ListView) findViewById(R.id.lstLeft);
rightList = (ListView) findViewById(R.id.lstRight);
populateLeftList();
populateRightList();
}
public void populateLeftList(){
leftList.setAdapter(new ArrayAdapter<string>(this, android.R.layout.simple_list_item_1, FRUITS));
}
public void populateRightList(){
rightList.setAdapter(new ArrayAdapter<string>(this, android.R.layout.simple_list_item_1, VEGETABLES));
}
}
for list1, layout_height=wrap_content, remove layout_weight
for list2, layout_height=100dp (any fixed value), remove layout_weight
add <Space layout_weight=1 layout_height=0dp/> at the end of the list.
that should give you the results you are looking for.
I have found one solution by myself. It works perfectly as i wanted. I dont know whether it is a good approach or not. I am searching for an xml solution.
getDeviceHeight() is my method to find device height in pixels.
200 is the approximate height of other ui elements above My ListViews.
myorderList is my first ListView
if(myorderList.getHeight() > ((getDeviceHeight()/2) - 200)) {
LayoutParams params = myorderList.getLayoutParams();
params.height = (int)((getDeviceHeight()/2) - 200);
myorderList.setLayoutParams(params);
}
This should be done when there is any kind of changes happening to the first ListView.
Edit:
This will also fail when items removed after reaching maximum height :-(
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...
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"
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
};
Currently I have a class that is extending the ListActivity class. I need to be able to add a few static buttons above the list that are always visible. I've attempted to grab the ListView using getListView() from within the class. Then I used addHeaderView(View) to add a small layout to the top of the screen.
Header.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" >
<Button
android:id="#+id/testButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Income"
android:textSize="15dip"
android:layout_weight="1" />
</LinearLayout>
Before I set the adapter I do:
ListView lv = getListView();
lv.addHeaderView(findViewById(R.layout.header));
This results in nothing happening to the ListView except for it being populated from my database. No buttons appear above it.
Another approach I tried as adding padding to the top of the ListView. When I did this it successfully moved down, however, if I added any above it, it pushed the ListView over. No matter what I do it seems as though I cannot put a few buttons above the ListView when I used the ListActivity.
synic, I tried your suggestion previously. I tried it again just for the sake of sanity, and the button did not display. Below is the layout file for the activity and the code I've implemented in the oncreate().
//My listactivity I am trying to add the header to
public class AuditActivity extends ListActivity {
Budget budget;
#Override
public void onCreate(Bundle savedInstanceState) {
Cursor test;
super.onCreate(savedInstanceState);
setContentView(R.layout.audit);
ListView lv = getListView();
LayoutInflater infalter = getLayoutInflater();
ViewGroup header = (ViewGroup) infalter.inflate(R.layout.header, lv, false);
lv.addHeaderView(header);
budget = new Budget(this);
/*
try {
test = budget.getTransactions();
showEvents(test);
} finally {
}
*/
// switchTabSpecial();
}
Layout.xml for activity:
<?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="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="#string/empty" />
</LinearLayout>
findViewById() only works to find subviews of the object View. It will not work on a layout id.
You'll have to use layout inflater to convert the xml to it's corresponding View components. Something like this:
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View header = inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);
I'm not sure why your code wasn't just throwing an error. findViewById() was probably just returning null, and so no header was added to your list.
Here is the simpliest sollution:
<?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="#color/background">
<include layout="#layout/actionbar"/>
<ListView
android:id="#+id/tasklist_TaskListView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textColor="#color/baseFont"/>
<include layout="#layout/bottombar"/>
</LinearLayout>
or
<?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="#color/background">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/tasklist_TaskListView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textColor="#color/baseFont"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
instead of button you can add another horizontal linear layout
After some research I was able to figure out that mixing TableLayout and LinearLayout within my ListActivity XML document I was able to add a header to the document. Below is my XML document if anyone is interested to see it. While synic's approach is probably the right approach after work with his solution for sometime I was unable to make it function the way I wanted it.
AuditTab.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audittab);
getListView().setEmptyView(findViewById(R.id.empty));
}
audittab.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="#+id/btnFromDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="1" />
<Button
android:id="#+id/btnToDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_toRightOf="#+id/btnFromDate"
android:layout_weight="1" />
<Button
android:id="#+id/btnQuery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Query"
android:layout_toRightOf="#+id/btnToDate"
android:layout_weight="1" />
</LinearLayout>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#android:id/list"
android:layout_width="300dip"
android:layout_height="330dip"
android:scrollbars="none" />
<TextView
android:id="#+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:text="- Please select a date range and press query." />
</LinearLayout>
</TableRow>
</TableLayout>
AuditItem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:padding="10sp">
<TextView
android:id="#+id/transactionDateLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date: " />
<TextView
android:id="#+id/transactionDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/transactionDateLabel" />
<TextView
android:id="#+id/transactionTypeLabel"
android:layout_below="#id/transactionDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type: " />
<TextView
android:id="#+id/transactionType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_below="#id/transactionDate"
android:layout_toRightOf="#id/transactionTypeLabel" />
<TextView
android:id="#+id/transactionAmountLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:text="Amount: "
android:layout_below="#id/transactionDate"
android:layout_toRightOf="#id/transactionType" />
<TextView
android:id="#+id/transactionAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/transactionDate"
android:layout_toRightOf="#id/transactionAmountLabel" />
<TextView
android:id="#+id/transactionCategoryLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category: "
android:layout_below="#id/transactionAmountLabel" />
<TextView
android:id="#+id/transactionCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/transactionAmountLabel"
android:layout_toRightOf="#id/transactionCategoryLabel"
/>
<TextView
android:id="#+id/transactionToAccountLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To Account: "
android:layout_below="#id/transactionCategoryLabel" />
<TextView
android:id="#+id/transactionToAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/transactionCategoryLabel"
android:layout_toRightOf="#id/transactionToAccountLabel" />
<TextView
android:id="#+id/transactionFromAccountLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From Account: "
android:layout_below="#id/transactionToAccountLabel" />
<TextView
android:id="#+id/transactionFromAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/transactionToAccountLabel"
android:layout_toRightOf="#id/transactionFromAccountLabel" />
<TextView
android:id="#+id/transactionNoteLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Note: "
android:layout_below="#id/transactionFromAccountLabel" />
<TextView
android:id="#+id/transactionNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/transactionFromAccountLabel"
android:layout_toRightOf="#id/transactionNoteLabel" />
<Button
android:id="#+id/editTransactionBtn"
android:layout_width="wrap_content"
android:layout_height="40sp"
android:visibility="gone"
android:text="Edit"
android:layout_below="#id/transactionNoteLabel"/>
<Button
android:id="#+id/deleteTransactionBtn"
android:layout_width="wrap_content"
android:layout_height="40sp"
android:text="Delete"
android:layout_below="#+id/transactionNoteLabel"
android:visibility="gone"
android:layout_toRightOf="#+id/editTransactionBtn"
android:ellipsize="end"/>
</RelativeLayout>
The ListView answer above is useful but scrolls with the list and doesn't keep the header graphics at the top. The best solution I've found is to set a custom title to the activity. Here is what my constructor looks like:
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.your_listview_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.your_header);
...
Where your_listview_layout.xml configures a ListView, and your_header.xml contains whatever custom header layout that you like. Just note that the three lines above must be called in exactly that order to not cause run-time problems.
The tutorial that helped me was http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/ and you can find many related pages on Stack Overflow by searching for the term "setFeatureInt"
Adding a static header is easy, just make a separate relative view that has the alignParentTop (or bottom, right or left) attribute set to true.