title: How to add listView with scroll inside a Popup window in android studio?
body: <p>I am trying to make an android app which opens a popup window when clicked on a button. And, the popup window has four TextView with heading and 4 listView with options. However, I could not add listView inside a popup window. The popup window opens when clicking a button but when I add listView and its adapter and try to run it, the app crashes. I want the layout of PopUp window to be as one in news.xml file.</p>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;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"&gt;
  
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:id="@+id/linear"&gt;
  
          &lt;RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"&gt;
  
              &lt;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" /&gt;
  
  
          &lt;/RelativeLayout&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/ScrollView&gt;
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>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&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1,local);
          lst.setAdapter(adapter);
          lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; 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;
                      }
                  });
              }
          });
  
      }
  }
  </code></pre>
  
  <p><strong>news.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#ABEBC6"&gt;
  
      &lt;RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_alignParentStart="true"
          android:layout_alignParentTop="true"&gt;
  
          &lt;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" /&gt;
  
          &lt;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" /&gt;
  
          &lt;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" /&gt;
  
          &lt;ListView
              android:id="@+id/listView2"
              android:layout_width="160dp"
              android:layout_height="184dp"
              android:layout_alignParentEnd="true"
              android:layout_alignTop="@+id/listView1" /&gt;
  
      &lt;/RelativeLayout&gt;
  
      &lt;RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_marginTop="260dp"&gt;
  
          &lt;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" /&gt;
  
          &lt;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" /&gt;
  
          &lt;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" /&gt;
  
          &lt;ListView
              android:id="@+id/listView4"
              android:layout_width="162dp"
              android:layout_height="184dp"
              android:layout_alignParentEnd="true"
              android:layout_below="@+id/textView4" /&gt;
      &lt;/RelativeLayout&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>strings.xml</strong></p>
  
  <pre><code>&lt;resources&gt;
      &lt;string name="app_name"&gt;Popup&lt;/string&gt;
          &lt;string name="news"&gt;Check Latest News!&lt;/string&gt;
          &lt;string name="local"&gt;Local News&lt;/string&gt;
          &lt;string name="national"&gt;National News&lt;/string&gt;
          &lt;string name="sports1"&gt;Sports One&lt;/string&gt;
          &lt;string name="sports2"&gt;Sports Two&lt;/string&gt;
  
  
          &lt;string-array name="local"&gt;
              &lt;item&gt;Star Ledger&lt;/item&gt;
              &lt;item&gt;Ny Times&lt;/item&gt;
              &lt;item&gt;The Record&lt;/item&gt;
              &lt;item&gt;Trentorian&lt;/item&gt;
  
          &lt;/string-array&gt;
  
  &lt;/resources&gt;
  </code></pre>