Roon13
Roon13

Reputation: 447

TextView is not acquiring space of ImageView

I am trying to get News from one site. Some articles have images. Here is my problem: some article doesn't have images so i want remove that Imageview for that row and title textview should acquire that space. I tried to implement it in BaseAdapter but setvisibility(View.Gone) is not working i guess.

Here is my layout file:

<!-- Thumbnail Image -->
<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/thumbnail"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="8dp" />

<!-- News Title -->
<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/title"
    android:ellipsize="end"
    android:textStyle="bold"
    android:layout_alignTop="@+id/thumbnail"
    android:layout_toRightOf="@+id/thumbnail"/>

Here is my implementation in base adapter:

  NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.thumbnail);
    TextView title = (TextView) convertView.findViewById(R.id.title);

    // getting movie data for the row
    Movie m = movieItems.get(position);

    if(m.getThumbnailUrl() == null)
    {

     thumbNail.setVisibility(View.GONE); //Not working

    }
    else {
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
    }

And current output is: I want that blank space of Imageview to be used by textview.

Example

When I try with ViewHolder for smooth transition i am unable to implement this logic:

if (position==-1)
    {
        viewHolder.thumbnail.setVisibility(View.GONE);
    }
    else
    {
        viewHolder.thumbnail.setImageUrl(news.getThumbnailUrl(), imageLoader);
    }

Upvotes: 0

Views: 97

Answers (5)

Hawraa Khalil
Hawraa Khalil

Reputation: 261

try this:

    if(m.getThumbnailUrl() == null)
    {
     thumbNail.setVisibility(View.GONE);
     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,  LinearLayout.LayoutParams.WRAP_CONTENT);
lp.addrule(LinearLayout.RIGHT_OF,0);
         lp.setMargins(-150,0,0,0); //set your margins for your textview as your want
         title.setLayoutParams(lp);
         Your_Linear_Layout_Name.addView(title, lp);
        //or RelativeLayoutName.addView(title, lp);
        }

        .....

Upvotes: 1

Florin T.
Florin T.

Reputation: 85

It's because of how the ListView works. You can try something like this:

if(m.getThumbnailUrl() == null)
    {

     thumbNail.setVisibility(View.GONE);
     LayoutParams layoutParams = title.getLayoutParams();
     layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
     layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
     title.setLayoutParams(layoutParams); 
    }

    else {

        thumbanil.setVisibility(View.VISIBLE);
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
        LayoutParams layoutParams = title.getLayoutParams();
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        title.setLayoutParams(layoutParams); 

    }

Upvotes: 1

Santosh Kathait
Santosh Kathait

Reputation: 1444

You can check whether URL for image is valid or not then you can use Visibility gone concept.Use something like below code:

if(!URLUtil.isValidUrl(m.getThumbnailUrl())){
    thumbNail.setVisibility(View.GONE);
}else {
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
        thumbNail.setVisibility(View.VISIBLE);
    }

Hope this help you.

Upvotes: 1

Praveena
Praveena

Reputation: 6941

When you don't have an image for article set LayoutParameters for that particular TextView programmatically.

if(m.getThumbnailUrl() == null)
{

 thumbNail.setVisibility(View.GONE);
 RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) title.getLayoutParams();
 layoutParams.addRule(RelativeLayout.RIGHT_OF,0);
 layoutParams.addRule(RelativeLayout.ALIGN_TOP,0);
 layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
 title.setLayoutParams(layoutParams);
}

Upvotes: 1

FelipeJM
FelipeJM

Reputation: 136

The m.getThumbnailUrl() return a String, right?

Did you try this?

if(m.getThumbnailUrl() == null || TextUtils.isEmpty(m.getThumbnailUrl()))

Try to debug and check if hit this line:

thumbNail.setVisibility(View.GONE);

Upvotes: 1

Related Questions