virendrao
virendrao

Reputation: 1813

Textview gets over ImageView back if text in textview is large Android?

I have created custom action bar which has ImageView of back button on left and center has textview which is title. I want title to be center. However if text is large it occupies every widhth available and overlaps Imageview as a result ImageView listener doesnt function and only TextView on click works any where on custom layout. Following is screenshot to explain you more.

TextView if large overlaps Back ImageView and onClick of Image doesnt work

If Textview is large i want textView to be center and move all characters to right for width available and then end with ...

Following is my code of XML:

<RelativeLayout
            android:id="@+id/actionbar"
            android:layout_width="match_parent"
            android:layout_height="45dip"
            android:layout_alignParentTop="true"
            android:alpha="0.8"
            android:background="@android:color/black"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/backbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:paddingLeft="10dp"
                android:src="@drawable/back" />

            <TextView
                android:id="@+id/titleofscreen"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:maxLines="1"
                android:textColor="#22c064"
                android:textSize="18sp" />
        </RelativeLayout>

Please help i want something like TextView center and it should look like asdasdasdasd ...

Upvotes: 1

Views: 551

Answers (4)

Meenal
Meenal

Reputation: 2877

change this code

 <ImageView
            android:id="@+id/backbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:paddingLeft="10dp"
            android:src="@drawable/back" />

        <TextView
            android:id="@+id/titleofscreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:maxLines="1"
            android:textColor="#22c064"
            android:textSize="18sp" />

to

       <TextView
            android:id="@+id/titleofscreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:maxLines="1"
            android:drawablePadding="5dip"
            android:drawableLeft="@drawable/back"
            android:textColor="#22c064"
            android:textSize="18sp" />

Upvotes: 1

Danial Hussain
Danial Hussain

Reputation: 2530

check thisUse just need to provide weight so which each view get percentage of total width.

    <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/actionbar"
android:layout_width="match_parent"
android:layout_height="45dip"
android:alpha="0.8"
android:background="@android:color/black"
android:orientation="horizontal"
android:weightSum="2" >

<ImageView
    android:id="@+id/backbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.25"
    android:paddingLeft="10dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/titleofscreen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1.75"
    android:maxLines="1"
    android:layout_gravity="center"
    android:paddingLeft="5dp"
     android:text="[2014-10-20 17:05:35 - NVOLV3] Unknown error: java.lang.NullPointerException [2014-10-20 17:05:35 - NVOLV3] Unknown error: java.lang.NullPointerException"
    android:singleLine="true"
    android:textColor="#22c064"
    android:textSize="18sp" />

  </LinearLayout>

Upvotes: 1

Muzikant
Muzikant

Reputation: 8090

Remove android:layout_centerInParent="true" and add android:layout_toRightOf="@+id/backbutton" from the TextView

I assume you should use android:layout_centerVertical="true" to align the text with the ImageView

Upvotes: 0

M D
M D

Reputation: 47817

try this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rel_music"
android:layout_width="fill_parent"
android:layout_height="46dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingRight="3dp" >

<LinearLayout
    android:id="@+id/back_lay"
    android:layout_width="46dp"
    android:layout_height="46dp"
    android:layout_marginLeft="10dp"
    android:background="@android:color/transparent"
    android:gravity="center_vertical|center_horizontal" >
    <Button
        android:id="@+id/ib_back_music"
        android:layout_width="30dp"
        android:layout_height="32dp"
        android:layout_marginLeft="5dp"
        android:clickable="true"
        android:gravity="center_vertical|center_horizontal|center" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="0dp"
    android:gravity="center_vertical|center_horizontal"
    android:paddingLeft="3dp" >

    <TextView
        android:id="@+id/txt_music"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="title title title title title title title title title title "
        android:typeface="normal" />
   </LinearLayout>

</LinearLayout>

Upvotes: 1

Related Questions