Search code examples
javaandroidxmlandroid-dialogfragmentandroid-dialog

CheckBox not staying at bottom of dialog


In my custom dialog, I am trying to get my checkbox to stay underneath my scrollview but it's not working. I've tried adding the checkbox under the scrollview in xml but it doesn't appear whenever I run the app. What needs to be done in order for the checkbox to remain at the bottom of the dialog (underneath the scrollview), and not be part of the scrollview when I scroll that? I don't want my checkbox to scroll with the scrollview at all.

java

LayoutInflater inflater = LayoutInflater.from(this);
View dialog_layout = inflater.inflate(R.layout.fragment_overlay, (ViewGroup) findViewById(R.id.Overlay_linearLayout));
AlertDialog.Builder db = new AlertDialog.Builder(this);
db.setView(dialog_layout);
db.setPositiveButton("OK", new DialogInterface.OnClickListener() {
});
db.show();

xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Overlay_linearLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Overlay_scrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Overlay_tableLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="1.0">
        <TableRow
            android:padding="10dp">
            <ImageView
                android:layout_width="0px"
                android:layout_weight="0.3"
                android:layout_height="80dp"
                android:src="@drawable/img_0" />
            <TextView
                android:id="@+id/MapOverlay_textView0"
                android:layout_width="0px"
                android:layout_weight="0.7"
                android:text="@string/overlay_instruction0" />
        </TableRow>
        <TableRow
            android:padding="10dp">
            <ImageView
                android:layout_width="0px"
                android:layout_weight="0.3"
                android:layout_height="80dp"
                android:src="@drawable/img_1" />
            <TextView
                android:id="@+id/MapOverlay_textView1"
                android:layout_width="0px"
                android:layout_weight="0.7"
                android:text="@string/overlay_instruction1" />
        </TableRow>
        <TableRow
            android:padding="10dp">
            <ImageView
                android:layout_width="0px"
                android:layout_weight="0.3"
                android:layout_height="80dp"
                android:src="@drawable/img_2" />
            <TextView
                android:id="@+id/MapOverlay_textView2"
                android:layout_width="0px"
                android:layout_weight="0.7"
                android:text="@string/overlay_instruction2" />
        </TableRow>
        <TableRow
            android:padding="10dp">
            <ImageView
                android:layout_width="0px"
                android:layout_weight="0.3"
                android:layout_height="80dp"
                android:src="@drawable/img_3" />
            <TextView
                android:id="@+id/MapOverlay_textView3"
                android:layout_width="0px"
                android:layout_weight="0.7"
                android:text="@string/overlay_instruction3"
                style="@android:style/TextAppearance.Medium" />
        </TableRow>
        <TableRow>
            <CheckBox
                android:text="Don't show this again"
                android:id="@+id/skip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>
    </TableLayout>
</ScrollView>


Solution

  • Try to set checkbox and ScrolView inside of new LinearLayout and put checkbox outside the ScrollView like this :

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <ScrollView android:id="@+id/Overlay_scrollView"
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1">
    
            <TableLayout android:id="@+id/Overlay_tableLayout"
                         xmlns:android="http://schemas.android.com/apk/res/android"
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content"
                         android:weightSum="1.0">
    
                    <ImageView
                        android:layout_width="0px"
                        android:layout_height="80dp"
                        android:layout_weight="0.3"
                        android:src="@drawable/ic_launcher"/>
    
                    <TextView
                        android:id="@+id/MapOverlay_textView0"
                        android:layout_width="0px"
                        android:layout_weight="0.7"
                        android:text="tion0"/>
    
    
                    <ImageView
                        android:layout_width="0px"
                        android:layout_height="80dp"
                        android:layout_weight="0.3"
                        android:src="@drawable/ic_launcher"/>
    
                    <TextView
                        android:id="@+id/MapOverlay_textView1"
                        android:layout_width="0px"
                        android:layout_weight="0.7"
                        android:text="dada"/>
    
    
                    <ImageView
                        android:layout_width="0px"
                        android:layout_height="80dp"
                        android:layout_weight="0.3"
                        android:src="@drawable/ic_launcher"/>
    
                    <TextView
                        android:id="@+id/MapOverlay_textView2"
                        android:layout_width="0px"
                        android:layout_weight="0.7"
                        android:text="ruction2"/>
    
    
                    <ImageView
                        android:layout_width="0px"
                        android:layout_height="80dp"
                        android:layout_weight="0.3"
                        android:src="@drawable/ic_launcher"/>
    
                    <TextView
                        android:id="@+id/MapOverlay_textView3"
                        style="@android:style/TextAppearance.Medium"
                        android:layout_width="0px"
                        android:layout_weight="0.7"
                        android:text="_instruction3"/>
    
    
            </TableLayout>
        </ScrollView>
    
        <CheckBox
            android:id="@+id/skip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Don't show this again"/>
    </LinearLayout>
    

    UPDATE

    How to inflate xml file and set it to dialog

    LayoutInflater inflater = getLayoutInflater();
    View dialoglayout = inflater.inflate(R.layout.dialog_layout, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(dialoglayout);
    builder.show();