Search code examples
androidandroid-layoutandroid-scrollview

android scrollview vertical scrollbar not showing up


So I have below layout for custom AlertDialog:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popuplayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:fillViewport="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/popup_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dip"
        android:gravity="center"
        android:text="DateTime"
        android:textColor="#cccccc"
        android:textSize="14sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:background="#333333" />

    <ScrollView
        android:id="@+id/popup_scrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:orientation="vertical"
        android:padding="10dp" >

        <TextView
            android:id="@+id/popup_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Message Text"
            android:textColor="#FFCC00"
            android:textSize="18sp" />
    </ScrollView>

    <Button
        android:id="@+id/btnSpam"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="markSpam"
        android:padding="10dp"
        android:text="Mark as Spam" />

</LinearLayout>

and here is how I am showing it:

getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(0));

LayoutInflater layoutInflater = LayoutInflater.from(context);

View promptView = layoutInflater.inflate(
    R.layout.activity_show_notification, null);

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
    context);

alertDialogBuilder.setView(promptView);

// create an alert dialog
AlertDialog popup = alertDialogBuilder.create();

popup.setTitle(title);
popup.setMessage("");
popup.show();

TextView messageText = (TextView) popup.findViewById(android.R.id.message);
messageText.setPadding(0, 0, 0, 0);
messageText.setHeight(0);
messageText.setVisibility(View.INVISIBLE);

TextView popupDate = (TextView) popup.findViewById(R.id.popup_date);
TextView popupText = (TextView) popup.findViewById(R.id.popup_text);
popupDate.setText(mdate);
popupText.setText(message);

popupText.setMovementMethod(new ScrollingMovementMethod());

However when there is longer text, it is cut and vertical scrollbar doesn't show up :(

Can anyone help what I am missing here ? Your help will be greatly appreciated. Thanks

Update

Here is activity in app xml file using holo theme for the dialog:

 <activity
    android:name="com.domain.notifier.ShowNotification"
    android:label="@string/title_activity_show_notification"
    android:noHistory="true"
    android:theme="@android:style/Theme.Holo.Dialog" >
 </activity>

And styles.xml file:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.


    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.


        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

This is how I start alert dialog activity:

Intent intnt = new Intent(context, ShowNotification.class);
intnt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intnt.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intnt.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

intnt.putExtra("address", address);
intnt.putExtra("body", body);
intnt.putExtra("date", date);
context.startActivity(intnt);

Solution

  • I've tried to reproduce your issue and it works after some modifications. Basically I made the view lookup from the promptView.

        getWindow().setBackgroundDrawable(new ColorDrawable(0));
    
        LayoutInflater layoutInflater = LayoutInflater.from(this);
    
        View promptView = layoutInflater.inflate(
                R.layout.test, null);
    
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                this);
    
        alertDialogBuilder.setView(promptView);
    
        // create an alert dialog
        AlertDialog popup = alertDialogBuilder.create();
        popup.setTitle("title");
    
        TextView messageText = (TextView) promptView.findViewById(android.R.id.message);
        TextView popupDate = (TextView) promptView.findViewById(R.id.popup_date);
        TextView popupText = (TextView) promptView.findViewById(R.id.popup_text);
    
        popupDate.setText("date");
        popupText.setText(longTestString);
    
    
        popup.show();
    

    UPDATE:

    I've overlooked that in my solution the scrollview pushes out the button on the bottom. It works fine if you add a layout-weight in your Scrollview. It than takes all available space and displays the bars if necessary.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popuplayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:fillViewport="true"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/popup_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dip"
        android:gravity="center"
        android:text="DateTime"
        android:textColor="#cccccc"
        android:textSize="14sp" />
    
    <View
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:background="#333333" />
    
    <ScrollView
        android:id="@+id/popup_scrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:orientation="vertical"
        android:padding="10dp"
        android:layout_weight="0.5">
    
        <TextView
            android:id="@+id/popup_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Message Text"
            android:textColor="#FFCC00"
            android:textSize="18sp" />
    </ScrollView>
    
    <Button
        android:id="@+id/btnSpam"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="markSpam"
        android:padding="10dp"
        android:text="Mark as Spam" />
    

    enter image description here