6

I want to display a text over an Image View. I do it like Alesqui suggested here: Android Text over image

The preview in Android studio looks fine: android Studio preview of the xml

But my actual result looks like this with the text unwantedly above:

Code in Emulator

I have to add the following XML dynamically to a LinearLayout during the execution:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/myImageSouce" />

    <TextView
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/myImageView"
        android:layout_alignTop="@+id/myImageView"
        android:layout_alignRight="@+id/myImageView"
        android:layout_alignBottom="@+id/myImageView"
        android:layout_margin="1dp"
        android:gravity="center"
        android:text="Hello"
        android:textColor="#000000" />

</RelativeLayout>

I add it the following way:

LinearLayout ll = (LinearLayout) findViewById(R.id.layout_story_covers);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    for (int i = 0; i < stories.size(); i++)
    {
        // add  imageView
        RelativeLayout coverLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_cover, null);
        ImageView imageView =(ImageView) coverLayout.findViewById(R.id.imageViewCover);
        TextView textView = (TextView) coverLayout.findViewById(R.id.textViewCover);
        textView.setText(stories.get(i).title);
        imageLoader.displayImage(stories.get(i).cover.fileUrl, imageView);
        ll.addView(coverLayout);
    }

This must have something to do with that parent LinearLayout. What is the proper way to do get the result?

Community
  • 1
  • 1
dba
  • 325
  • 1
  • 6
  • 16
  • the textcolor is wrong. it should be black, not yellow. And it should say Hello, not My Text. – njzk2 Nov 17 '14 at 19:36
  • yeah, it's inconsistent. But i neglected that due to its lesser importance. – dba Nov 18 '14 at 12:30

2 Answers2

5

Try this instead:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/myImageSouce" />

    <TextView
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="1dp"
        android:gravity="center"
        android:text="Hello"
        android:textColor="#000000" />

</RelativeLayout>
Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • There are many thing that this answer doesn't provide and you should stop doing. I had an answer as well trying to point them out. It shouldn't have been chosen as the correct one. If anyone has more improvement for my answer, please comment it. – jpardogo Nov 17 '14 at 16:07
  • I also would prefer FrameLayout here, but I don't know the full context of his app and it could be that he wants to do other things that require a RelativeLayout (perhaps he will add more views?), so I intentionally offered only the change that solves his immediate problem. – Karakuri Nov 17 '14 at 19:31
2

Tips:

  • Romain guy said: "I didn’t say don’t use RelativeLayout, you just must be aware of its (possible) impact on performance if used at the top of the tree." Much better to use a simple FrameLayout if you can.

  • Be aware of the scaleType property in ImageView's

  • If you want the ImageView to be the same size as the parent then you need to set it as match_parent height and width. If you use wrap content it will depend on the size of the bitmap and the resolution.

  • Consider using match_parent instead of the deprecated fill_parent

  • I would prefer Picasso for bitmaps management.

-

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/myImageSouce"
        android:scaleType="centerCrop"/>

    <TextView
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/spacing_medium"
        android:layout_gravity="center"
        tools:text="Test"/>
</FrameLayout>
Community
  • 1
  • 1
jpardogo
  • 5,636
  • 2
  • 23
  • 27
  • based on this https://www.infinum.co/the-capsized-eight/articles/top-5-android-libraries-every-android-developer-should-know-about I chose UIL. [Also interesting](http://stackoverflow.com/questions/19995007/local-image-caching-solution-for-android-square-picasso-vs-universal-image-load) – dba Nov 17 '14 at 17:51
  • Well, I should modify my answer and say that Picasso is just my personal preference. – jpardogo Nov 17 '14 at 17:59