How to change the contents of a pop up window

2.3k views Asked by At

I'm trying to write an app where the user clicks on an item in a listview. Then a pop up window will show up containing some information regarding the item the user clicked on. The pop up window shows up with no problem, but the code does not change the contents of the TextViews or Buttons. Any feedback will be appreciated.

Here is the code for the listview and pop up window:

public class MyRoutes extends LinearLayout {

    private Context context = null;
    private View contentView = null;
    private List<String> list=null;
    private int size = 0;
    private LayoutInflater nlayoutInflater;
    private View popup = null;
    public static PopupWindow myroutes_popupwin=null;



    public MyRoutes(Context context , List<String> all , int actionbarsize) {
        super(context);
        this.context = context;
        this.list=all;
        this.size=actionbarsize;
        getLayout();
    }

    private void getLayout() {


        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.
                LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);   

        layoutParams.topMargin = (int) ((1.0)*(int)(size));
        layoutParams.leftMargin = (int)((0.0)*(int)(size));
        layoutParams.bottomMargin=(int) ((0.0)*(int)(size));
        layoutParams.rightMargin= (int)((0.0)*(int)(size));        

        contentView = layoutInflater.inflate(R.layout.myroutes, null);
        contentView.setLayoutParams(layoutParams);
        this.addView(contentView);
        final ListView lv = (ListView) findViewById(R.id.navigation_routes);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1, list);
        lv.setAdapter(arrayAdapter);

        Button close = (Button) findViewById(R.id.navigation_close_routes);

        nlayoutInflater = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);   
        popup = nlayoutInflater.inflate(R.layout.route, null);

        final PopupWindow myroutes_popupwin = new PopupWindow(context);
        myroutes_popupwin.setContentView(popup);
        myroutes_popupwin.setWindowLayoutMode(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

//      myroutes_popupwin=new PopupWindow(popup, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        close.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(myroutes_popupwin.isShowing()){
                    myroutes_popupwin.dismiss();
                }else{
                contentView.setVisibility(View.GONE);
                }

            }
        });

        lv.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
                myroutes_popupwin.dismiss();
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub
                myroutes_popupwin.dismiss();
            }
        });

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String temp = list.get(position);
                if(myroutes_popupwin.isShowing()){
                    myroutes_popupwin.dismiss(); 
                }
                try{


                lv.setFocusableInTouchMode(true);

                // doing some stuff to read item information.                   

//              myroutes_popupwin.showAsDropDown(view);
                myroutes_popupwin.showAtLocation(view, Gravity.CENTER, 0, 0);




                TextView trailname = (TextView) findViewById(R.id.myroutes_name); // THIS IS where I have problem, the code is not able to read the textview
                trailname.getText().toString();


                }catch(Exception e){e.printStackTrace();}

            }
        });


    }



}

and here is the code for the popup window

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="0dp"
    android:paddingRight="5dp"
    android:paddingLeft="5dp"
    android:background="#C0000000"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/myroutes_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Trail Name"
        android:paddingBottom="2dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp"        
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Date"
        android:textSize="15sp"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:textStyle="italic" 
        android:paddingRight="5dp"
        android:paddingTop="3dp"
        android:text="Date" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Total Distance"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="10dp"
        android:text="Total Distance"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Average Speed"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp"
        android:text="Average Speed"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="italic" />
     <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Duration"
        android:paddingBottom="15dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp"
        android:text="Duration"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="italic" />
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="0dp"
    android:background="#00000000"
    android:orientation="horizontal" >
     <Button
         android:id="@+id/myroutes_display"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:drawableTop="@drawable/add"
         android:layout_marginLeft="1dp"
         android:text="display"
         android:paddingBottom="5dp"
         android:textSize="10sp"
         android:background="#00000000"
         android:textColor="@color/AliceBlue"/>

     <Button
         android:id="@+id/imageButton2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:drawableTop="@drawable/add"
         android:layout_marginLeft="1dp"
         android:text="resume"
         android:paddingBottom="5dp"
         android:textSize="10sp"
         android:background="#00000000"
         android:textColor="@color/AliceBlue"/>

     </LinearLayout>

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="0dp"
    android:background="#00000000"
    android:orientation="horizontal" >
     <Button
         android:id="@+id/imageButton3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:drawableTop="@drawable/add"
         android:layout_marginLeft="1dp"
         android:text="share"
         android:paddingTop="5dp"
         android:paddingBottom="10dp"
         android:textSize="10sp"
         android:background="#00000000"
         android:textColor="@color/AliceBlue"/>

     <Button
         android:id="@+id/imageButton4"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:drawableTop="@drawable/add"
         android:layout_marginLeft="1dp"
         android:text="delete"
         android:paddingTop="5dp"
         android:paddingBottom="10dp"
         android:textSize="10sp"
         android:background="#00000000"
         android:textColor="@color/AliceBlue"/>

     </LinearLayout>





</LinearLayout>
2

There are 2 answers

1
Markus Rubey On BEST ANSWER

You have to call findViewById() on your ContentView and call setText() to change your content.

TextView textView = (TextView) popup.getContentView().findViewById(R.id.textView_id);
textView.setText("New Content");


A small tip: If you set a background on your PopupWindow you get the dismiss behavior for touches outside of the PopupWindow for free.

popup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
0
akadouri On

Checkout AlertDialog or DialogFragment. Those should be easier to use than building your own pop-up solution.