Search code examples
androidlistviewandroid-relativelayout

TextView with wrap_content height obscured by ListView below it


I am not seeing my "Hello World" textview. The listview, which I populate with elements, is all that shows.

<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">
    tools:context="lister.quantumproductions.com.lister.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_above="@+id/recipe_list_view"></TextView>

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

</RelativeLayout>



public class MainActivity extends AppCompatActivity {
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        loadListView();
    }

    private void loadListView() {
        mListView = findViewById(R.id.recipe_list_view);
        final ArrayList<Recipe> recipes = Recipe.getRecipesFromFile("recipes.json", this);

        RecipeAdapter a = new RecipeAdapter(this, recipes);
        mListView.setAdapter(a);
    }
}

Solution

  • Your ListView fills the entire screen, and then you say that the TextView should be above the ListView, which makes it appear outside the screen, so it's not visible.

    You need to invert the dependency.

    So instead of the TextView being above the ListView, the ListView should be below the TextView.