Java – How do I add a scrolling listView to a popup in android studio?

How do I add a scrolling listView to a popup in android studio?… here is a solution to the problem.

How do I add a scrolling listView to a popup in android studio?

I’m trying to make an android app that opens a popup when the button is clicked. Also, the pop-up window has four TextViews with headers and four listViews with options. However, I can’t add a listView to the popup. A popup opens when I click the button, but when I add the listView and its adapter and try to run it, the app crashes. I want the layout of the PopUp window to be the same as in the news.xml file.

activity_main.xml

<ScrollView 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:fillViewport="true"
    android:visibility="visible"
    tools:context=". MainActivity">

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/linear">

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

<Button
                android:id="@+id/news1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

android:layout_alignParentStart="true"
                android:layout_marginStart="15dp"
                android:text="@string/news" />

</RelativeLayout>

</LinearLayout>

</ScrollView>

MainActivity.java

package com.example.abina.popup;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import static android.view.Gravity.NO_GRAVITY;

public class MainActivity extends AppCompatActivity  {

private Button news1;
    private PopupWindow popupWindow;
    private LayoutInflater layoutInflater;
    private LinearLayout linearLayout;
    ListView lst;
    String [] local = {"asdf","Sgadf","adfhtr","trdbfa"};
    @RequiresApi(api = Build.VERSION_CODES. O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

for listView 1
        lst = (ListView) findViewById(R.id.listView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android. R.layout.simple_list_item_1,local);
        lst.setAdapter(adapter);
        lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this,local[i],Toast.LENGTH_SHORT).show();
            }
        });

linearLayout  = (LinearLayout) findViewById(R.id.linear);
        news1 = (Button) findViewById(R.id.news1);
        news1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                layoutInflater = (LayoutInflater) getApplication().getSystemService(LAYOUT_INFLATER_SERVICE);
                ViewGroup container  = (ViewGroup) layoutInflater.inflate(R.layout.news,null);

popupWindow = new PopupWindow(container,800,1300,true);
                popupWindow.showAsDropDown(news1);
                container.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        popupWindow.dismiss();
                        return false;
                    }
                });
            }
        });

}
}

News .xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ABEBC6">

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

<ListView
            android:id="@+id/listView1"
            android:layout_width="160dp"
            android:layout_height="184dp"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView"
            android:layout_marginStart="13dp" />

<TextView
            android:id="@+id/textView"
            android:layout_width="109dp"
            android:layout_height="33dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="30dp"
            android:layout_marginTop="20dp"
            android:text="@string/local" />

<TextView
            android:id="@+id/textView2"
            android:layout_width="108dp"
            android:layout_height="33dp"
            android:layout_alignParentEnd="true"
            android:layout_alignTop="@+id/textView"
            android:layout_marginEnd="30dp"
            android:text="@string/national" />

<ListView
            android:id="@+id/listView2"
            android:layout_width="160dp"
            android:layout_height="184dp"
            android:layout_alignParentEnd="true"
            android:layout_alignTop="@+id/listView1" />

</RelativeLayout>

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="260dp">

<TextView
            android:id="@+id/textView3"
            android:layout_width="109dp"
            android:layout_height="33dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="30dp"
            android:layout_marginTop="20dp"
            android:text="@string/sports1" />

<TextView
            android:id="@+id/textView4"
            android:layout_width="108dp"
            android:layout_height="33dp"
            android:layout_alignParentEnd="true"
            android:layout_alignTop="@+id/textView3"
            android:layout_marginEnd="43dp"
            android:text="@string/sports2" />

<ListView
            android:id="@+id/listView3"
            android:layout_width="160dp"
            android:layout_height="184dp"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView3"
            android:layout_marginStart="20dp" />

<ListView
            android:id="@+id/listView4"
            android:layout_width="162dp"
            android:layout_height="184dp"
            android:layout_alignParentEnd="true"
            android:layout_below="@+id/textView4" />
    </RelativeLayout>
</RelativeLayout>

strings.xml

<resources>
    <string name="app_name">Popup</string>
        <string name="news">Check Latest News!</string>
        <string name="local">Local News</string>
        <string name="national">National News</string>
        <string name="sports1">Sports One</string>
        <string name="sports2">Sports Two</string>

<string-array name="local">
            <item>Star Ledger</item>
            <item>Ny Times</item>
            <item>The Record</item>
            <item>Trentorian</item>

</string-array>

</resources>

Solution

The problem is that your list doesn’t exist in your activity_main layout. Instead, it exists in the pop-up window.

Follow the code below to change your onCreate() method and it should work.

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

for listView 1
    linearLayout  = (LinearLayout) findViewById(R.id.linear);
    news1 = (Button) findViewById(R.id.news1);
    news1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            layoutInflater = (LayoutInflater) getApplication().getSystemService(LAYOUT_INFLATER_SERVICE);
            ViewGroup container  = (ViewGroup) layoutInflater.inflate(R.layout.news,null);

popupWindow = new PopupWindow(container,800,1300,true);
            lst = container.findViewById(R.id.listView1);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android. R.layout.simple_list_item_1,local);
            lst.setAdapter(adapter);
            lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Toast.makeText(MainActivity.this,local[i],Toast.LENGTH_SHORT).show();
                }
            });

popupWindow.showAsDropDown(news1);
            container.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    popupWindow.dismiss();
                    return false;
                }
            });
        }
    });

}

Hope this helps.
Best regards,

Related Problems and Solutions