0

I was trying to find out if you could add a top bar to a ListView. Maybe 20-50 px in height with a couple of buttons(the buttons would also have to be click-able). Image to represent my idea:

enter image description here

The far left shot has a bar above the ListView, is there anyway I can achieve this?

user
  • 86,916
  • 18
  • 197
  • 190
TheBlueCat
  • 1,147
  • 5
  • 19
  • 38
  • yes, it looks like just a horizontal LinearLayout with some Buttons in it. Have you tried this already and had some trouble? – FoamyGuy May 04 '12 at 19:46
  • So you're saying there isn't a listview. Yes, I've tried this and it doesn't display, no error messages. – TheBlueCat May 04 '12 at 19:47
  • If you are referring to the left part of the image, that is the `ActionBar`(native or custom implemented). – user May 04 '12 at 19:50
  • The action bar Is not what I was referring to. I wanted to see if I could add a bar above a listview (in the image it's black). – TheBlueCat May 04 '12 at 19:55
  • Adding a bar to the top of a `ListView` is simple, you could do it with some layout modifications. – user May 04 '12 at 20:06
  • 1
    @TheBlueCat The Android terminology for "that black bar above the ListView" is an ActionBar. In Android 3.0+ it's a native widget, prior to that we typically build them out of a `LinearLayout`. You can very easily add one to your layout as Luksprog describes in his answer. Android layouts can have many views in the hierarchy at the same time; ListView is just one of these widgets. – devunwired May 04 '12 at 20:25

2 Answers2

3

I don't know your current layout so I assume that you have only a ListView element in a LinearLayout. Here is a LinearLayout with 3 Buttons at the top of the list:

<?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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button3" />
    </LinearLayout>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

I hope this is what you want.

user
  • 86,916
  • 18
  • 197
  • 190
  • Thank you :) Is there any documentation I can read. I'm interested in this issue and would appreciate if you could point me in the right direction. – TheBlueCat May 05 '12 at 12:23
  • I get an error when I try this code. " You must supply a resource Id for a textview". No, I don't have a textview in that code. – TheBlueCat May 05 '12 at 12:38
  • @TheBlueCat There is no documentation to recommend because the code above is a simple layout, you are limited by your goals(you could replace the `LinearLayout` full of `Buttons` with a `TableLayout` for example and add other views in a grid like layout). For example the black bar from your image is composed from 1 `TextView`(`layout_weight` 1) and 3 `ImageButtons` with drawable separators. There is a lot of documentation for the `ActionBar` if you want to read about it. – user May 05 '12 at 12:45
  • @TheBlueCat The layout above works for me so there is something in your code. Try cleaning the project and then running it again. – user May 05 '12 at 12:46
  • @TheBlueCat Are you supplying the right thing in the adapter's constructor? If you provide a layout file(containing other elements than a simple `TextView`) you should also pass the id of the `TextView` from the layout where you want your data to be put by the adapter. – user May 05 '12 at 12:53
  • `ListView lv = getListView(); /* Gets a ListView */ lv.setTextFilterEnabled(true); /* sorts the listview */ ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), com.gta5news.bananaphone.R.layout.chatservice, result.getheadlines()); setListAdapter(adapter);` Is this ok? – TheBlueCat May 05 '12 at 13:11
  • @TheBlueCat If `R.layout.chatservice` contains more then a simple `TextView` tag(for example even if you enclose the `TextView` with `the LinearLayout`) then the adapter declaration should be: `ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), com.gta5news.bananaphone.R.layout.chatservice, R.id.id_of_textview_from_the_previous_layout, result.getheadlines());` – user May 05 '12 at 13:16
  • What do you mean? Id from a previous layout? – TheBlueCat May 05 '12 at 13:19
  • @TheBlueCat In `R.layout.chatservice` you have a `TextView`. Pass the `id` of that `TextView` in the adapter constructor. – user May 05 '12 at 13:20
  • I'm doing this `ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), com.gta5news.bananaphone.R.layout.chatservice, com.gta5news.bananaphone.R.id.listView1, result.getheadlines()); setListAdapter(adapter);` yet, it still crashes with the same log. – TheBlueCat May 05 '12 at 13:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10917/discussion-between-luksprog-and-thebluecat) – user May 05 '12 at 13:26
-1

Use this:

listView.setDivider(getResources().getDrawable(R.drawable.list_view_divider));

list_view_divider should be a transparent png image with height you desire.

Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106