Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView in a pop up window

I am making an application and using a custom adapter for ListView. It is working in the activity

public class Adapter extends ArrayAdapter {
    Context mContext;
    int resourceID;
    ArrayList<String> names;
    public Adapter(Context context, int resource, ArrayList<String> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.resourceID=resource;
        this.names= objects;
    }

    @Override
    public String getItem(int position) {
        return names.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        row = inflater.inflate(resourceID, parent, false);

        TextView text = (TextView) row.findViewById(R.id.text);

        text.setText(names.get(position));
        return row;
    }

}

I used this code to make them appear on an activity

    myNames= (ListView) findViewById(R.id.List);
    adapter = new Adapter(this,R.layout.names_view, Current.Names);
    myNames.setAdapter(adapter);

now I want to click on a button to make the same list appear on a popup, any help?

like image 369
Esraa Avatar asked Feb 25 '17 16:02

Esraa


4 Answers

You may do as following:

1) Create a custom dialog on button click:

Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setOnClickListener( new OnClickListener() {

        @Override
        public void onClick(View v) {

            final Dialog dialog = new Dialog(YourActivity.this);
            dialog.setContentView(R.layout.custom_dialog);
            dialog.setTitle("Title...");
            myNames= (ListView) dialog.findViewById(R.id.List);
            adapter = new Adapter(YourActivity.this,R.layout.names_view, Current.Names);
            myNames.setAdapter(adapter);
            dialog.show();

        }
    });

2)Add the listview in the dialog layout (custom_dialog.xml):

<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">

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

</LinearLayout>
like image 146
tahsinRupam Avatar answered Oct 16 '22 22:10

tahsinRupam


custom.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</ListView> 

activity

String names[] ={"A","B","C","D"};
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                LayoutInflater inflater = getLayoutInflater();
                View convertView = (View) inflater.inflate(R.layout.custom, null);
                alertDialog.setView(convertView);
                alertDialog.setTitle("List");
                ListView lv = (ListView) convertView.findViewById(R.id.List);
                Adapter<String> adapter = new Adapter(this,R.layout.names_view, Current.Names);
                lv.setAdapter(adapter);
                alertDialog.show();
like image 42
mahiraj2709 Avatar answered Oct 16 '22 22:10

mahiraj2709


You can use custom dialog

custom dialog mydialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
    android:id="@+id/lv"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"/>
</LinearLayout>

In your activity

Dialog dialog = new Dialog(Activity.this);
   dialog.setContentView(R.layout.mydialog)
ListView lv = (ListView ) dialog.findViewById(R.id.lv);
dialog.setCancelable(true);
dialog.setTitle("ListView");
dialog.show();
like image 20
Shahbaz Ansari Avatar answered Oct 16 '22 20:10

Shahbaz Ansari


I typed "PopupWindow" into google and found this:

https://www.youtube.com/watch?v=fn5OlqQuOCk

I didn't knew the PopupWindow until now and I'd recommend to use a dialog e.g. AlertDialog with a costum view.

I hope I could help!

like image 20
the_dani Avatar answered Oct 16 '22 22:10

the_dani