Search code examples
javaandroidlayoutcustomdialog

View not showing properly in android layout


I am designing custom dialogue in android, For this purpose I designed xml file as

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/item_bg_color"
    android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">

    <TextView android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/custom_dialog_text"
    android:textColor="#000000" 
    android:textSize="14dip"
    android:typeface="serif"
    android:singleLine="false"
    android:text="You are not authorized to use this application." 
    android:layout_marginTop="10dip"/>

    <Button android:id="@+id/custom_button_ok" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="Ok" 
    android:layout_below="@+id/custom_dialog_text"
    android:layout_toRightOf="@+id/custom_dialog_text"/>


</RelativeLayout>

Problem :

I want to show button at end (Right) of TextView, And textview's text should be in more than one line(if more characters have to show). But when I am using this layout, button is not appearing anywhere.

I used LinearLayout and TableLayout also and in both cases, button is appearing with a little part. Where am I wrong?


Solution

  • Basically you want to tell the button to be align to the right, take all the space it needs, then give the rest of the space to the TextView. You do this by specifying android:layout_layout_alignParentRight="true" for the button and android:layout_toLeftOf="@id/custom_button_ok" for the TextView. Mind the order!!!

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/item_bg_color" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">
      <Button android:id="@+id/custom_button_ok" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"
       android:text="Ok" 
       android:layout_layout_alignParentRight="true" />
    
      <TextView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/custom_dialog_text"
       android:textColor="#000000" 
       android:textSize="14dip"
       android:typeface="serif"
       android:singleLine="false"
       android:text="You are not authorized to use this application." 
       android:layout_marginTop="10dip"
       android:layout_toLeftOf="@id/custom_button_ok"
      />
    </RelativeLayout>