Good day! I have a screen with one news: image + title.

enter image description here

News is downloaded via RSS feed, and, accordingly, the image is, that is, no.

I do not want to put a stub. I had the following idea: write android: layout_height = "wrap_content" in the layout and specify maxHeight. Therefore, if there was no image, there would be only a title. (in my dreams)

But, as indicated on the developers site , there is only android: minHeight.

Question: Is there a way to solve this problem through layout? If not, how do you usually deal with it?

    2 answers 2

    Use .setVisibility(View.GONE); when there is no image and .setVisibility(View.VISIBLE); when she is. Initially, in the xml markup, also specify for the picture android:visibility="gone" .

    When there is no picture - nothing will happen, when it is there - it will be displayed as you need.

      You can try to do the following:

      In the image of interest in the layout indicate

      android:adjustViewBounds="true" android:maxHeight="250dp" (нужная вам высота)

      in the MainActivity specify the method that will calculate this height

        public int getWindowHeightForImage() { return (int) Math.round(getWindowManager().getDefaultDisplay().getHeight() * 0.4); } 

      In the adapter, set a variable for the height (part of the code removed, only the part directly relevant to the question is shown)

       public class NewsFeedRecyclerViewAdapter extends RecyclerView.Adapter<NewsFeedRecyclerViewAdapter.ViewHolder> { private int mImageViewHeight; // вот эта переменная public NewsFeedRecyclerViewAdapter(Context context, RealmResults<News> items, int imageViewHeight) { mImageViewHeight = imageViewHeight; } 

      Specify this value in the holder

        class ViewHolder extends RecyclerView.ViewHolder { ImageView mNewsImage; public ViewHolder(View itemView) { super(itemView); mNewsImage = (ImageView) itemView.findViewById(R.id.one_news_main_picture); mNewsImage.getLayoutParams().height = mImageViewHeight; // наша переменная 

      and transfer to a fragment of our one item from the list

       private void setAdapter() { mAdapter = new NewsFeedRecyclerViewAdapter(getActivity(), results, ((MainActivity) getActivity()).getWindowHeightForImage());} 

      I understand that I have somehow explained it very clumsily now, but it works.