I have created this layout which as you can see on the second picture is not horizontally centered, with the card view always being left aligned:
above: picture from xml design view.
below: picture from emulator.
I have tried:
- changing the
MaterialCardView
's width and height to 0dp, which just removed the view entirely. - setting
gravity
andlayout_gravity
to center - setting
weight = 1
- the above in linear layout
- tested in both emulator and physical device.
Below is the xml:
<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="120sp">
<com.google.android.material.card.MaterialCardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginVertical="10dp"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="30dp"
app:cardElevation="10dp"
app:strokeColor="@color/black"
app:strokeWidth="3dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
This layout is held within a recycler view:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
app:layout_constraintBottom_toBottomOf="parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/above_item"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/below_item" />
The ListAdapter for the RecyclerView:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val binding = LayoutAlarmBinding.inflate(LayoutInflater.from(parent.context))
return MyViewHolder(binding)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val currentItem = alarmList[position]
val recurring = currentItem.repeat
val switch = holder.itemView.findViewById<SwitchCompat>(R.id.switch_alarm)
}
}
home binding:
class HomeFragment : Fragment() {
lateinit var binding: FragmentHomeBetterBinding
private lateinit var alarmViewModel: AlarmViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentHomeBetterBinding.inflate(inflater, container, false)
// RecyclerView
val adapter = AlarmListAdapter()
val recyclerView = binding.recyclerView
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(requireContext())
//ViewModel
alarmViewModel = ViewModelProvider(this)[AlarmViewModel::class.java]
alarmViewModel.readAlarmData.observe(viewLifecycleOwner) { alarm ->
adapter.setData(alarm)
setTvNextAlarm(adapter, alarm)
}
binding.btnAddAlarm.setOnClickListener {
Navigation.findNavController(requireView())
.navigate(R.id.action_homeFragment_to_newAlarmFragment)
}
return binding.root
}