0

I have implemented a ListView in an Activity. I am having issue with layout. I have a TextView in the Activity and then, below to it, I need to display the ListView items. Both are filling correctly, but the first ListView item overlaps the TextView layout, and hence the TextView text and first ListView item text appear in the same line. I am putting the layout text below. Please let me know what layout property I am missing and should use to correct the issue.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.rrd.school.project.AllClasses" >

    <TextView 
        android:id="@+id/allclassess_institutename"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:visibility="visible"
        android:textSize="10pt"
        android:textColor="#0404B4"
        android:background="#DF3A01"
        />
    <ListView 
        android:id="@+id/classlistview"
        android:layout_below="@+id/allclassess_institutename"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />
</RelativeLayout>

listviewitem.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.rrd.school.project.ClassListItme" >

    <TextView
        android:id="@+id/classid"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:visibility="gone"
        />
    <TextView 
        android:id="@+id/classname"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_centerHorizontal="true"
        android:visibility="visible"
        android:textSize="8pt"
        android:textColor="#0404B4"
        />

</RelativeLayout>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Pawan Rawat
  • 495
  • 1
  • 7
  • 25

2 Answers2

0

check this link

Difference between "@id/" and "@+id/" in Android

and

replace this line in ur listView

android:layout_below="@id/allclassess_institutename"

by this

android:layout_below="@+id/allclassess_institutename"

Edit:

align ur TextView to parentTop and try ..

<TextView
  android:id="@+id/allclassess_institutename"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#DF3A01"
  android:layout_alignParentTop="true"
  android:gravity="center"
  android:textColor="#0404B4"
  android:textSize="10pt"
  android:visibility="visible" />

<ListView
  android:id="@+id/classlistview"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_below="@+id/allclassess_institutename"
  android:fillViewport="true" />
Community
  • 1
  • 1
M S Gadag
  • 2,057
  • 1
  • 12
  • 15
  • Hello M S, it didn't work and i think it shoudn't as well. @+id is used to register a new id under resource and @id is to use a predeclared one. i have already declared id for ""@+id/allclassess_institutename"" in above TV so why to declare again ? – Pawan Rawat Feb 03 '15 at 14:43
  • @M S Gadag , It didnt' work too.... i also read the docs and i cannot use "@android:id/allClasses_institute" . it didn't appear in the list as well. you have used "android:layout_below="@+id/allclassess_institutename"" in your code but i think its not correct as its to register a new id and not to refer it. i used excluding the ""+"" here – Pawan Rawat Feb 04 '15 at 10:18
0

RelativeLayout arranges the views based on "relations" (got it, relative, relations) between the views. It's a kinda of complex, but very powerful ViewGroup. But if all you need is to have one under the other, you can easily replace with LinearLayout, like following:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.rrd.school.project.AllClasses" >

<TextView 
    android:id="@+id/allclassess_institutename"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="10pt"
    android:textColor="#0404B4"
    android:background="#DF3A01" />
<ListView 
    android:id="@+id/classlistview"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />
</LinearLayout>
Budius
  • 39,391
  • 16
  • 102
  • 144
  • tried replacing the relatvieLayout with Linearlayout but that didn't work. Also layout_below are the attributes which can be used in RelativeLayout only i guess! – Pawan Rawat Feb 03 '15 at 15:27
  • `layout_below` was my bad, I should have removed. I edited the XML on my answer to remove it. That should work, LinearLayout, orientation vertical. It will put every view one under the other. – Budius Feb 03 '15 at 15:31
  • logically it should solve the problem but not sure why list view is still overlaping the textview... :( ... didn't work as well . in the list i am putting id and name both but hiding the id view using visibility:gone ... can it cause this issue ? – Pawan Rawat Feb 03 '15 at 15:41
  • Also if i use android:layout_above="@+id/classlistview" in the textview and then use android:id="@id/classlistview" in the list view , the application shuts down abruptly where as per the documentation it should work properly... – Pawan Rawat Feb 03 '15 at 15:47