1. Are you ready for the Galaxy S20? Here is everything we know so far!

Displaying a Button and EditBox after a ListView

Discussion in 'Android Development' started by Orby, May 19, 2011.

  1. Orby

    Orby Lurker
    Thread Starter

    Hi, i'm new to Android and having a lot of trouble displaying a Button and a EditBox after a Listview. I've attached a sample paint image of what i'm trying to achieve and included my code below.

    Any help would be appreciated.

    Thanks :)

    My XML File:

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout
    3.     xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:orientation="vertical"
    5.     android:layout_width="fill_parent"
    6.     android:layout_height="fill_parent">
    7.         <LinearLayout
    8.             android:orientation="horizontal"
    9.             android:layout_width="fill_parent"
    10.             android:layout_height="fill_parent"
    11.             android:layout_weight="1">
    12.         <TextView
    13.             android:id="@+id/selection"
    14.             android:layout_width="wrap_content"
    15.             android:layout_height="wrap_content"
    16.             android:textAppearance="?android:attr/textAppearanceLarge"/>
    17.         <ImageView
    18.             android:id="@+id/icon"
    19.             android:layout_width="wrap_content"
    20.             android:layout_height="wrap_content"
    21.             android:src="@drawable/icon"/> 
    22.         </LinearLayout>
    23.         <LinearLayout
    24.             android:orientation="vertical"
    25.             android:layout_width="fill_parent"
    26.             android:layout_height="fill_parent">
    27.             <EditText android:id="@+id/edittext"
    28.                 android:layout_width="fill_parent"
    29.                 android:layout_height="wrap_content"
    30.                 android:text="add text"/>      
    31.             <Button
    32.                 android:id="@+id/btn_search"
    33.                 android:layout_width="fill_parent"
    34.                 android:layout_height="wrap_content"
    35.                 android:text="Search"/>
    36.             </LinearLayout>  
    37.  </LinearLayout>
    My Code so far:

    Code (Text):
    1. public class recipes extends ListActivity {
    2.  
    3.     public void onCreate(Bundle savedInstanceState) {
    4.         super.onCreate(savedInstanceState);
    5.         main_array = getResources().getStringArray(R.array.main_array);
    6.         setListAdapter(new MyCustomAdapter(recipes.this, R.layout.row, main_array));
    7.  
    8.         }
    9.    
    10.     public void onListItemClick(ListView parent, View v, int position, long id){
    11.         Intent intent = new Intent(v.getContext(), Activity2.class);
    12.         intent.putExtra("selected_item", position);
    13.         intent.putExtra("selected_name", main_array[position]);
    14.         startActivityForResult(intent, 0);     
    15.     }
    16.  
    17.        
    18.     public class MyCustomAdapter extends ArrayAdapter<String> {
    19.  
    20.        
    21.         public MyCustomAdapter(Context context, int textViewResourceId, String[] objects) {
    22.             super(context, textViewResourceId, objects);
    23.         }
    24.  
    25.         @Override
    26.         public View getView(int position, View convertView, ViewGroup parent) {
    27.             LayoutInflater inflater=getLayoutInflater();
    28.             View row = inflater.inflate(R.layout.row, parent, false);
    29.             TextView selection = (TextView)row.findViewById(R.id.selection);
    30.             selection.setText(recipes.main_array[position]);
    31.             ImageView image =(ImageView)row.findViewById(R.id.icon);           
    32.             return row;
    33.        
    34.         }
    35.     }
    36.     }
     

    Attached Files:




    1. Download the Forums for Android™ app!


      Download

       
  2. alostpacket

    alostpacket Over Macho Grande?

  3. Orby

    Orby Lurker
    Thread Starter

    Hi alostpacket, thanks for your response! I tried this and its still overlapping the EditBox and Search Button on List Items. Can you please explain what i need to include in my code to make the Button and EditBox appear after the ListView?

    Thanks

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout
    3.     xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:orientation="vertical"
    5.     android:layout_width="fill_parent"
    6.     android:layout_height="fill_parent">
    7.     <LinearLayout
    8.         android:orientation="horizontal"
    9.         android:layout_width="fill_parent"
    10.         android:layout_height="fill_parent">
    11.         <ImageView
    12.             android:id="@+id/icon"
    13.             android:layout_width="wrap_content"
    14.             android:layout_height="wrap_content"
    15.             android:src="@drawable/icon"/> 
    16.         <TextView
    17.             android:id="@+id/selection"
    18.             android:layout_width="wrap_content"
    19.             android:layout_height="wrap_content"
    20.             android:textAppearance="?android:attr/textAppearanceLarge"/>
    21.     </LinearLayout>
    22.     <LinearLayout
    23.             android:orientation="vertical"
    24.             android:layout_width="fill_parent"
    25.             android:layout_height="fill_parent">
    26.         <EditText android:id="@+id/edittext"
    27.             android:layout_width="fill_parent"
    28.             android:layout_height="wrap_content"
    29.             android:text="add text"/>      
    30.         <Button
    31.             android:id="@+id/btn_search"
    32.             android:layout_width="fill_parent"
    33.             android:layout_height="wrap_content"
    34.             android:text="Search"/>
    35.     </LinearLayout>
    36. </RelativeLayout>
     
  4. Boogs

    Boogs Member

    I think it's recommended that you provide two XML files, one for the layout of the list view and your edit text and button, and another which describes the row of the edit text.

    For example:

    XML #1 (abbreviated):

    Code (Text):
    1.  
    2. <LinearLayout
    3.    android:layout_width="fill_parent"
    4.    android:layout_height="fill_parent"
    5.    android:orientation="vertical" >
    6.    <ListView
    7.       android:id="@+id/list"
    8.       android:layout_width="fill_parent"
    9.       android:layout_height="wrap_content"
    10.       android:layout_weight="1" />
    11.    <TextView
    12.       android:id="@+id/empty"
    13.       android:layout_width="fill_parent"
    14.       android:layout_height="wrap_content"
    15.       android:layout_weight="1" />
    16.    <EditText
    17.       android:id="@+id/your_edit_text"
    18.       android:layout_width="fill_parent"
    19.       android:layout_height="wrap_content" />
    20.    <Button
    21.       android:id="@+id/your_button"
    22.       android:layout_width="fill_parent"
    23.       android:layout_height="wrap_content" />
    24. </LinearLayout>
    25.  
    This does 2 things: first, it allows the ListActivity to manage your list view (and if it's empty, then it will display whatever text is given in your @id/empty TextView), and secondly, it abstracts the row from the list view. So, you can either A) reuse your XML for another activity, or B) make it easier to read.

    Your second XML file which describes the row of each list view item would be something like:

    XML #2 (abbreviated):

    Code (Text):
    1.  
    2. <LinearLayout
    3.    android:layout_width="fill_parent"
    4.    android:layout_height="fill_parent"
    5.    android:orientation="horizontal" >
    6.    <TextView
    7.       android:id="@+id/text"
    8.       android:layout_width="wrap_content"
    9.       android:layout_height="wrap_content"
    10.       android:layout_weight="1" />
    11.    <ImageView
    12.       android:id="@+id/image"
    13.       android:layout_width="wrap_content"
    14.       android:layout_height="wrap_content" />
    15. </LinearLayout>
    16.  
    Then, in your code, you can do way with the getView() code in your adapter. I recommend using a SimpleAdapter to get the job done:

    Code (Text):
    1.  
    2. List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
    3.  
    4. String[] yourStringArray = getResources().getStringArray(R.array.your_array);
    5.  
    6. for (int i = 0; i < yourStringArray.length; ++i) {
    7.    HashMap<String, Object> map = new HashMap<String, Object>();
    8.    map.put("text", yourStringArray[i]);
    9.    map.put("image", some_link_to_your_image);
    10.    data.put(map);
    11. }
    12.  
    13. new SimpleAdapter(
    14.    this,
    15.    data,
    16.    R.layout.row,
    17.    new String[] {"text", "image"},
    18.    new int[] {R.id.text, R.id.image});
    19.  
     
Loading...

Share This Page

Loading...