• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • Last Modified:

How to create a listview and button on the same screen

HI

Im just starting out with Android and i am looking to create a form (i think its referred to as an activity) which has a listview and a button at the bottom of the screen. Knocking this kind of thing out on microsoft VS would take 5 mins, however with Android i cant seem to do it!
I can create a string array and bind it to a simple_List_item in a short time, which is great if i just want to display the list however it covers the entire screen including my button at the bottom.
How can i bind my string array to the listview i dragged onto the screen without using the simple_list_view.
The example below works but the simple_list_item covers the whole screen:

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Phones));

Or maybe i can adjust the size?

Thanks in advance
0
wilko100
Asked:
wilko100
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Molnar IstvánHelpDesk / ProgrammerCommented:
you can use relativelayout instead of linearlayout:
http://developer.android.com/reference/android/widget/RelativeLayout.html

hope it helps
0
 
wilko100Author Commented:
Im using a relative layout already and the simple_list_item covers the whole screen, including my button and the list view i dragged on. Can i not use this simple_list_item and bind the array to my list view instead?
0
 
Molnar IstvánHelpDesk / ProgrammerCommented:
first add the button on the bottom of the relativelayout and then add the listview:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

    </ListView>

</RelativeLayout>

Open in new window

0
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
wilko100Author Commented:
This has already been set:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

<Button
            android:id="@+id/btnviewmessages"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="67dp"
            android:layout_marginLeft="14dp"
            android:text="View Messages" />

<Button
            android:id="@+id/btnrefresh"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/btnviewmessages"
            android:layout_alignBottom="@+id/btnviewmessages"
            android:layout_alignParentRight="true"
            android:layout_marginRight="26dp"
            android:text="Refresh List" />


        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/imageView1" >

        </ListView>

The problem i have is these controls get overlayed completly by the simple_list_item_1. All i want to do is bind my array to the list view (@+id/listview) or if possible, simply adjust the size of the simple_list_item_1and i will scrap my listview (@+id/listview) .
0
 
PCableGuyCommented:
How can i bind my string array to the listview i dragged onto the screen without using the simple_list_view?

In your Java code, you need to find your listview using its ID then use setAdapter.

Try this in your Java code:

ListView myList = (ListView) findViewById(R.id.listview);
myList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Phones);




There's an example here:
http://codehenge.net/blog/2011/05/customizing-android-listview-item-layout/
0
 
PCableGuyCommented:
wilko100,

I forgot a parenthesis, use this one instead.

myList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Phones));
0
 
SanthanaTechnical LeadCommented:
Hi wilko100,
http://www.vogella.com/articles/AndroidListView/article.html

Refer this link..Here you can see the varities of examples related to listview..
0
 
wilko100Author Commented:
Sorry for the delay in responding. Yes this worked perfectly, thank you:

ListView listview = (ListView) findViewById(R.id.listview);
            String[] values = new String[] {
                        "User1 12/9",
                        "User2 13/9 14/9",
                        "User3 12/9",
                        "User4 13/9",
                        "User5 12/9",
                        "User6 13/9 14/9"
            };
            
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                        android.R.id.text1, values);
            
            listview.setAdapter(adapter);
works a treat
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now