Textview is getting behind other objects

Asked

Viewed 1,571 times

3

In my application, I have a textview that is getting behind the other objects on the screen, I wanted it to end where Imageview starts on the screen.

This layout is used and filled in an Adapter for Listview.

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

  <TextView
    android:id="@+id/tvBoleiro"
    android:layout_width="wrap_content"
    android:layout_height="40sp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="3dp"
    android:layout_marginTop="3dp"
    android:layout_marginRight="3dp"        
    android:layout_toLeftOf="@+id/ivGols"
    android:layout_alignParentRight="true"
    android:layout_weight=".8"        
    android:ellipsize="end"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:textSize="25sp" />

<ImageView
    android:id="@+id/ivGols"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/btPagamento"
    android:layout_weight="0.05"
    android:background="@null"
    android:contentDescription="@string/app_name"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:src="@drawable/ic_gol" />

<Button
    android:id="@+id/btPagamento"
    android:layout_width="70dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/tvBoleiro"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_weight=".15" />   
</RelativeLayout>

Imagem do Problema

  • A doubt, you posted the entire XML?

  • Yes @Guilhermenascimento

  • They are two XML’s one with only Listview and another that is the one I posted that makes the listview line

  • I can’t tell you this, but I wanted to limit the textview size so it doesn’t look like this, I was able to edit the post with full xml.

  • Good afternoon, did any of the answers solve your problem? If yes please mark it as "Correct". If you do not say what is missing. Thank you.

3 answers

1

When you work with RelativeLayout you should take into account the relative position of the elements around you, for example: the left of (android:layout_toLeftOf), below (android:layout_below), etc. When you want to do the distribution of the elements considering a weight (android:layout_weight) utilize LinearLayout, otherwise you won’t have the expected result.

In the layout that you did, you used at the same time in TextView the properties: android:layout_alignParentLeft="true" and android:layout_alignParentRight="true", that is, you determined that your TextView will be aligned left and right, occupying the entire width of the parent element (RelativeLayout).

So that your TextView not be superimposed by other elements, remove from it the line android:layout_alignParentRight="true", can also remove the android:layout_weight, they will not make any difference (in this case), your XML should look like this:

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

    <TextView
        android:id="@+id/tvBoleiro"
        android:layout_width="wrap_content"
        android:layout_height="40sp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginTop="3dp"
        android:layout_toLeftOf="@+id/ivGols"
        android:ellipsize="end"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:maxLines="1"
        android:textSize="25sp" />

    <ImageView
        android:id="@id/ivGols"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/btPagamento"
        android:background="@null"
        android:contentDescription="@string/app_name"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@drawable/ic_gol" />

    <Button
        android:id="@id/btPagamento"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/tvBoleiro"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</RelativeLayout>

If you choose to use LinearLayout, an alternative would be:

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

    <TextView
        android:id="@+id/tvBoleiro"
        android:layout_width="0dp"
        android:layout_height="40sp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginTop="3dp"
        android:layout_weight=".65"
        android:ellipsize="end"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:maxLines="1"
        android:textSize="25sp" />

    <ImageView
        android:id="@+id/ivGols"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".15"
        android:background="@null"
        android:contentDescription="@string/app_name"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@drawable/ic_gol" />

    <Button
        android:id="@+id/btPagamento"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".20"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</LinearLayout>

Note: used android:layout_width="0dp" so that the elements are distributed correctly according to their weights and android:maxLines="1" to appear the ... when and text is greater than the limit.

References

Linear Layout
Relative Layout
Add ellipsis in Textview with one or multiple lines

0

In this case to prevent "overlap" (over position of items) it will be necessary to use LinearLayout instead of RelativeLayout

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

  <TextView
    android:id="@+id/tvBoleiro"
    android:layout_width="wrap_content"
    android:layout_height="40sp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="3dp"
    android:layout_marginTop="3dp"
    android:layout_marginRight="3dp"        
    android:layout_toLeftOf="@+id/ivGols"
    android:layout_alignParentRight="true"
    android:layout_weight=".8"        
    android:ellipsize="end"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:textSize="25sp" />

<ImageView
    android:id="@+id/ivGols"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/btPagamento"
    android:layout_weight="0.05"
    android:background="@null"
    android:contentDescription="@string/app_name"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:src="@drawable/ic_gol" />

<Button
    android:id="@+id/btPagamento"
    android:layout_width="70dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/tvBoleiro"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_weight=".15" />   
</LinearLayout>

Note: maybe in the <Button> it is necessary to use wrap_content in width also, so <Button ... android:layout_width="wrap_content">

0

Inside the Adapter in the getView method has the snippet to add the Badgeview:

if (convertView == null) {

        holder = new PresencaViewHolder();

        convertView = mInflater.inflate(R.layout.tela_lista_presenca_linha,
                null);

        // Find the child views.
        holder.tvNomeBoleiro = (TextView) convertView
                .findViewById(R.id.tvBoleiro);
        holder.btPagamento = (Button) convertView
                .findViewById(R.id.btPagamento);
        holder.btAddGol = (ImageView) convertView.findViewById(R.id.ivGols);

        holder.badge = new BadgeView(context, holder.btAddGol);

        convertView.setTag(holder);
    } else {
        // Because we use a ViewHolder, we avoid having to call
        // findViewById().
        holder = (PresencaViewHolder) convertView.getTag();

    }

Analyzing the lib code of Badgeview vi that it uses Framelayout, so I used the layout you posted and put the imageview that receives the Badge inside a Framelayout and worked.

<FrameLayout
    android:id="@+id/laGols"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/btPagamento" >

    <ImageView
        android:id="@+id/ivGols"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:contentDescription="@string/app_name"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:src="@drawable/ic_gol" />
</FrameLayout>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.