Question

I've got a LinearLayout in my layout folder, called "settings_caching_popup.xml" I tried to geth this layout in the onOptionsItemSelected(MenuItem item) method for showing a popup. But findViewById(R.layout.settings_caching_popup) returns always null. Then I set an ID in the xml Layout to the LinearLayout with android:id="@+id/settings_caching_popup and called findViewById(R.id.settings_caching_popup). Returns also null.

Pullout of the onOptionsItemSelected(MenuItem item):

PopupWindow popUp = new PopupWindow(context);

LinearLayout ll = (LinearLayout) findViewById(R.layout.settings_caching_popup);

popUp.setContentView(ll);
popUp.showAtLocation(ll, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
Was it helpful?

Solution

You need to inflate the layout you want to show in the popup. findViewById() only returns views, that already have been inflated.

Try this:

final PopupWindow popUp = new PopupWindow(context);
LayoutInflater inflater = LayoutInflater.from(this);
final LinearLayout ll =
    (LinearLayout)inflater.inflate(R.layout.settings_caching_popup, null);
popUp.setContentView(ll);
ll.post(new Runnable() {
    public void run() {
        popUp.showAtLocation(ll, Gravity.BOTTOM, 10, 10);
        popUp.update(50, 50, 300, 80);
    }
});

Notice, that this in LayoutInflater.from(this); has to be your activity. So, if you want to call this from an OnClickListener or something similar, you need to put YourActivity.this there.

OTHER TIPS

you are using:

LinearLayout ll = (LinearLayout) findViewById(R.layout.settings_caching_popup);

the method is Find View by ID, so you should get it by it's ID

LinearLayout ll = (LinearLayout) findViewById(R.id.settings_caching_popup);

You can't use findViewById until the view that you're looking for has been inflated from the underlying xml. My guess is that the view you're looking for needs to be inflated first. The main inflation of views typically occurs on onCreate(...) with lines like setContentView(...). For the menus, inflation also occurs in onCreateOptionsMenu(...) where you see things like:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.action_menu, menu);

    // findViewById will now work for views related to the options menu
}

For views which are not menus, use the LAYOUT_INFLATER_SERVICE as follows

inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.settings_caching_popup, null);
View childView = findViewById(R.id.childView); // view in R.layout.settings_caching_popup

As noted elsewhere here, use R.id. in findViewById and R.layout. with the inflater service.

Use

[parent view of settings_caching_popup].findViewById(R.id.settings_caching_popup);

instead of

findViewById(R.id.settings_caching_popup);

findViewById() is looking for views within the layout that was inflated for the activity when you called setContentView() (or something similar)

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