Home > Software design >  Why does listview is not showing on my emulator?
Why does listview is not showing on my emulator?

Time:10-14

I'm making simple listview of therapies and the list is not showing on the emulator. I've upgraded and downgraded my api from 22 to 30 and change relative layout to linear layout but still the list is not showing.

activty_stress.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="9"
android:background="#83BCD4"
tools:context=".stress">

<TextView
    android:id="@ id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Find your relaxation"
    android:textColor="@color/white"
    android:textSize="18pt" />

<ListView
    android:id="@ id/listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="70dp"/>

 </RelativeLayout>

stress.java

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

    ListView listView = findViewById(R.id.listview);

    List<String> list = new ArrayList<>();
    list.add("Therapy1");
    list.add("Therapy2");
    list.add("Therapy3");
    list.add("Therapy4");
    list.add("Therapy5");

    ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,list);
    listView.setAdapter(arrayAdapter);
    
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position==0){
                //clicked therapy1
                startActivity(new Intent(stress.this,Therapy1.class));
            } else if (position==1){
                //clicked therapy2
            }else{

            }
        }
    });
}

CodePudding user response:

Heyy try replacing your RelativeLayout with LinearLayout, or remove weightSum because weightSum cannot be used with RelativeLayout

CodePudding user response:

Replace with this code.

Note: Whenever you give textSize, give it in sp not in Pixel.

activty_stress.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#83BCD4"
    android:orientation="vertical">

    <TextView
        android:id="@ id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Find your relaxation"
        android:textColor="@color/white"
        android:textSize="30sp" />

    <ListView
        android:id="@ id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp" />

</RelativeLayout>

stress.java

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

        ListView listView = findViewById(R.id.listview);

        List<String> list = new ArrayList<>();
        list.add("Therapy1");
        list.add("Therapy2");
        list.add("Therapy3");
        list.add("Therapy4");
        list.add("Therapy5");

        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    //clicked therapy1
//                    startActivity(new Intent(stress.this,Therapy1.class));
                } else if (position == 1) {
                    //clicked therapy2
                } else {

                }
            }
        });

    }

Output:

enter image description here

  • Related