Question

I have a popup. i want to change the background of the LinearLayout of the popup window dynamically from android:background="@drawable/popup_bg" to android:background="@drawable/popup_bg2"

here's my code:

    private void showPopup(final Activity context, int x, int y, String id, String n, String a, String l) {
   int popupWidth = 200;
   int popupHeight = 150;

   // Inflate the popup_layout.xml
   LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);


   LayoutInflater layoutInflater = (LayoutInflater) context
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

  TextView id1 = (TextView) layout.findViewById(R.id.id);
  TextView name = (TextView) layout.findViewById(R.id.name);
  TextView absent = (TextView) layout.findViewById(R.id.absence);
  TextView late = (TextView) layout.findViewById(R.id.lateness);

  id1.setText(id);
  name.setText(n);
  absent.setText(a);
  late.setText(l);

   // Creating the PopupWindow
   final PopupWindow popup = new PopupWindow(context);
   popup.setContentView(layout);
   popup.setWidth(popupWidth);
   popup.setHeight(popupHeight);
   popup.setFocusable(true);

   // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
   int OFFSET_X = 30;
   int OFFSET_Y = 300;

     // Clear the default translucent background
   popup.setBackgroundDrawable(new BitmapDrawable());

   // Displaying the popup at the specified location, + offsets.
   popup.showAtLocation(layout, Gravity.NO_GRAVITY, x + OFFSET_X, y +OFFSET_Y);

   // Getting a reference to Close button, and close the popup when clicked.
   Button close = (Button) layout.findViewById(R.id.close);
   close.setOnClickListener(new OnClickListener() {

     public void onClick(View v) {
       popup.dismiss();
     }
   });
}

popup_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
     android:id="@+id/popup"
     android:layout_height="wrap_content"
     android:background="@drawable/popup_bg" 
     android:orientation="vertical" >
</LinearLayout>
Was it helpful?

Solution

Try this,

     LinearLayout ll=popup.getContentView().findViewById(R.id.popup);
     ll.setBackgroundResource(R.drawable.yourImage);

provided, you do this, popup.setContentView(layout); before doing the above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top