Esraa
Esraa

Reputation: 165

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?

Upvotes: 8

Views: 22059

Answers (4)

tahsinRupam
tahsinRupam

Reputation: 6396

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>

Upvotes: 16

the_dani
the_dani

Reputation: 2524

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!

Upvotes: 1

mahiraj2709
mahiraj2709

Reputation: 71

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();

Upvotes: 4

Shahbaz Ansari
Shahbaz Ansari

Reputation: 187

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();

Upvotes: 2

Related Questions