I have question about scrollview. I look up on the internet but I couldn't fit any solution for it. I am using a windowed activity in my application. I use it without a problem until I add a scrollview. Scrollview isn't fit screen properly.
Here is the my activity code.
class ExtendedMedicineInfoActivity : Activity() {
private lateinit var binding: ActivityExtendedMedicineInfoBinding
var medicineUsageData = MedicineUsageData()
var gson = Gson()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityExtendedMedicineInfoBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
medicineUsageData = gson.fromJson(intent.getStringExtra("medicineData"), MedicineUsageData::class.java)
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
val width = displayMetrics.widthPixels
val height = displayMetrics.heightPixels
window.setLayout((width * .8).toInt(), (height * .8).toInt())
val params = window.attributes
params.gravity = Gravity.CENTER
params.x = 0
params.y = -20
window.attributes = params
setFields()
}
private fun setFields() {
binding.expireDateEditText.setText(medicineUsageData.expireDate)
binding.barcodeNumEditText.setText(medicineUsageData.barcodeNum)
binding.companyNameEditText.setText(medicineUsageData.companyName)
binding.drugSubstanceEditText.setText(medicineUsageData.drugSubstance)
binding.medicineNameEditText.setText(medicineUsageData.productName)
binding.partyNumEditText.setText(medicineUsageData.partyNum)
binding.serialNumEditText.setText(medicineUsageData.serialNum)
}
}
This code doesn't produce any errors. I use it for my other windowed activities too. But this is the first time I try to add a scrollview to it.
Here is the xml code for this activity
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/popup_medicine_list"
tools:context=".ExtendedMedicineInfoActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="@string/medicine_information"
android:textSize="32sp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<GridLayout
android:id="@+id/grid_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clipToPadding="false"
android:columnCount="1"
android:padding="16dp">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/medicine_name_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="@string/medicine_name">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/medicine_name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:cursorVisible="false"
android:textColor="@color/black" />
</com.google.android.material.textfield.TextInputLayout>
<Space />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/expire_date_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="@string/expire_date">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/expire_date_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:cursorVisible="false"
android:textColor="@color/black" />
</com.google.android.material.textfield.TextInputLayout>
<Space />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/company_name_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="@string/company_name">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/company_name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:cursorVisible="false"
android:textColor="@color/black" />
</com.google.android.material.textfield.TextInputLayout>
<Space />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/barcode_num_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="@string/barcode_number">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/barcode_num_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:cursorVisible="false"
android:textColor="@color/black" />
</com.google.android.material.textfield.TextInputLayout>
<Space />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/drug_substance_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="@string/drug_substance">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/drug_substance_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:cursorVisible="false"
android:textColor="@color/black" />
</com.google.android.material.textfield.TextInputLayout>
<Space />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/party_num_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="@string/party_number">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/party_num_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:cursorVisible="false"
android:textColor="@color/black" />
</com.google.android.material.textfield.TextInputLayout>
<Space />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/serial_num_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="@string/serial_number">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/serial_num_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:cursorVisible="false"
android:textColor="@color/black" />
</com.google.android.material.textfield.TextInputLayout>
</GridLayout>
</ScrollView>
<com.google.android.material.button.MaterialButton
android:id="@+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/delete" />
</LinearLayout>
This code build and works without error. But this is the result.
As you can see there is another element at the top but it doesn't show.
And there is redundant space under it.
I can fix upper problem by adding android:layout_marginTop="56dp"
to inner GridLayout. But I know it isn't the best solution. Screen size can change or something else. And even if I add this line it doesn't fix my down side redundant space problem.
Can anyone help me with this problem. Thanks in advance.
I figure it out. When I change my code values my problem is solved.
Here is the code which I changed.
window.setLayout((width * .8).toInt(), (height * .8).toInt())
val params = window.attributes
params.gravity = Gravity.CENTER
params.x = 0
params.y = -20
I changed it to this:
window.setLayout((width * .8).toInt(), (height * .9).toInt())
val params = window.attributes
params.gravity = Gravity.CENTER
params.x = 0
params.y = 100