Android TextInputLayout slow rendering inside a RecyclingView - android

I'm using multiple TextInputLayout in each element in a Recycling View.
I noticed that navigating to the fragment containing this list is slow and scrolling for the first few seconds is also slow. As soon as I remove the TextInputLayout the performance drastically improves.
Is there a way to render the TextInputLayout in a recycling view with a good performance?
I mostly want to use this layout for its animations and design.
This is my layout for a individual item in recycling view:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="16dp"
android:background="#FFFFFF"
android:elevation="8dp">
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:background="?attr/selectableItemBackgroundBorderless"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_baseline_add_24" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Bench Press"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginEnd="28dp"
android:text="Remove Exercise"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
android:baselineAligned="false">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textField1"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_weight="1"
android:hint="Reps">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textField2"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_weight="1"
android:hint="Sets">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textField3"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_weight="1"
android:hint="Rest">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment code:
class EditRoutineFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_edit_routine, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val adapter = EditRoutineListAdapter()
requireView().rv_list.apply {
this.adapter = adapter
layoutManager = LinearLayoutManager(context)
setHasFixedSize(true)
}
}
}
Recycling View Adapter
class EditRoutineListAdapter: RecyclerView.Adapter<EditRoutineListAdapter.ViewHolder>() {
class ViewHolder(itemView: ViewGroup): RecyclerView.ViewHolder(itemView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater
.from(parent.context)
.inflate(R.layout.rv_edit_routine_list_item, parent, false) as ViewGroup
return ViewHolder(view)
}
override fun getItemCount() = 16
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
}
}

You can leftout the ImageButton and use the built in option like:
app:endIconMode="custom"
app:endIconDrawable="#drawable/ic_check_circle_24dp"
app:endIconContentDescription="#string/content_description_end_icon"
Check also RecyclerView on Bind method taking too long(that is, onBindViewHolder(VH, int)) should be very simple, and take much less than one millisecond for all but the most complex items.

Related

Horizontal RecyclerView in Horizontal RecyclerView is not scrolling

I have horizontal recycler view in which each child has nested scroll view in which I have another horizontal recycler view. View seems to bo displaying fine but I am not able to scroll child recycler view. When I am trying to scroll - parent recycler view is scrolling, which is not what I want. Already was trying to set android:nestedScrollingEnabled="false” but it’s not working.
Main view xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".views.home.WeatherView">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_WeatherRecycler"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/pb_WeatherLoading"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Parent recycler view item xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
app:cardCornerRadius="40dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="#drawable/ic_location">
<ImageView
android:id="#+id/iv_CityImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
tools:srcCompat="#tools:sample/avatars"
/>
<androidx.core.widget.NestedScrollView
android:id="#+id/sv_ScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv_LocationIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintEnd_toStartOf="#+id/tv_CityName"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_location" />
<TextView
android:id="#+id/tv_CityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="#+id/iv_LocationIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/iv_LocationIcon"
app:layout_constraintTop_toTopOf="#+id/iv_LocationIcon" />
<TextView
android:id="#+id/tv_CurrentTemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="400dp"
android:text="TextView"
android:textSize="48sp"
app:layout_constraintBottom_toTopOf="#+id/tv_CurrentWeatherDesc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv_LocationIcon"
app:layout_constraintVertical_chainStyle="spread" />
<TextView
android:id="#+id/tv_CurrentWeatherDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="TextView"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#+id/tv_SunriseLabel"
app:layout_constraintEnd_toEndOf="#+id/tv_CurrentTemp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/tv_CurrentTemp"
app:layout_constraintTop_toBottomOf="#+id/tv_CurrentTemp" />
<TextView
android:id="#+id/tv_SunriseLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Sunrise"
app:layout_constraintBottom_toTopOf="#+id/tv_SunsetLabel"
app:layout_constraintEnd_toStartOf="#+id/tv_SunriseValue"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_CurrentWeatherDesc" />
<TextView
android:id="#+id/tv_SunsetLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="32dp"
android:text="Sunset"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/tv_SunsetValue"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_SunriseLabel" />
<TextView
android:id="#+id/tv_SunriseValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="SunriseVal"
app:layout_constraintBaseline_toBaselineOf="#+id/tv_SunriseLabel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/tv_SunriseLabel" />
<TextView
android:id="#+id/tv_SunsetValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="SunsetVal"
app:layout_constraintBaseline_toBaselineOf="#+id/tv_SunsetLabel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/tv_SunsetLabel" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_HourlyForecastRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="32dp"
android:scrollbars="horizontal|vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_SunsetLabel" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Child item xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="10dp"
app:cardElevation="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv_HourlyForecast_Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="#+id/iv_ForecastHourly_WeatherIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/iv_ForecastHourly_WeatherIcon"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/tv_HourlyForecast_Temp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_HourlyForecast_Time"
app:srcCompat="#drawable/ic_error" />
<TextView
android:id="#+id/tv_HourlyForecast_Temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/iv_ForecastHourly_WeatherIcon" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Main view code:
#AndroidEntryPoint
class WeatherView : Fragment() {
private var _binding: FragmentWeatherViewBinding? = null
private val binding: FragmentWeatherViewBinding get() = _binding!!
private val viewModel by viewModels<WeatherViewModel>()
private lateinit var weatherAdapter: WeatherAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentWeatherViewBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val cities = getSavedCitiesFromFirebase()
viewModel.getWeatherAndImageForCities(cities)
setupRecyclerView()
observeWeatherData()
}
private fun observeWeatherData() {
viewModel.citiesLoadingLiveData.observe(viewLifecycleOwner, { isLoading ->
if (isLoading) {
showLoading(true)
} else {
showLoading(false)
}
})
viewModel.citiesDataLiveData.observe(viewLifecycleOwner, { cities ->
if (cities != null) {
weatherAdapter.setCities(cities)
}
})
viewModel.cityFetchErrorLiveData.observe(viewLifecycleOwner, {
/* To Do */
})
}
private fun setupRecyclerView() {
weatherAdapter = WeatherAdapter()
binding.rvWeatherRecycler.apply {
layoutManager = LinearLayoutManager(
requireContext(), RecyclerView.HORIZONTAL, false
)
adapter = weatherAdapter
}
}
private fun showLoading(isLoading: Boolean) {
binding.pbWeatherLoading.visibility = if (isLoading) View.VISIBLE else View.GONE
}
private fun getSavedCitiesFromFirebase(): List<String> {
return listOf("Berlin", "Amsterdam")
}
}
Parent recycler view adapter code:
class WeatherAdapter: RecyclerView.Adapter<WeatherAdapter.WeatherViewHolder>() {
private var cities: MutableList<CityData> = mutableListOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WeatherViewHolder {
val itemBinding = ItemWeatherBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return WeatherViewHolder(itemBinding.root, itemBinding)
}
override fun onBindViewHolder(holder: WeatherViewHolder, position: Int) {
holder.bind(cities[position])
}
override fun getItemCount(): Int {
return cities.size
}
fun setCities(newCities: List<CityData>) {
cities.clear()
cities.addAll(newCities)
notifyDataSetChanged()
}
class WeatherViewHolder(private val itemView: View, private val itemBinding: ItemWeatherBinding): RecyclerView.ViewHolder(itemBinding.root) {
fun bind(item: CityData) {
itemBinding.rvHourlyForecastRecycler.apply {
layoutManager = LinearLayoutManager(
itemView.context, RecyclerView.HORIZONTAL, false
)
adapter = HourlyForecastAdapter(item.forecastModel.list)
setHasFixedSize(true)
}
itemBinding.tvCityName.text = item.weatherModel.name
itemBinding.tvCurrentTemp.text = TemperatureConverter.convertKelvinToCelsius(item.weatherModel.main.temp).toString()
itemBinding.tvCurrentWeatherDesc.text = item.weatherModel.weather[0].description
itemBinding.tvSunriseValue.text = TimeConverter.convertTime(item.weatherModel.sys.sunrise)
itemBinding.tvSunsetValue.text = TimeConverter.convertTime(item.weatherModel.sys.sunset)
Glide
.with(itemBinding.ivCityImage)
.load(item.cityImg)
.error(R.drawable.ic_error)
.centerCrop()
.into(itemBinding.ivCityImage)
}
}
}
Child recycler view adapter code:
class HourlyForecastAdapter(private val forecast: List<WeatherForecast>): RecyclerView.Adapter<HourlyForecastAdapter.HourlyForecastViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HourlyForecastViewHolder {
val itemBinding = ItemWeatherHourlyForecastBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return HourlyForecastViewHolder(itemBinding)
}
override fun onBindViewHolder(holder: HourlyForecastViewHolder, position: Int) {
holder.bind(forecast[position])
}
override fun getItemCount(): Int {
return forecast.size
}
class HourlyForecastViewHolder(private val itemBinding: ItemWeatherHourlyForecastBinding): RecyclerView.ViewHolder(itemBinding.root) {
fun bind(item: WeatherForecast) {
Timber.d("DT: ${item.dt}")
itemBinding.tvHourlyForecastTime.text = TimeConverter.convertTime(item.dt.toInt())
itemBinding.tvHourlyForecastTemp.text = TemperatureConverter.convertKelvinToCelsius(item.main.temp).toString()
Glide
.with(itemBinding.ivForecastHourlyWeatherIcon)
.load("https://openweathermap.org/img/wn/${item.weather[0].icon}#2x.png")
.error(R.drawable.ic_error)
.centerCrop()
.into(itemBinding.ivForecastHourlyWeatherIcon)
}
}
}

TextView in ConstraintLayout (for MaterialCardView) not in vertical centre

I'm trying to vertically align my TextView to the centre using constraints within my MaterialCardView but it won't move at all for some reason. Does anyone know what's causing this problem to occur? Do constraints need to be used at all to achieve this? The affected component is the MaterialCardView with a white border. The word 'Information' + the drawable don't seem to be exactly in the vertical centre for some reason. It looks like there is a bit more space above the text view than below it for some strange reason (indicated by the orange line).
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/myCardViewA"
app:cardUseCompatPadding="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/myConstraintLayoutA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ImageButton
android:id="#+id/myImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="#drawable/ic_chevron_down"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/myTextViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toEndOf="#+id/myImageButton"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="#+id/myTextViewSubtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
app:layout_constrainedWidth="true"
app:layout_constraintStart_toEndOf="#+id/myImageButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/myTextViewTitle"
app:layout_constraintHorizontal_bias="0.0"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Button"
android:textAllCaps="false"
android:padding="12dp"
app:layout_constraintTop_toBottomOf="#+id/myTextViewSubtitle"
app:layout_constraintBottom_toTopOf="#+id/myCardViewB"
app:layout_constraintStart_toEndOf="#+id/myImageButton"
app:layout_constraintHorizontal_bias="0.0" />
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/myCardViewB"
android:clickable="false"
android:focusable="false"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#android:color/transparent"
app:contentPadding="12dp"
app:cardElevation="0dp"
app:layout_constraintTop_toBottomOf="#+id/myButton"
app:layout_constraintStart_toEndOf="#+id/myImageButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#+id/myTextViewDescription"
app:layout_constraintHorizontal_bias="0.0">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/myConstraintLayoutB"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/myTextViewinCardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.0"
android:text=""
style="#android:style/TextAppearance.Medium"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<TextView
android:id="#+id/myTextViewDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/myTextViewTitle"
app:layout_constraintTop_toBottomOf="#+id/myCardViewB"
app:layout_constraintEnd_toEndOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
Kotlin
class MyFragment : androidx.fragment.app.Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.my_fragment, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
val v = view
val imageGetter = Html.ImageGetter { name ->
val resId = when (name) {
"door" -> { R.drawable.ic_door }
else -> { throw IllegalArgumentException("what is $name") }
}
ResourcesCompat.getDrawable(resources, resId, requireActivity().theme)?.apply {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
}
}
val info = getString(R.string.information)
val htmlTxt = "<img src=\"door\"/> $info
val myTxt = Html.fromHtml(htmlTxt, Html.FROM_HTML_MODE_COMPACT, imageGetter, null)
super.onActivityCreated(savedInstanceState)
}
}
Just remove app:layout_constraintHorizontal_bias="0.0"
The maximum is app:layout_constraintHorizontal_bias="1.0"
When you set "0.0" that mean you want it to the left and when it is "1.0" that mean it is to the right.

Disable certain part of fragment for scrolling even though it's inside a nested scrollview

Currently I am finishing the last details of my project and I am trying to keep a certain part inside a nested scrollview to stay on top of the screen and let only the recyclerview scroll.
This is the code I have right now:
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/marktplaats_refresh"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:tint="#9F000000"
app:layout_constraintCircleRadius="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/offer_bg"/>
<Button
android:id="#+id/verkoopEenProduct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:background="#null"
android:text="Verkoop een product"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<EditText
android:id="#+id/button5"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:background="#drawable/light_contact_buttons"
android:hint="Zoek in marktplaats"
android:textAlignment="center"
android:gravity="center_vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView33"/>
<TextView
android:id="#+id/textView32"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="Marktplaats"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/verkoopEenProduct"/>
<TextView
android:id="#+id/textView33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Hier vind u alles wat u nodig\nhebt voor uw praktijk"
android:textColor="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView32"/>
<TextView
android:id="#+id/textView34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Alle producten"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView14"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/marktplaats_recyclerview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView34"/>
<Button
android:id="#+id/button11"
android:translationX="-20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#null"
android:rotation="180"
android:text="➜"
android:textColor="#color/white"
android:textSize="36sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
And this is my kotlin
class MarktplaatsOverview : ApplicationFragment(){
var products: Array<MarketModel> = arrayOf()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState)
return inflater.inflate(R.layout.fragment_marktplaats, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
marktplaats_refresh.setOnRefreshListener {
refreshData()
marktplaats_refresh.isRefreshing = false
}
button11.setOnClickListener {
navController.navigate(R.id.action_marktplaatsOverview_to_more)
}
button5.requestFocus()
refreshData()
verkoopEenProduct.setOnClickListener {
navController.navigate(R.id.action_marktplaatsOverview_to_marktplaatsDetail)
}
}
private fun refreshData() {
if (DataStorage.market.isNotEmpty()) {
setupRecyclerView()
} else {
SharedInstance.api.getAllMarketItems {
if (isAdded) {
setupRecyclerView()
}
}
}
}
private fun setupRecyclerView(){
SharedInstance.api.getAllMarketItems {
products = DataStorage.market
val xLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true)
xLayoutManager.stackFromEnd = true
marktplaats_recyclerview.layoutManager = xLayoutManager
marktplaats_recyclerview.adapter = MarktplaatsAdapter(products)
}
}
}
Does anyone have a solution for this awkward problem? I've been trying to do some restructuring of the swiperefreshlayout, scrollviews and constraints but no succes so far.
I also can't find anything on the internet where someone wants the same as me...
As per your question description, I am assuming that you face an issue that recycler view scroll or take focus automatically while you open screen and you need to stick layout at the top so pass this field on your parent layout.
android:descendantFocusability="blocksDescendants"
or replace your SwipeRefreshLayout
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/marktplaats_refresh"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="match_parent">
I hope it helps you.

RecyclerView items not showing properly

In my project, I have used RecyclerView which is shown in layout-preview as I expected but the problem is when I run the application in emulator/device the items of RecyclerView not showing as layout-preview shown.
Here is the XML of recyclerview.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".view.LActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="#layout/rv_sample" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
android:src="#drawable/ic_add"
android:id="#+id/fabAdd"
app:tint="#color/white"
app:backgroundTint="#color/colorPrimary"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
XML of recyclerview items.
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
app:cardCornerRadius="10dp"
android:minHeight="60dp"
app:cardUseCompatPadding="true"
app:strokeColor="#color/design_default_color_secondary_variant"
app:strokeWidth="1dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="10dp"
android:drawableStart="#drawable/arrow_right"
android:fontFamily="monospace"
android:text="#{item.word}"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{item.type}"
android:layout_marginEnd="10dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#id/tvWord" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
Adapter class
class MyAdapter(private var itemList: List<Item>, private val listener: ItemClickListener) :
RecyclerView.Adapter<MyAdapter.VHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = RvSampleBinding.inflate(inflater)
return VHolder(binding)
}
override fun getItemCount(): Int = itemList.size
override fun onBindViewHolder(holder: VHolder, position: Int) = holder.bind(itemList[position])
inner class VHolder(private val binding: RvSampleBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: Item) {
binding.item = item
binding.root.setOnClickListener {
listener.onItemClick(item)
}
binding.executePendingBindings()
}
}
Screenshot of layout-preview
And Screenshot of actual output.
I have run the application in several emulators and devices but can't figure out where is the problem.
Here I am going to answer my own question. So that this answer will help other users who will face same issue.
Thanks, #MikeM for his helpful comment. All credit goes to him.
Problem: The problem is in my adapter, I was inflating the item-layout without the parent viewgroup passed.
Cause: If you don't pass the parent in your layout-inflater that will inflate your layout with default layout-params and that wrap your content in both directions(width and height), even though you might have match_parent for the layout_width.
in my case, I have tried like this
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = RvSampleBinding.inflate(inflater) // here i didn't pass viewgroup and boolean parameters.
return VHolder(binding)
}
Solution: My problem is solved by modifying the onCreateViewHolder like the following.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = RvSampleBinding.inflate(inflater,parent,false) //here i have passed viewgroup and boolean parameter and that's solve my problem
return VHolder(binding)
}
So basically your parent Layout is constraint layout, so you have to add all constraint related to all the views, One suggestion just used another parent layout instead of Constraint layout just for the check/validation purpose.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".RecyclerViewActivity"
android:id="#+id/recyclerView_ex1">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="#layout/recycler_view_item_ex1" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
android:src="#drawable/ic_add"
android:id="#+id/fabAdd"
app:tint="#color/cardview_light_background"
app:backgroundTint="#color/colorPrimary"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
itemview
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tool="http://schemas.android.com/tools"
android:padding="5dp"
app:cardCornerRadius="10dp"
android:minHeight="60dp"
app:cardUseCompatPadding="true"
app:strokeColor="#color/colorAccent"
app:strokeWidth="1dp"
xmlns:android="http://schemas.android.com/apk/res/android"
tool:context=".RecyclerViewActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="10dp"
android:drawableStart="#drawable/arrow_right"
android:fontFamily="monospace"
android:text="word"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="#+id/tvType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="type"
android:layout_marginEnd="10dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#id/tvWord"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
Please vote if it works.
Happy coding. Thanks

textStyle="bold" not working in DialogFragment

I'm struggling with a custom DialogFragment layout defined as below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="360dp"
android:background="#color/colorDarkerGreen"
android:minWidth="280dp"
android:orientation="vertical">
<ImageView
android:id="#+id/ivNoRetails"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:scaleType="centerInside"
android:src="#drawable/no_retails_nearby_dialog"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:text="#string/no_retails_nearby"
android:textAlignment="center"
android:textColor="#color/colorBaseWhite"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ivNoRetails" />
<TextView
android:id="#+id/tvHopeGetThere"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="#string/hope_we_get_there"
android:textAlignment="center"
android:textColor="#color/colorBaseWhite"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="#+id/textView2"
app:layout_constraintStart_toStartOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<com.google.android.material.button.MaterialButton
android:id="#+id/btClose"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
android:text="#string/understood"
android:textColor="#color/colorBaseWhite"
app:backgroundTint="#android:color/transparent"
app:cornerRadius="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvHopeGetThere"
app:strokeColor="#color/colorBaseWhite"
app:strokeWidth="2dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
I the preview from the layout editor, I can see the text of tvHopeGetThere TextView displayed as bold, as expected.
However, when I show the dialog in my activity, the text is displayed as with a normal textStyle.
DialogFragment:
class NoRetailsNearbyDialogFragment : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.no_retails_alert_dialog, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btClose.setOnClickListener {
dismiss()
}
}
companion object {
fun newInstance(): NoRetailsNearbyDialogFragment {
return NoRetailsNearbyDialogFragment()
}
}
}
Display of the dialog in my fragment:
val fragment = NoRetailsNearbyDialogFragment.newInstance()
fragment.show(fragmentManager, "")
I really can't understand why the textview has this strange behavior.
there is one method, you can set textview typeface by calling setTypeface method,
see the following
your can do it programatically as:
textView.setTypeface(null, Typeface.BOLD);
or you can do it by html approach as:
TextView t = new TextView(mContext);
t.setText(Html.fromHtml("<b>This is bold</b>"));
Problem was related to Calligraphy library that was overriding common textStyle behavior. Once I removed it, everything started to work fine :)

Categories

Resources