Skip to content

Commit

Permalink
Extension Function For ListItem and Message
Browse files Browse the repository at this point in the history
  • Loading branch information
neeldoshii committed Apr 23, 2024
1 parent 41346ee commit 057ea62
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
25 changes: 25 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/utils/AlertDialogFacade.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ import android.content.DialogInterface.OnClickListener
import android.text.InputFilter
import android.view.LayoutInflater
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.ListView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
Expand Down Expand Up @@ -328,3 +331,25 @@ fun AlertDialog.Builder.listItems(items: List<CharSequence>, onClick: (dialog: D
onClick(dialog, which)
}
}

/**
* Extension workaround for Displaying ListView & Message Together
* Alert Dialog Doesn't allow message and listview together so a customView is used.
*
* @param messageResID The message which you want to display in the dialog
* @param items The items to display in the list.
* @param onClick A lambda function that is invoked when an item is clicked.
*/
fun AlertDialog.Builder.listItemsAndMessage(@StringRes messageResId: String?, items: List<CharSequence>, onClick: (dialog: DialogInterface, index: Int) -> Unit): AlertDialog.Builder {
val dialogView = View.inflate(this.context, R.layout.dialog_listview_message, null)
dialogView.findViewById<TextView>(R.id.dialog_message).text = messageResId

val listView = dialogView.findViewById<ListView>(R.id.dialog_list_view)
listView.adapter = ArrayAdapter(context, android.R.layout.simple_list_item_1, items)

val dialog = this.create()
listView.setOnItemClickListener { _, _, index, _ ->
onClick(dialog, index)
}
return this.setView(dialogView)
}
46 changes: 46 additions & 0 deletions AnkiDroid/src/main/res/layout/dialog_listview_message.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2024 Neel Doshi <neeldoshi147@gmail.com>
~
~ This program is free software; you can redistribute it and/or modify it under
~ the terms of the GNU General Public License as published by the Free Software
~ Foundation; either version 3 of the License, or (at your option) any later
~ version.
~
~ This program is distributed in the hope that it will be useful, but WITHOUT ANY
~ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
~ PARTICULAR PURPOSE. See the GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License along with
~ this program. If not, see <http://www.gnu.org/licenses/>.
-->

<!-- source: https://stackoverflow.com/a/50397736 -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:orientation="vertical">

<!--Dialog Message-->
<TextView
android:id="@+id/dialog_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/side_margin"
android:paddingEnd="@dimen/side_margin"
android:paddingTop="16dp"
android:textSize="@dimen/content_textsize"
tools:text="Dialog Message"/>

<!--Dialog ListItem-->
<ListView
android:id="@+id/dialog_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="8dp"
android:scrollbars="vertical"
android:divider="@null" />
</LinearLayout>

0 comments on commit 057ea62

Please sign in to comment.