improve the way selected contacts are shown, properly create new lines

This commit is contained in:
tibbi 2020-04-05 23:32:51 +02:00
parent d15005bfd7
commit 741e619822
3 changed files with 68 additions and 17 deletions

View File

@ -5,12 +5,13 @@ import android.os.Bundle
import android.provider.ContactsContract import android.provider.ContactsContract
import android.provider.ContactsContract.CommonDataKinds import android.provider.ContactsContract.CommonDataKinds
import android.text.TextUtils import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.WindowManager import android.view.WindowManager
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import com.simplemobiletools.commons.extensions.beVisible import android.widget.LinearLayout
import com.simplemobiletools.commons.extensions.getIntValue import android.widget.LinearLayout.LayoutParams
import com.simplemobiletools.commons.extensions.getStringValue import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.smsmessenger.R import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.adapters.AutoCompleteTextViewAdapter import com.simplemobiletools.smsmessenger.adapters.AutoCompleteTextViewAdapter
@ -72,10 +73,58 @@ class NewMessageActivity : SimpleActivity() {
private fun addSelectedContact(contact: Contact) { private fun addSelectedContact(contact: Contact) {
selectedContacts.add(contact) selectedContacts.add(contact)
selected_contacts.beVisible() selected_contacts.beVisible()
layoutInflater.inflate(R.layout.item_selected_contact, null).apply { message_divider_one.beVisible()
selected_contacts.onGlobalLayout {
val first = layoutInflater.inflate(R.layout.item_selected_contact, null).apply {
selected_contact_name.text = contact.name selected_contact_name.text = contact.name
selected_contacts.addView(this)
} }
showSelectedContact(arrayListOf(first))
}
}
// show selected contacts, properly split to new lines when appropriate
// based on https://stackoverflow.com/a/13505029/1967672
private fun showSelectedContact(views: ArrayList<View>) {
var newLinearLayout = LinearLayout(this)
newLinearLayout.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
newLinearLayout.orientation = LinearLayout.HORIZONTAL
val mediumMargin = resources.getDimension(R.dimen.medium_margin).toInt()
var widthSoFar = 0
var isFirstRow = true
for (i in views.indices) {
val LL = LinearLayout(this)
LL.orientation = LinearLayout.HORIZONTAL
LL.gravity = Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM
LL.layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
views[i].measure(0, 0)
var params = LayoutParams(views[i].measuredWidth, LayoutParams.WRAP_CONTENT)
params.setMargins(0, 0, mediumMargin, 0)
LL.addView(views[i], params)
LL.measure(0, 0)
widthSoFar += views[i].measuredWidth + mediumMargin
if (widthSoFar >= selected_contacts.width) {
isFirstRow = false
selected_contacts.addView(newLinearLayout)
newLinearLayout = LinearLayout(this)
newLinearLayout.layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
newLinearLayout.orientation = LinearLayout.HORIZONTAL
params = LayoutParams(LL.measuredWidth, LL.measuredHeight)
params.topMargin = mediumMargin
newLinearLayout.addView(LL, params)
widthSoFar = LL.measuredWidth
} else {
if (!isFirstRow) {
(LL.layoutParams as LayoutParams).topMargin = mediumMargin
}
newLinearLayout.addView(LL)
}
}
selected_contacts.addView(newLinearLayout)
} }
private fun getNames(): List<Contact> { private fun getNames(): List<Contact> {

View File

@ -1,5 +0,0 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size
android:width="@dimen/medium_margin"
android:height="0dp" />
</shape>

View File

@ -9,16 +9,23 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_margin" android:layout_margin="@dimen/activity_margin"
android:divider="@drawable/empty_divider" android:orientation="vertical"
android:orientation="horizontal" android:visibility="gone" />
android:showDividers="middle"
<View
android:id="@+id/message_divider_one"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_below="@+id/selected_contacts"
android:background="@color/divider_grey"
android:importantForAccessibility="no"
android:visibility="gone" /> android:visibility="gone" />
<com.simplemobiletools.commons.views.MyAutoCompleteTextView <com.simplemobiletools.commons.views.MyAutoCompleteTextView
android:id="@+id/new_message_to" android:id="@+id/new_message_to"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/normal_icon_size" android:layout_height="@dimen/normal_icon_size"
android:layout_below="@+id/selected_contacts" android:layout_below="@+id/message_divider_one"
android:layout_marginStart="@dimen/activity_margin" android:layout_marginStart="@dimen/activity_margin"
android:layout_marginEnd="@dimen/activity_margin" android:layout_marginEnd="@dimen/activity_margin"
android:background="@android:color/transparent" android:background="@android:color/transparent"
@ -29,7 +36,7 @@
android:textSize="@dimen/big_text_size" /> android:textSize="@dimen/big_text_size" />
<View <View
android:id="@+id/message_divider" android:id="@+id/message_divider_two"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="1px" android:layout_height="1px"
android:layout_below="@+id/new_message_to" android:layout_below="@+id/new_message_to"