Perhaps not quite correctly put it. The bottom line is this. I have a Recyclerview. I wrote my adapter for it. There are 3 types of cells. Depending on the type of cell connects its template (layout). One of the types of advertising cells. For it connects the following layout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ad_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:orientation="vertical"> </LinearLayout> 

In the adapter, this template is connected as follows:

  public PostFeedAdapter.RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { this.mContext = parent.getContext(); View view; switch (viewType) { case HEADER: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_header_layout, parent, false); return new RecyclerViewHolder(view, viewType, headerType, header.parsed() && header.isFriendignoreBtnsEnabled()); case POST: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_post_layout, parent, false); return new RecyclerViewHolder(view, viewType, headerType, header.parsed() && header.isFriendignoreBtnsEnabled()); case ADVERT: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adspost_layout, parent, false); return new RecyclerViewHolder(view, viewType, headerType, header.parsed() && header.isFriendignoreBtnsEnabled()); default: return null; } } 

Namely this is adspost_layout. To make an ad request, I need to specify the size of the ad unit. In the examples of Google code, I found this method:

 mRecyclerView.post(new Runnable() { @Override public void run() { final LinearLayout lv = (LinearLayout) findViewById(R.id.ad_view); final int adWidth = lv.getWidth(); } }); 

But this method does not work for me. My lv turns out to be null, so I can't get its size. Honestly, I first meet the .post (new Runnable () method, so I don’t quite understand how it works and what needs to be done to make it work correctly.

How do I get the correct width of the LinearLayout?

  • one
    If there are no more indents anywhere, then why not just give the screen width? - Yuriy SPb
  • @Yuriy SPb I thought so too, but in the example of Google, I substituted my template (where LinearLayout is pure) and how its width turned out to be 608 when the screen width is 640. - cheerful_weasel
  • So you have 32 pixel indents there. Look for them in the markup - YuriySPb
  • if you want to use native advertising, then most likely it already has a special wrapper for your adapter, for example, MoPub has MoPubRecyclerAdapter - Vitaly
  • one
    @YuriySPb in general about the screen width setting you are right after all) I checked, substituted the screen width and everything fit in normally. Thanks for the advice. - cheerful_weasel

0