Search code examples
androidxmlandroid-layoutandroid-scrollviewscrollable

ScrollView with fixed height inside dialog in Android


I am trying to do ScrollView in dialog popup with this code:

    <?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="40dp" >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="40sp"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/new_realm_avatar"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_centerHorizontal="true"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/new_realm_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/new_realm_avatar"
            android:text="long text" />
    </RelativeLayout>
 </ScrollView>

</RelativeLayout>

So when I call this in my message part and the text is bigger than the screen it becomes scrollable but hides the keys and it's height is as big as the phone screen. I want the message in the dialog to be with fixed height like I tried by wrapping the ScrollView with another layout and enforcing it height but it doesn't work that way too. So anyone has any ideas?


Solution

  • Try this

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="40dp" >
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="40dp" >
    
            <ImageView
                android:id="@+id/new_realm_avatar"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_centerHorizontal="true"
                android:src="@drawable/ic_launcher" />
    
            <TextView
                android:id="@+id/new_realm_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/new_realm_avatar"
                android:text="long text" />
        </RelativeLayout>
    </ScrollView>
    
    </RelativeLayout>