mu_sa
mu_sa

Reputation: 2725

ListView hides the EditText

I am new to Android and am reading Wrox's professional android 4 app dev book. In chapter 4 of the book it explains how to modify the existing text view. The problem i am facing is that the listview in my app hides the edit text box. Its hidden (can be seen in the background) but still works that is more stuff can be added to the list through it. Below is the code for my main activity xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/addItemContentDescription"
    android:hint="@string/addItemHint"
    />
<ListView
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</RelativeLayout>

and my todolist_item xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.wroxexample.ToDoListItemView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" 
android:padding="10dp"
android:scrollbars="vertical"
android:textColor="@color/notepad_text"
android:fadingEdge="vertical"
/>

Upvotes: 1

Views: 966

Answers (4)

Sam
Sam

Reputation: 3485

Timothee got there before me but ill just add a little more. you can, as he says, use a linear layout, or as user1387035 says, you can set the listview to be below the editText.

Relative Layout means "i want to lay things out relatively" and if you dont tell things where to go they will just float to where the 'gravity' is pulling them. The default gravity is top - so I'm guessing your items both ended up bunched at the top left?

As a rule of thumb - do you want your items to come one after another, bunched together (either horizontally or vertically)? if yes then use linear layout. If you want them to be pushed in different directions, use a relative layout. There are some exceptions, normally involving the "weight" attribute you can set in a linearlayout. (here's one I've just had to use: http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/)

If you have a Relative layout and are just using the layout_below/above attributes, without any 'alignParentBottom' or other thing set, then you probably just want a linearlayout

In your case I would say it sounds like you want Timothee's solution. If you want a little separation between the objects, you can use padding/margins to space them a little.

As for gravities, here is a useful blog entry that helped me get my head around LinearLayout's gravities (as well as generally): http://sandipchitale.blogspot.co.uk/2010/05/linearlayout-gravity-and-layoutgravity.html

Upvotes: 2

Timoth&#233;e Jeannin
Timoth&#233;e Jeannin

Reputation: 9912

The first option you have is to use a LinearLayout instead of a RelativeLayout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/addItemContentDescription"
    android:hint="@string/addItemHint"
    />
<ListView
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

A RelativeLayout will allow you to position the elements relatively to the others.

On the other hand a LinearLayout will position the elements one below the other in the order they appear in the xml file.

The second option you have is to keep your RelativeLayout and just add the following tag to your ListView:

android:layout_below="@id/myEditText"

This will position the ListView below the EditText.

Upvotes: 2

Filipe Batista
Filipe Batista

Reputation: 1854

Use a LinearLayout and the property android:layout_weight http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html

Try something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >
   <ListView
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:entries="@array/testea"
    android:layout_height="wrap_content"/>
<EditText
    android:id="@+id/myEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/addItemContentDescription"
    android:hint="@string/addItemHint"
    />

</LinearLayout>

This way ListView will grow to fill only the unused space.

Upvotes: 2

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

Try this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/addItemContentDescription"
    android:hint="@string/addItemHint"
    android:layout_alignParentTop="true"
    />
<ListView
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/myEditText"/>
</RelativeLayout>

Upvotes: 2

Related Questions