-1

I have a XML file with RelativeLayout, I also have Textview which has Gravity Center. In code I would like to get X coordinate of this Textview, where the TextView begins (length of TextView is not constant, it can change). I need a ImageView to be next to the TextView.

<TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="TextView" /> `

    countryText = (TextView) findViewById(R.id.textView6);
    countryText.getLeft()

getLeft() return 0;
Pratik Tank
  • 2,213
  • 1
  • 17
  • 29
M. Krsak
  • 13
  • 2

2 Answers2

0

Add android:layout_centerInParent="true" to textview and add android:layout_toLeftOf="@id/textView6" to imageview

saber safavi
  • 452
  • 3
  • 8
0

Simply get rid of the ImageView and add a compound drawable to the left of your TextView.
This is also more efficient, because it helps flattening your layout hierarchy.

I.e.:

<TextView
    android:id="@+id/textView6"
    android:drawableLeft="@drawable/my_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:text="TextView"
/>

Official docs

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115